com.dslplatform.json
Class JsonReader<TContext>

java.lang.Object
  extended by com.dslplatform.json.JsonReader<TContext>
Type Parameters:
TContext - context passed to deserialized object instances

public final class JsonReader<TContext>
extends Object

Object for processing JSON from byte[] and InputStream. DSL-JSON works on byte level (instead of char level). Deserialized instances can obtain TContext information provided with this reader.

JsonReader can be reused by calling process methods.


Nested Class Summary
static interface JsonReader.BindObject<T>
          Existing instances can be provided as target for deserialization.
static class JsonReader.DoublePrecision
           
static class JsonReader.ErrorInfo
           
static interface JsonReader.ReadJsonObject<T extends JsonObject>
           
static interface JsonReader.ReadObject<T>
          Custom objects can be deserialized based on the implementation specified through this interface.
static class JsonReader.UnknownNumberParsing
           
 
Field Summary
protected  byte[] buffer
           
protected  char[] chars
           
 TContext context
           
protected  int doubleLengthLimit
           
protected  JsonReader.DoublePrecision doublePrecision
           
protected  JsonReader.ErrorInfo errorInfo
           
protected  int maxNumberDigits
           
protected  JsonReader.UnknownNumberParsing unknownNumbers
           
 
Constructor Summary
JsonReader(byte[] buffer, int length, TContext context)
          Deprecated. 
JsonReader(byte[] buffer, int length, TContext context, char[] tmp)
          Deprecated. 
JsonReader(byte[] buffer, int length, TContext context, char[] tmp, StringCache keyCache, StringCache valuesCache)
          Deprecated. 
JsonReader(byte[] buffer, TContext context)
          Deprecated. 
JsonReader(byte[] buffer, TContext context, char[] tmp)
          Deprecated. 
JsonReader(byte[] buffer, TContext context, StringCache keyCache, StringCache valuesCache)
          Deprecated. 
 
Method Summary
 StringBuffer appendString(StringBuffer buffer)
           
 StringBuilder appendString(StringBuilder builder)
           
 int calcHash()
           
 int calcWeakHash()
           
 void checkArrayEnd()
          Check if the last read token is an array end
 void checkObjectEnd()
          Check if the last read token is an object end
 void comma()
          Will advance to next token and check if it's comma
<T extends JsonObject>
ArrayList<T>
deserializeCollection(JsonReader.ReadJsonObject<T> readObject)
           
<T extends JsonObject>
void
deserializeCollection(JsonReader.ReadJsonObject<T> readObject, Collection<T> res)
           
<T,S extends T>
ArrayList<T>
deserializeCollection(JsonReader.ReadObject<S> readObject)
           
<T,S extends T>
void
deserializeCollection(JsonReader.ReadObject<S> readObject, Collection<T> res)
           
<T extends JsonObject>
ArrayList<T>
deserializeNullableCollection(JsonReader.ReadJsonObject<T> readObject)
           
<T extends JsonObject>
void
deserializeNullableCollection(JsonReader.ReadJsonObject<T> readObject, Collection<T> res)
           
<T,S extends T>
ArrayList<T>
deserializeNullableCollection(JsonReader.ReadObject<S> readObject)
           
<T,S extends T>
void
deserializeNullableCollection(JsonReader.ReadObject<S> readObject, Collection<T> res)
           
 void endArray()
          Will advance to next token and check if it's array end
 void endObject()
          Will advance to next token and check it it's object end
 int fillName()
           
 int fillNameWeakHash()
           
 int getCurrentIndex()
           
 int getLastHash()
           
 String getLastName()
           
 byte getNextToken()
          Read next token (byte) from input JSON.
 int getTokenStart()
           
<T extends JsonObject>
Iterator<T>
iterateOver(JsonReader.ReadJsonObject<T> reader)
           
