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;
 
 import static com.fasterxml.jackson.core.JsonToken.END_ARRAY;
 import static com.fasterxml.jackson.core.JsonToken.END_OBJECT;
 import static com.fasterxml.jackson.core.JsonToken.FIELD_NAME;
 import static com.fasterxml.jackson.core.JsonToken.START_ARRAY;
 import static com.fasterxml.jackson.core.JsonToken.START_OBJECT;
 import static com.fasterxml.jackson.core.JsonToken.VALUE_FALSE;
 import static com.fasterxml.jackson.core.JsonToken.VALUE_NULL;
 import static com.fasterxml.jackson.core.JsonToken.VALUE_STRING;
 import static com.fasterxml.jackson.core.JsonToken.VALUE_TRUE;
 
 
An input used for reading data with json format.

Author(s):
David Yu
Created:
Nov 20, 2009
 
 public final class JsonInput implements Input
 {

    
The wrapped json parser.
 
     public final JsonParser parser;

    
If true, the field number will be used on json keys.
 
     public final boolean numeric;
 
     private boolean lastRepeated;
     private String lastName;
     private int lastNumber;
 
     public JsonInput(JsonParser parser)
     {
         this(parserfalse);
     }
 
     public JsonInput(JsonParser parserboolean numeric)
     {
         this. = parser;
         this. = numeric;
     }

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

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

    
Returns true if the last read field was a repeated field.
 
     public boolean isLastRepeated()
     {
         return ;
     }

    
Resets this input.
 
     public JsonInput reset()
     {
          = false;
          = null;
          = 0;
         return this;
    }
    @Override
    public <T> void handleUnknownField(int fieldNumberSchema<T> schemathrows IOException
    {
        if (.getCurrentToken().isScalarValue())
        {
            // numeric json
            // we can skip this unknown field
            if ()
            {
                 = false;
                // skip the scalar elements
                while (.nextToken() != )
                    ;
            }
            return;
        }
        throw new JsonInputException("Unknown field: " +
                ( ? fieldNumber : ) +
                " on message " +
                schema.messageFullName());
    }
    @Override
    public <T> int readFieldNumber(final Schema<T> schemathrows IOException
    {
        if ()
        {
            if (.getCurrentToken() == )
            {
                // skip null elements
                JsonToken jt;
                while ( == (jt = .nextToken()))
                    ;
                if (jt == )
                {
                    // remaining elements were all null
                    // move to the next field
                     = false;
                    return readFieldNumber(schema);
                }
            }
            return ;
        }
        return readFieldNumber(schema);
    }
    private <T> int readFieldNumber(final Schema<T> schemafinal JsonParser parser)
            throws IOException
    {
        for (;;)
        {
            if (parser.nextToken() == )
                return 0;
            if (parser.getCurrentToken() != )
            {
                throw new JsonInputException("Expected token: $field: but was " +
                        parser.getCurrentToken() + " on message " + schema.messageFullName());
            }
            final String name = parser.getCurrentName();
            // move to the next token
            if (parser.nextToken() == )
            {
                JsonToken jt = parser.nextToken();
                // if empty array, read the next field
                if (jt == )
                    continue;
                if (jt == )
                {
                    // skip null elements
                    while ( == (jt = parser.nextToken()))
                        ;
                    // all elements were null.
                    if (jt == )
                        continue;
                }
                final int number =  ? Integer.parseInt(name) :
                        schema.getFieldNumber(name);
                if (number == 0)
                {
                    // unknown field
                    if (parser.getCurrentToken().isScalarValue())
                    {
                        // skip the scalar elements
                        while (parser.nextToken() != )
                            ;
                        continue;
                    }
                    throw new JsonInputException("Unknown field: " + name + " on message " +
                            schema.messageFullName());
                }
                 = true;
                 = name;
                 = number;
                return number;
            }
            // skip null value
            if (parser.getCurrentToken() == )
                continue;
            final int number =  ? Integer.parseInt(name) :
                    schema.getFieldNumber(name);
            if (number == 0)
            {
                // we can skip this unknown field
                if (parser.getCurrentToken().isScalarValue())
                    continue;
                throw new JsonInputException("Unknown field: " + name + " on message " +
                        schema.messageFullName());
            }
             = name;
             = number;
            return number;
        }
    }
    @Override
    public boolean readBool() throws IOException
    {
        final JsonToken jt = .getCurrentToken();
        if ( && .nextToken() == )
             = false;
        if (jt == )
            return true;
        if (jt == )
            return false;
        throw new JsonInputException("Expected token: true/false but was " + jt);
    }
    @Override
    public byte[] readByteArray() throws IOException
    {
        final byte[] value = .getBinaryValue();
        if ( && .nextToken() == )
             = false;
        return value;
    }
    @Override
    public ByteString readBytes() throws IOException
    {
        return ByteString.wrap(readByteArray());
    }
    @Override
    public double readDouble() throws IOException
    {
        final double value = .getDoubleValue();
        if ( && .nextToken() == )
             = false;
        return value;
    }
    @Override
    public int readEnum() throws IOException
    {
        return readInt32();
    }
    @Override
    public int readFixed32() throws IOException
    {
        return readInt32();
    }
    @Override
    public long readFixed64() throws IOException
    {
        return readInt64();
    }
    @Override
    public float readFloat() throws IOException
    {
        final float value = .getFloatValue();
        if ( && .nextToken() == )
             = false;
        return value;
    }
    @Override
    public int readInt32() throws IOException
    {
        final int value = .getIntValue();
        if ( && .nextToken() == )
             = false;
        return value;
    }
    @Override
    public long readInt64() throws IOException
    {
        final long value = .getLongValue();
        if ( && .nextToken() == )
             = false;
        return value;
    }
    @Override
    public int readSFixed32() throws IOException
    {
        return readInt32();
    }
    @Override
    public long readSFixed64() throws IOException
    {
        return readInt64();
    }
    @Override
    public int readSInt32() throws IOException
    {
        return readInt32();
    }
    @Override
    public long readSInt64() throws IOException
    {
        return readInt64();
    }
    @Override
    public String readString() throws IOException
    {
        if (.getCurrentToken() != )
            throw new JsonInputException("Expected token: string but was " + .getCurrentToken());
        final String value = .getText();
        if ( && .nextToken() == )
             = false;
        return value;
    }
    @Override
    public int readUInt32() throws IOException
    {
        return readInt32();
    }
    @Override
    public long readUInt64() throws IOException
    {
        return readInt64();
    }
    @Override
    public <T> T mergeObject(T valuefinal Schema<T> schemathrows IOException
    {
        if (.getCurrentToken() != )
        {
            throw new JsonInputException("Expected token: { but was " +
                    .getCurrentToken() + " on " +  + " of message " +
                    schema.messageFullName());
        }
        final int lastNumber = this.;
        final boolean lastRepeated = this.;
        final String lastName = this.;
        // reset
        this. = false;
        if (value == null)
            value = schema.newMessage();
        schema.mergeFrom(thisvalue);
        if (.getCurrentToken() != )
        {
            throw new JsonInputException("Expected token: } but was " +
                    .getCurrentToken() + " on " + lastName + " of message " +
                    schema.messageFullName());
        }
        if (!schema.isInitialized(value))
            throw new UninitializedMessageException(valueschema);
        // restore state
        this. = lastNumber;
        this. = lastRepeated;
        this. = lastName;
        if (lastRepeated && .nextToken() == )
            this. = false;
        return value;
    }
    @Override
    public void transferByteRangeTo(Output outputboolean utf8Stringint fieldNumber,
            boolean repeatedthrows IOException
    {
        if (utf8String)
            output.writeString(fieldNumberreadString(), repeated);
        else
            output.writeByteArray(fieldNumberreadByteArray(), repeated);
    }

    
Reads a byte array/ByteBuffer value.
    @Override
    public ByteBuffer readByteBuffer() throws IOException
    {
        return ByteBuffer.wrap(readByteArray());
    }
New to GrepCode? Check out our FAQ X