Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  //========================================================================
  //Copyright 2007-2009 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;
 
 
An output used for writing data with json format.

Author(s):
David Yu
Created:
Nov 20, 2009
 
 public final class JsonOutput implements OutputStatefulOutput
 {
 
     private final JsonGenerator generator;
     private Schema<?> schema;
     private final boolean numeric;
     private boolean lastRepeated;
     private int lastNumber;
 
     public JsonOutput(JsonGenerator generator)
     {
         this(generatorfalse);
     }
 
     public JsonOutput(JsonGenerator generatorboolean numeric)
     {
         this. = generator;
         this. = numeric;
     }
 
     public JsonOutput(JsonGenerator generatorboolean numericSchema<?> schema)
     {
         this(generatornumeric);
         this. = schema;
     }

    
Resets this output for re-use.
 
     public JsonOutput reset()
     {
          = false;
          = 0;
         return this;
     }

    
Before serializing a message/object tied to a schema, this should be called. This also resets the internal state of this output.
 
     public JsonOutput use(Schema<?> schema)
     {
         this. = schema;
         return reset();
     }

    
Returns whether the incoming messages' field names are numeric.
 
     public boolean isNumeric()
     {
         return ;
     }

    
Gets the last field number written.
 
     public int getLastNumber()
     {
         return ;
     }

    
Returns true if the last written field was a repeated field.
 
     public boolean isLastRepeated()
     {
         return ;
     }
 
     @Override
     public void updateLast(Schema<?> schemaSchema<?> lastSchema)
    {
        if (lastSchema != null && lastSchema == this.)
        {
            this. = schema;
        }
    }
    @Override
    public void writeBool(int fieldNumberboolean valueboolean repeatedthrows IOException
    {
        if ( == fieldNumber)
        {
            // repeated field
            .writeBoolean(value);
            return;
        }
        final JsonGenerator generator = this.;
        if ()
            generator.writeEndArray();
        final String name =  ? Integer.toString(fieldNumber) :
                .getFieldName(fieldNumber);
        if (repeated)
        {
            generator.writeArrayFieldStart(name);
            generator.writeBoolean(value);
        }
        else
            generator.writeBooleanField(namevalue);
         = fieldNumber;
         = repeated;
    }
    @Override
    public void writeByteArray(int fieldNumberbyte[] valueboolean repeatedthrows IOException
    {
        if ( == fieldNumber)
        {
            // repeated field
            .writeBinary(value);
            return;
        }
        final JsonGenerator generator = this.;
        if ()
            generator.writeEndArray();
        final String name =  ? Integer.toString(fieldNumber) :
                .getFieldName(fieldNumber);
        if (repeated)
        {
            generator.writeArrayFieldStart(name);
            generator.writeBinary(value);
        }
        else
        {
            generator.writeFieldName(name);
            generator.writeBinary(value);
        }
         = fieldNumber;
         = repeated;
    }
    @Override
    public void writeByteRange(boolean utf8Stringint fieldNumberbyte[] value,
            int offsetint lengthboolean repeatedthrows IOException
    {
        if ( == fieldNumber)
        {
            // repeated field
            if (utf8String)
                .writeUTF8String(valueoffsetlength);
            else
                .writeBinary(valueoffsetlength);
            return;
        }
        final JsonGenerator generator = this.;
        if ()
            generator.writeEndArray();
        final String name =  ? Integer.toString(fieldNumber) :
                .getFieldName(fieldNumber);
        if (repeated)
        {
            generator.writeArrayFieldStart(name);
            if (utf8String)
                generator.writeUTF8String(valueoffsetlength);
            else
                generator.writeBinary(valueoffsetlength);
        }
        else
        {
            generator.writeFieldName(name);
            if (utf8String)
                generator.writeUTF8String(valueoffsetlength);
            else
                generator.writeBinary(valueoffsetlength);
        }
         = fieldNumber;
         = repeated;
    }
    @Override
    public void writeBytes(int fieldNumberByteString valueboolean repeatedthrows IOException
    {
        writeByteArray(fieldNumbervalue.getBytes(), repeated);
    }
    @Override
    public void writeDouble(int fieldNumberdouble valueboolean repeatedthrows IOException
    {
        if ( == fieldNumber)
        {
            // repeated field
            .writeNumber(value);
            return;
        }
        final JsonGenerator generator = this.;
        if ()
            generator.writeEndArray();
        final String name =  ? Integer.toString(fieldNumber) :
                .getFieldName(fieldNumber);
        if (repeated)
        {
            generator.writeArrayFieldStart(name);
            generator.writeNumber(value);
        }
        else
            generator.writeNumberField(namevalue);
         = fieldNumber;
         = repeated;
    }
    @Override
    public void writeEnum(int fieldNumberint valueboolean repeatedthrows IOException
    {
        writeInt32(fieldNumbervaluerepeated);
    }
    @Override
    public void writeFixed32(int fieldNumberint valueboolean repeatedthrows IOException
    {
        writeInt32(fieldNumbervaluerepeated);
    }
    @Override
    public void writeFixed64(int fieldNumberlong valueboolean repeatedthrows IOException
    {
        writeInt64(fieldNumbervaluerepeated);
    }
    @Override
    public void writeFloat(int fieldNumberfloat valueboolean repeatedthrows IOException
    {
        if ( == fieldNumber)
        {
            // repeated field
            .writeNumber(value);
            return;
        }
        final JsonGenerator generator = this.;
        if ()
            generator.writeEndArray();
        final String name =  ? Integer.toString(fieldNumber) :
                .getFieldName(fieldNumber);
        if (repeated)
        {
            generator.writeArrayFieldStart(name);
            generator.writeNumber(value);
        }
        else
            generator.writeNumberField(namevalue);
         = fieldNumber;
         = repeated;
    }
    @Override
    public void writeInt32(int fieldNumberint valueboolean repeatedthrows IOException
    {
        if ( == fieldNumber)
        {
            // repeated field
            .writeNumber(value);
            return;
        }
        final JsonGenerator generator = this.;
        if ()
            generator.writeEndArray();
        final String name =  ? Integer.toString(fieldNumber) :
                .getFieldName(fieldNumber);
        if (repeated)
        {
            generator.writeArrayFieldStart(name);
            generator.writeNumber(value);
        }
        else
            generator.writeNumberField(namevalue);
         = fieldNumber;
         = repeated;
    }
    @Override
    public void writeInt64(int fieldNumberlong valueboolean repeatedthrows IOException
    {
        if ( == fieldNumber)
        {
            // repeated field
            .writeNumber(value);
            return;
        }
        final JsonGenerator generator = this.;
        if ()
            generator.writeEndArray();
        final String name =  ? Integer.toString(fieldNumber) :
                .getFieldName(fieldNumber);
        if (repeated)
        {
            generator.writeArrayFieldStart(name);
            generator.writeNumber(value);
        }
        else
            generator.writeNumberField(namevalue);
         = fieldNumber;
         = repeated;
    }
    @Override
    public void writeSFixed32(int fieldNumberint valueboolean repeatedthrows IOException
    {
        writeInt32(fieldNumbervaluerepeated);
    }
    @Override
    public void writeSFixed64(int fieldNumberlong valueboolean repeatedthrows IOException
    {
        writeInt64(fieldNumbervaluerepeated);
    }
    @Override
    public void writeSInt32(int fieldNumberint valueboolean repeatedthrows IOException
    {
        writeInt32(fieldNumbervaluerepeated);
    }
    @Override
    public void writeSInt64(int fieldNumberlong valueboolean repeatedthrows IOException
    {
        writeInt64(fieldNumbervaluerepeated);
    }
    @Override
    public void writeString(int fieldNumberString valueboolean repeatedthrows IOException
    {
        if ( == fieldNumber)
        {
            // repeated field
            .writeString(value);
            return;
        }
        final JsonGenerator generator = this.;
        if ()
            generator.writeEndArray();
        final String name =  ? Integer.toString(fieldNumber) :
                .getFieldName(fieldNumber);
        if (repeated)
        {
            generator.writeArrayFieldStart(name);
            generator.writeString(value);
        }
        else
            generator.writeStringField(namevalue);
         = fieldNumber;
         = repeated;
    }
    @Override
    public void writeUInt32(int fieldNumberint valueboolean repeatedthrows IOException
    {
        writeInt32(fieldNumbervaluerepeated);
    }
    @Override
    public void writeUInt64(int fieldNumberlong valueboolean repeatedthrows IOException
    {
        writeInt64(fieldNumbervaluerepeated);
    }
    @Override
    public <T> void writeObject(final int fieldNumberfinal T valuefinal Schema<T> schema,
            final boolean repeatedthrows IOException
    {
        final JsonGenerator generator = this.;
        final Schema<?> lastSchema = this.;
        if ( != fieldNumber)
        {
            if ()
                generator.writeEndArray();
            final String name =  ? Integer.toString(fieldNumber) :
                    lastSchema.getFieldName(fieldNumber);
            if (repeated)
                generator.writeArrayFieldStart(name);
            else
                generator.writeFieldName(name);
        }
        // reset
        this. = schema;
         = 0;
         = false;
        generator.writeStartObject();
        // recursive write
        schema.writeTo(thisvalue);
        if ()
            generator.writeEndArray();
        generator.writeEndObject();
        // restore state
         = fieldNumber;
         = repeated;
        this. = lastSchema;
    }

    
Writes a ByteBuffer field.
    @Override
    public void writeBytes(int fieldNumberByteBuffer valueboolean repeatedthrows IOException
    {
        writeByteRange(falsefieldNumbervalue.array(), value.arrayOffset() + value.position(),
                value.remaining(), repeated);
    }
New to GrepCode? Check out our FAQ X