<T> Iterator<T>
iterateOver(JsonReader.ReadObject<T> reader)
           
 byte last()
          Which was last byte read from the JSON input.
 int length()
          Valid length of the input buffer.
 ParsingException newParseError(String description)
           
 ParsingException newParseError(String description, int positionOffset)
           
 ParsingException newParseErrorAt(String description, int positionOffset)
           
 ParsingException newParseErrorAt(String description, int positionOffset, Exception cause)
           
 ParsingException newParseErrorFormat(String shortDescription, int positionOffset, String longDescriptionFormat, Object... arguments)
           
 ParsingException newParseErrorWith(String shortDescription, int positionOffset, String longDescriptionPrefix, String longDescriptionMessage, Object argument, String longDescriptionSuffix)
           
 ParsingException newParseErrorWith(String description, Object argument)
           
<T> T
next(Class<T> manifest)
          Will advance to next token and read the JSON into specified type
<T> T
next(Class<T> manifest, T instance)
          Will advance to next token and bind the JSON to provided instance
<T> T
next(JsonReader.BindObject<T> binder, T instance)
          Will advance to next token and bind the JSON to provided instance
<T> T
next(JsonReader.ReadObject<T> reader)
          Will advance to next token and read the JSON into specified type
 String positionDescription()
           
 String positionDescription(int offset)
           
 long positionInStream()
           
 long positionInStream(int offset)
           
 JsonReader<TContext> process(byte[] newBuffer, int newLength)
          Bind byte[] buffer for processing.
 JsonReader<TContext> process(InputStream stream)
          Bind input stream for processing.
 byte read()
          Read next byte from the JSON input.
<T> T[]
readArray(JsonReader.ReadObject<T> readObject, T[] emptyArray)
           
 byte[] readBase64()
           
<T> ArrayList<T>
readCollection(JsonReader.ReadObject<T> readObject)
           
 String readKey()
          Read key value of JSON input.
<K,V> LinkedHashMap<K,V>
readMap(JsonReader.ReadObject<K> readKey, JsonReader.ReadObject<V> readValue)
           
 String readNext()
          Deprecated. 
 char[] readNumber()
          Deprecated. 
<T> LinkedHashSet<T>
readSet(JsonReader.ReadObject<T> readObject)
           
 char[] readSimpleQuote()
          Read simple "ascii string" into temporary buffer.
 String readSimpleString()
          Read simple ascii string.
 String readString()
          Read string from JSON input.
 void reset(InputStream stream)
          Deprecated. 
 int scanNumber()
           
 void semicolon()
          Will advance to next token and check if it's semicolon
 byte skip()
          Skip to next non-whitespace token (byte) Will not allocate memory while skipping over JSON input.
 void startArray()
          Will advance to next token and check if it's array start
 void startAttribute(String name)
           
 void startObject()
          Will advance to next token and check if it's object start
 String toString()
           
 boolean wasFalse()
          Checks if 'false' value is at current position.
 boolean wasLastName(byte[] name)
           
 boolean wasLastName(String name)
           
 boolean wasNull()
          Checks if 'null' value is at current position.
 boolean wasTrue()
          Checks if 'true' value is at current position.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

context

public final TContext context

buffer

protected byte[] buffer

chars

protected char[] chars

errorInfo

protected final JsonReader.ErrorInfo errorInfo

doublePrecision

protected final JsonReader.DoublePrecision doublePrecision

doubleLengthLimit

protected final int doubleLengthLimit

unknownNumbers

protected final JsonReader.UnknownNumberParsing unknownNumbers

maxNumberDigits

protected final int maxNumberDigits
Constructor Detail

JsonReader

@Deprecated
public JsonReader(byte[] buffer,
                             @Nullable
                             TContext context)
Deprecated. 

Prefer creating reader through DslJson#newReader since it will pass several arguments (such as key/string value cache) First byte will not be read. It will allocate new char[64] for string buffer. Key and string vales cache will be null.

Parameters:
buffer - input JSON
context - context

JsonReader

@Deprecated
public JsonReader(byte[] buffer,
                             @Nullable
                             TContext context,
                             @Nullable
                             StringCache keyCache,
                             @Nullable
                             StringCache valuesCache)
Deprecated. 


