Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  //========================================================================
  //Copyright 2007-2010 David Yu dyuproject@gmail.com
  //------------------------------------------------------------------------
  //Licensed under the Apache License, Version 2.0 (the "License");
  //you may not use this file except in compliance with the License.
  //You may obtain a copy of the License at 
  //http://www.apache.org/licenses/LICENSE-2.0
  //Unless required by applicable law or agreed to in writing, software
  //distributed under the License is distributed on an "AS IS" BASIS,
 //WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 //See the License for the specific language governing permissions and
 //limitations under the License.
 //========================================================================
 
 package io.protostuff;
 
 import java.util.List;

Json serialization via JsonXOutput.

Author(s):
David Yu
Created:
Jul 2, 2010
 
 public final class JsonXIOUtil
 {
 
     private static final byte[] EMPTY_ARRAY = new byte[] {
             (byte'[', (byte']'
     };
 
     public static <T> byte[] toByteArray(T messageSchema<T> schemaboolean numeric,
             LinkedBuffer buffer)
     {
         if (buffer.start != buffer.offset)
             throw new IllegalArgumentException("Buffer previously used and had not been reset.");
 
         final JsonXOutput output = new JsonXOutput(buffernumericschema);
 
         try
         {
             output.writeStartObject();
 
             schema.writeTo(outputmessage);
 
             if (output.isLastRepeated())
                 output.writeEndArray();
 
             output.writeEndObject();
         }
         catch (IOException e)
         {
             throw new RuntimeException("Serializing to a byte array threw an IOException " +
                     "(should never happen)."e);
         }
 
         return output.toByteArray();
     }

    
Serializes the message into a io.protostuff.LinkedBuffer via JsonXOutput using the given schema with the supplied buffer.
 
     public static <T> void writeTo(LinkedBuffer buffer, T messageSchema<T> schema,
             boolean numeric)
     {
         if (buffer.start != buffer.offset)
             throw new IllegalArgumentException("Buffer previously used and had not been reset.");
 
         final JsonXOutput output = new JsonXOutput(buffernumericschema);
 
         try
         {
             output.writeStartObject();
 
             schema.writeTo(outputmessage);
 
             if (output.isLastRepeated())
                 output.writeEndArray();
 
             output.writeEndObject();
         }
         catch (IOException e)
         {
             throw new RuntimeException("Serializing to a byte array threw an IOException " +
                     "(should never happen)."e);
         }
     }

    
Serializes the message into an java.io.OutputStream via JsonXOutput with the supplied buffer.
 
     public static <T extends Message<T>> void writeTo(OutputStream out, T messageboolean numeric,
             LinkedBuffer buffer)
             throws IOException
     {
         writeTo(outmessagemessage.cachedSchema(), numericbuffer);
    }

    
Serializes the message into an java.io.OutputStream via JsonXOutput using the given schema.
    public static <T> void writeTo(OutputStream out, T messageSchema<T> schemaboolean numeric,
            LinkedBuffer bufferthrows IOException
    {
        if (buffer.start != buffer.offset)
            throw new IllegalArgumentException("Buffer previously used and had not been reset.");
        final JsonXOutput output = new JsonXOutput(bufferoutnumericschema);
        output.writeStartObject();
        schema.writeTo(outputmessage);
        if (output.isLastRepeated())
            output.writeEndArray();
        output.writeEndObject();
        LinkedBuffer.writeTo(outbuffer);
    }

    
Serializes the messages into the io.protostuff.LinkedBuffer using the given schema.
    public static <T> void writeListTo(LinkedBuffer buffer,
            List<T> messagesSchema<T> schemaboolean numeric)
    {
        if (buffer.start != buffer.offset)
            throw new IllegalArgumentException("Buffer previously used and had not been reset.");
        if (messages.isEmpty())
        {
            System.arraycopy(, 0, buffer.bufferbuffer.offset.);
            buffer.offset += .;
            return;
        }
        final JsonXOutput output = new JsonXOutput(buffernumericschema);
        try
        {
            output.writeStartArray();
            boolean first = true;
            for (T m : messages)
            {
                if (first)
                {
                    first = false;
                    output.writeStartObject();
                }
                else
                    output.writeCommaAndStartObject();
                schema.writeTo(outputm);
                if (output.isLastRepeated())
                    output.writeEndArray();
                output.writeEndObject().reset();
            }
            output.writeEndArray();
        }
        catch (IOException e)
        {
            throw new RuntimeException("Serializing to a byte array threw an IOException " +
                    "(should never happen)."e);
        }
    }

    
Serializes the messages into the stream using the given schema with the supplied buffer.
    public static <T> void writeListTo(OutputStream outList<T> messagesSchema<T> schema,
            boolean numericLinkedBuffer bufferthrows IOException
    {
        if (buffer.start != buffer.offset)
            throw new IllegalArgumentException("Buffer previously used and had not been reset.");
        if (messages.isEmpty())
        {
            System.arraycopy(, 0, buffer.bufferbuffer.offset.);
            buffer.offset += .;
            return;
        }
        final JsonXOutput output = new JsonXOutput(bufferoutnumericschema);
        output.writeStartArray();
        boolean first = true;
        for (T m : messages)
        {
            if (first)
            {
                first = false;
                output.writeStartObject();
            }
            else
                output.writeCommaAndStartObject();
            schema.writeTo(outputm);
            if (output.isLastRepeated())
                output.writeEndArray();
            output.writeEndObject().reset();
        }
        output.writeEndArray();
        LinkedBuffer.writeTo(outbuffer);
    }
New to GrepCode? Check out our FAQ X