JsonReader

@Deprecated
public JsonReader(byte[] buffer,
                             TContext context,
                             char[] tmp)
Deprecated. 


JsonReader

@Deprecated
public JsonReader(byte[] buffer,
                             int length,
                             TContext context)
Deprecated. 


JsonReader

@Deprecated
public JsonReader(byte[] buffer,
                             int length,
                             TContext context,
                             char[] tmp)
Deprecated. 


JsonReader

@Deprecated
public JsonReader(byte[] buffer,
                             int length,
                             @Nullable
                             TContext context,
                             char[] tmp,
                             @Nullable
                             StringCache keyCache,
                             @Nullable
                             StringCache valuesCache)
Deprecated. 

Method Detail

reset

@Deprecated
public final void reset(InputStream stream)
                 throws IOException
Deprecated. 

Will be removed. Exists only for backward compatibility

Parameters:
stream - process stream
Throws:
IOException - error reading from stream

process

public final JsonReader<TContext> process(@Nullable
                                          InputStream stream)
                                   throws IOException
Bind input stream for processing. Stream will be processed in byte[] chunks. If stream is null, reference to stream will be released.

Parameters:
stream - set input stream
Returns:
itself
Throws:
IOException - unable to read from stream

process

public final JsonReader<TContext> process(@Nullable
                                          byte[] newBuffer,
                                          int newLength)
Bind byte[] buffer for processing. If this method is used in combination with process(InputStream) this buffer will be used for processing chunks of stream. If null is sent for byte[] buffer, new length for valid input will be set for existing buffer.

Parameters:
newBuffer - new buffer to use for processing
newLength - length of buffer which can be used
Returns:
itself

length

public final int length()
Valid length of the input buffer.

Returns:
size of JSON input

toString

public String toString()
Overrides:
toString in class Object

read

public final byte read()
                throws IOException
Read next byte from the JSON input. If buffer has been read in full IOException will be thrown

Returns:
next byte
Throws:
IOException - when end of JSON input

last

public final byte last()
Which was last byte read from the JSON input. JsonReader doesn't allow to go back, but it remembers previously read byte

Returns:
which was the last byte read

positionDescription

public String positionDescription()

positionDescription

public String positionDescription(int offset)

newParseError

public final ParsingException newParseError(String description)

newParseError

public final ParsingException newParseError(String description,
                                            int positionOffset)

newParseErrorAt

public final ParsingException newParseErrorAt(String description,
                                              int positionOffset)

newParseErrorAt

public final ParsingException newParseErrorAt(String description,
                                              int positionOffset,
                                              Exception cause)

newParseErrorFormat

public final ParsingException newParseErrorFormat(String shortDescription,
                                                  int positionOffset,
                                                  String longDescriptionFormat,
                                                  Object... arguments)

newParseErrorWith

public final ParsingException newParseErrorWith(String description,
                                                @Nullable
                                                Object argument)

newParseErrorWith

public final ParsingException newParseErrorWith(String shortDescription,
                                                int positionOffset,
                                                String longDescriptionPrefix,
                                                String longDescriptionMessage,
                                                @Nullable
                                                Object argument,
                                                String longDescriptionSuffix)

getTokenStart

public final int getTokenStart()

getCurrentIndex

public final int getCurrentIndex()

readNumber

@Deprecated
public final char[] readNumber()
Deprecated. 

will be removed. not used anymore

Returns:
parsed chars from a number

scanNumber

public final int scanNumber()

readSimpleString

public final String readSimpleString()
                              throws ParsingException
Read simple ascii string. Will not use values cache to create instance.

Returns:
parsed string
Throws:
ParsingException - unable to parse string

readSimpleQuote

public final char[] readSimpleQuote()
                             throws ParsingException
Read simple "ascii string" into temporary buffer. String length must be obtained through getTokenStart and getCurrentToken

Returns:
temporary buffer
Throws:
ParsingException - unable to parse string

readString

public final String readString()
                        throws IOException
Read string from JSON input. If values cache is used, string will be looked up from the cache.

String value must start and end with a double quote (").

Returns:
parsed string
Throws:
IOException - error reading string input

appendString

public final StringBuilder appendString(StringBuilder builder)
                                 throws IOException
Throws:
IOException

appendString

public final StringBuffer appendString(StringBuffer buffer)
                                throws IOException
Throws:
IOException

getNextToken

public final byte getNextToken()
                        throws IOException
Read next token (byte) from input JSON. Whitespace will be skipped and next non-whitespace byte will be returned.

Returns:
next non-whitespace byte in the JSON input
Throws:
IOException - unable to get next byte (end of stream, ...)

positionInStream

public final long positionInStream()

positionInStream

public final long positionInStream(int offset)

fillName

public final int fillName()
                   throws IOException
Throws:
IOException

fillNameWeakHash

public final int fillNameWeakHash()
                           throws IOException
Throws:
IOException

calcHash

public final int calcHash()
                   throws IOException
Throws:
IOException

calcWeakHash

public final int calcWeakHash()
                       throws IOException
Throws:
IOException

getLastHash

public final int getLastHash()

wasLastName

public final boolean wasLastName(String name)

wasLastName

public final boolean wasLastName(byte[] name)

getLastName

public final String getLastName()
                         throws IOException
Throws:
IOException

skip

public final byte skip()
                throws IOException
Skip to next non-whitespace token (byte) Will not allocate memory while skipping over JSON input.

Returns:
next non-whitespace byte
Throws:
IOException - unable to read next byte (end of stream, invalid JSON, ...)

readNext

@Deprecated
public String readNext()
                throws IOException
Deprecated. 

will be removed

Returns:
not used anymore
Throws:
IOException - throws if invalid JSON detected

readBase64

public final byte[] readBase64()
                        throws IOException
Throws:
IOException

readKey

public final String readKey()
                     throws IOException
Read key value of JSON input. If key cache is used, it will be looked up from there.

Returns:
parsed key value
Throws:
IOException - unable to parse string input

wasNull

public final boolean wasNull()
                      throws ParsingException
Checks if 'null' value is at current position. This means last read byte was 'n' and 'ull' are next three bytes. If last byte was n but next three are not 'ull' it will throw since that is not a valid JSON construct.

Returns:
true if 'null' value is at current position
Throws:
ParsingException - invalid 'null' value detected

wasTrue

public final boolean wasTrue()
                      throws ParsingException
Checks if 'true' value is at current position. This means last read byte was 't' and 'rue' are next three bytes. If last byte was t but next three are not 'rue' it will throw since that is not a valid JSON construct.

Returns:
true if 'true' value is at current position
Throws:
ParsingException - invalid 'true' value detected

wasFalse

public final boolean wasFalse()
                       throws ParsingException
Checks if 'false' value is at current position. This means last read byte was 'f' and 'alse' are next four bytes. If last byte was f but next four are not 'alse' it will throw since that is not a valid JSON construct.

Returns:
true if 'false' value is at current position
Throws:
ParsingException - invalid 'false' value detected

comma

public final void comma()
                 throws IOException
Will advance to next token and check if it's comma

Throws:
IOException - it's not comma

semicolon

public final void semicolon()
                     throws IOException
Will advance to next token and check if it's semicolon

Throws:
IOException - it's not semicolon

startArray

public final void startArray()
                      throws IOException
Will advance to next token and check if it's array start

Throws:
IOException - it's not array start

endArray

public final void endArray()
                    throws IOException
Will advance to next token and check if it's array end

Throws:
IOException - it's not array end

startObject

public final void startObject()
                       throws IOException
Will advance to next token and check if it's object start

Throws:
IOException - it's not object start

endObject

public final void endObject()
                     throws IOException
Will advance to next token and check it it's object end

Throws:
IOException - it's not object end

startAttribute

public final void startAttribute(String name)
                          throws IOException
Throws:
IOException

checkArrayEnd

public final void checkArrayEnd()
                         throws IOException
Check if the last read token is an array end

Throws:
IOException - it's not array end

checkObjectEnd

public final void checkObjectEnd()
                          throws IOException
Check if the last read token is an object end

Throws:
IOException - it's not object end

next

@Nullable
public final <T> T next(Class<T> manifest)
             throws IOException
Will advance to next token and read the JSON into specified type

Type Parameters:
T - type
Parameters:
manifest - type to read into
Returns:
new instance from input JSON
Throws:
IOException - unable to process JSON

next

@Nullable
public final <T> T next(JsonReader.ReadObject<T> reader)
             throws IOException
Will advance to next token and read the JSON into specified type

Type Parameters:
T - type
Parameters:
reader - reader to use
Returns:
new instance from input JSON
Throws:
IOException - unable to process JSON

next

@Nullable
public final <T> T next(Class<T> manifest,
                                 T instance)
             throws IOException
Will advance to next token and bind the JSON to provided instance

Type Parameters:
T - type
Parameters:
manifest - type to read into
instance - instance to bind
Returns:
bound instance
Throws:
IOException - unable to process JSON

next

@Nullable
public final <T> T next(JsonReader.BindObject<T> binder,
                                 T instance)
             throws IOException
Will advance to next token and bind the JSON to provided instance

Type Parameters:
T - type
Parameters:
binder - binder to use
instance - instance to bind
Returns:
bound instance
Throws:
IOException - unable to process JSON

readCollection

@Nullable
public final <T> ArrayList<T> readCollection(JsonReader.ReadObject<T> readObject)
                                  throws IOException
Throws:
IOException

readSet

@Nullable
public final <T> LinkedHashSet<T> readSet(JsonReader.ReadObject<T> readObject)
                               throws IOException
Throws:
IOException

readMap

@Nullable
public final <K,V> LinkedHashMap<K,V> readMap(JsonReader.ReadObject<K> readKey,
                                                       JsonReader.ReadObject<V> readValue)
                                 throws IOException
Throws:
IOException

readArray

@Nullable
public final <T> T[] readArray(JsonReader.ReadObject<T> readObject,
                                        T[] emptyArray)
                    throws IOException
Throws:
IOException

deserializeCollection

public final <T,S extends T> ArrayList<T> deserializeCollection(JsonReader.ReadObject<S> readObject)
                                         throws IOException
Throws:
IOException

deserializeCollection

public final <T,S extends T> void deserializeCollection(JsonReader.ReadObject<S> readObject,
                                                        Collection<T> res)
                                 throws IOException
Throws:
IOException

deserializeNullableCollection

public final <T,S extends T> ArrayList<T> deserializeNullableCollection(JsonReader.ReadObject<S> readObject)
                                                 throws IOException
Throws:
IOException

deserializeNullableCollection

public final <T,S extends T> void deserializeNullableCollection(JsonReader.ReadObject<S> readObject,
                                                                Collection<T> res)
                                         throws IOException
Throws:
IOException

deserializeCollection

public final <T extends JsonObject> ArrayList<T> deserializeCollection(JsonReader.ReadJsonObject<T> readObject)
                                                            throws IOException
Throws:
IOException

deserializeCollection

public final <T extends JsonObject> void deserializeCollection(JsonReader.ReadJsonObject<T> readObject,
                                                               Collection<T> res)
                                 throws IOException
Throws:
IOException

deserializeNullableCollection

public final <T extends JsonObject> ArrayList<T> deserializeNullableCollection(JsonReader.ReadJsonObject<T> readObject)
                                                                    throws IOException
Throws:
IOException

deserializeNullableCollection

public final <T extends JsonObject> void deserializeNullableCollection(JsonReader.ReadJsonObject<T> readObject,
                                                                       Collection<T> res)
                                         throws IOException
Throws:
IOException

iterateOver

public final <T> Iterator<T> iterateOver(JsonReader.ReadObject<T> reader)

iterateOver

public final <T extends JsonObject> Iterator<T> iterateOver(JsonReader.ReadJsonObject<T> reader)


Copyright © 2019. All rights reserved.