Package com.dslplatform.json
Class JsonReader<TContext>
java.lang.Object
com.dslplatform.json.JsonReader<TContext>
- Type Parameters:
TContext- context passed to deserialized object instances
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
Nested ClassesModifier and TypeClassDescriptionstatic interfaceExisting instances can be provided as target for deserialization.static enumprivate static classstatic enumstatic interfaceJsonReader.ReadJsonObject<T extends JsonObject>static interfaceCustom objects can be deserialized based on the implementation specified through this interface.static enumprivate static classJsonReader.WithObjectReader<T extends JsonObject>private static class -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected byte[]private intprotected char[]final TContextprivate intprivate longprotected final intprotected final JsonReader.DoublePrecisionprivate static final EOFExceptionprivate final StringBuilderprivate final Formatterprotected final JsonReader.ErrorInfoprivate final StringCacheprivate byteprivate intprivate intprotected final intprivate final intprivate intprivate final byte[]private final intprivate intprivate InputStreamprivate final char[]private intprivate final TypeLookupprotected final JsonReader.UnknownNumberParsingprivate final StringCacheprivate static final boolean[] -
Constructor Summary
ConstructorsModifierConstructorDescription(package private)JsonReader(byte[] buffer, int length, TContext context, char[] tmp, StringCache keyCache, StringCache valuesCache, TypeLookup typeLookup, JsonReader.ErrorInfo errorInfo, JsonReader.DoublePrecision doublePrecision, JsonReader.UnknownNumberParsing unknownNumbers, int maxNumberDigits, int maxStringBuffer) privateJsonReader(char[] tmp, byte[] buffer, int length, TContext context, StringCache keyCache, StringCache valuesCache, TypeLookup typeLookup, JsonReader.ErrorInfo errorInfo, JsonReader.DoublePrecision doublePrecision, JsonReader.UnknownNumberParsing unknownNumbers, int maxNumberDigits, int maxStringBuffer) -
Method Summary
Modifier and TypeMethodDescription(package private) final booleanallWhitespace(int start, int end) final StringBufferappendString(StringBuffer buffer) final StringBuilderappendString(StringBuilder builder) final intcalcHash()private intcalcHashAndCopyName(long hash, int ci) final intprivate intcalcWeakHashAndCopyName(int hash, int ci) final voidCheck if the last read token is an array endfinal voidCheck if the last read token is an object endfinal voidcomma()Will advance to next token and check if it's commafinal <T extends JsonObject>
ArrayList<T> deserializeCollection(JsonReader.ReadJsonObject<T> readObject) final <T extends JsonObject>
voiddeserializeCollection(JsonReader.ReadJsonObject<T> readObject, Collection<T> res) final <T,S extends T>
ArrayList<T> deserializeCollection(JsonReader.ReadObject<S> readObject) final <T,S extends T>
voiddeserializeCollection(JsonReader.ReadObject<S> readObject, Collection<T> res) final <T extends JsonObject>
ArrayList<T> deserializeNullableCollection(JsonReader.ReadJsonObject<T> readObject) final <T extends JsonObject>
voiddeserializeNullableCollection(JsonReader.ReadJsonObject<T> readObject, Collection<T> res) final <T,S extends T>
ArrayList<T> deserializeNullableCollection(JsonReader.ReadObject<S> readObject) final <T,S extends T>
voiddeserializeNullableCollection(JsonReader.ReadObject<S> readObject, Collection<T> res) final voidendArray()Will advance to next token and check if it's array endfinal voidWill advance to next token and check it it's object endfinal intfillName()final int(package private) final intfindNonWhitespace(int end) final intfinal intfinal Stringfinal byteRead next token (byte) from input JSON.final intprivate inthexToInt(byte value) (package private) final booleanfinal <T extends JsonObject>
Iterator<T> iterateOver(JsonReader.ReadJsonObject<T> reader) final <T> Iterator<T> iterateOver(JsonReader.ReadObject<T> reader) final bytelast()Which was last byte read from the JSON input.final intlength()Valid length of the input buffer.final ParsingExceptionnewParseError(String description) final ParsingExceptionnewParseError(String description, int positionOffset) final ParsingExceptionnewParseErrorAt(String description, int positionOffset) final ParsingExceptionnewParseErrorAt(String description, int positionOffset, Exception cause) final ParsingExceptionnewParseErrorFormat(String shortDescription, int positionOffset, String longDescriptionFormat, Object... arguments) final ParsingExceptionnewParseErrorWith(String shortDescription, int positionOffset, String longDescriptionPrefix, String longDescriptionMessage, Object argument, String longDescriptionSuffix) final ParsingExceptionnewParseErrorWith(String description, Object argument) final <T> Tnext(JsonReader.BindObject<T> binder, T instance) Will advance to next token and bind the JSON to provided instancefinal <T> Tnext(JsonReader.ReadObject<T> reader) Will advance to next token and read the JSON into specified typefinal <T> TWill advance to next token and read the JSON into specified typefinal <T> TWill advance to next token and bind the JSON to provided instance(package private) final intpositionDescription(int offset) private voidpositionDescription(int offset, StringBuilder error) final longfinal longpositionInStream(int offset) (package private) final char[]prepareBuffer(int start, int len) private intfinal JsonReader<TContext> process(byte[] newBuffer, int newLength) Bind byte[] buffer for processing.final JsonReader<TContext> process(InputStream stream) Bind input stream for processing.final byteread()Read next byte from the JSON input.final <T> T[]readArray(JsonReader.ReadObject<T> readObject, T[] emptyArray) final byte[]final <T> ArrayList<T> readCollection(JsonReader.ReadObject<T> readObject) private static intreadFully(byte[] buffer, InputStream stream, int offset) final StringreadKey()Read key value of JSON input.final <K,V> LinkedHashMap <K, V> readMap(JsonReader.ReadObject<K> readKey, JsonReader.ReadObject<V> readValue) readNext()Deprecated.private Objectfinal char[]Deprecated.final <T> LinkedHashSet<T> readSet(JsonReader.ReadObject<T> readObject) final char[]Read simple "ascii string" into temporary buffer.final StringRead simple ascii string.final StringRead string from JSON input.(package private) final voidreset()Reset reader after processing input It will release reference to provided byte[] or InputStream input(package private) final voidreset(int size) Deprecated.final voidreset(InputStream stream) Deprecated.final intfinal voidWill advance to next token and check if it's semicolonfinal byteskip()Skip to next non-whitespace token (byte) Will not allocate memory while skipping over JSON input.private bytefinal voidWill advance to next token and check if it's array startfinal voidstartAttribute(String name) final voidWill advance to next token and check if it's object starttoString()final booleanwasFalse()Checks if 'false' value is at current position.final booleanwasLastName(byte[] name) final booleanwasLastName(String name) final booleanwasNull()Checks if 'null' value is at current position.final booleanwasTrue()Checks if 'true' value is at current position.private boolean(package private) boolean
-
Field Details
-
WHITESPACE
private static final boolean[] WHITESPACE -
tokenStart
private int tokenStart -
nameEnd
private int nameEnd -
currentIndex
private int currentIndex -
currentPosition
private long currentPosition -
last
private byte last -
length
private int length -
tmp
private final char[] tmp -
context
-
buffer
protected byte[] buffer -
chars
protected char[] chars -
stream
-
readLimit
private int readLimit -
bufferLenWithExtraSpace
private int bufferLenWithExtraSpace -
keyCache
-
valuesCache
-
typeLookup
-
originalBuffer
private final byte[] originalBuffer -
originalBufferLenWithExtraSpace
private final int originalBufferLenWithExtraSpace -
errorInfo
-
doublePrecision
-
doubleLengthLimit
protected final int doubleLengthLimit -
unknownNumbers
-
maxNumberDigits
protected final int maxNumberDigits -
maxStringBuffer
private final int maxStringBuffer -
eof
-
error
-
errorFormatter
-
lastNameLen
private int lastNameLen
-
-
Constructor Details
-
JsonReader
private JsonReader(char[] tmp, byte[] buffer, int length, @Nullable TContext context, @Nullable StringCache keyCache, @Nullable StringCache valuesCache, @Nullable TypeLookup typeLookup, JsonReader.ErrorInfo errorInfo, JsonReader.DoublePrecision doublePrecision, JsonReader.UnknownNumberParsing unknownNumbers, int maxNumberDigits, int maxStringBuffer) -
JsonReader
JsonReader(byte[] buffer, int length, @Nullable TContext context, char[] tmp, @Nullable StringCache keyCache, @Nullable StringCache valuesCache, @Nullable TypeLookup typeLookup, JsonReader.ErrorInfo errorInfo, JsonReader.DoublePrecision doublePrecision, JsonReader.UnknownNumberParsing unknownNumbers, int maxNumberDigits, int maxStringBuffer)
-
-
Method Details
-
reset
Deprecated.Will be removed. Exists only for backward compatibility- Parameters:
stream- process stream- Throws:
IOException- error reading from stream
-
reset
Deprecated.Will be removed. Exists only for backward compatibility- Parameters:
size- size of byte[] input to use
-
reset
final void reset()Reset reader after processing input It will release reference to provided byte[] or InputStream input -
process
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
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 processingnewLength- 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
-
readFully
- Throws:
IOException
-
withStackTrace
boolean withStackTrace() -
read
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
-
prepareNextBlock
- Throws:
IOException
-
isEndOfStream
- Throws:
IOException
-
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
-
positionDescription
-
positionDescription
-
newParseError
-
newParseError
-
newParseErrorAt
-
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
-
newParseErrorWith
-
getTokenStart
public final int getTokenStart() -
getCurrentIndex
public final int getCurrentIndex() -
readNumber
Deprecated.will be removed. not used anymore- Returns:
- parsed chars from a number
-
scanNumber
public final int scanNumber() -
prepareBuffer
- Throws:
ParsingException
-
allWhitespace
final boolean allWhitespace(int start, int end) -
findNonWhitespace
final int findNonWhitespace(int end) -
readSimpleString
Read simple ascii string. Will not use values cache to create instance.- Returns:
- parsed string
- Throws:
ParsingException- unable to parse string
-
readSimpleQuote
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
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
- Throws:
IOException
-
appendString
- Throws:
IOException
-
parseString
- Throws:
IOException
-
hexToInt
- Throws:
ParsingException
-
wasWhiteSpace
private boolean wasWhiteSpace() -
getNextToken
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
- Throws:
IOException
-
fillNameWeakHash
- Throws:
IOException
-
calcHash
- Throws:
IOException
-
calcWeakHash
- Throws:
IOException
-
getLastHash
public final int getLastHash() -
calcHashAndCopyName
- Throws:
IOException
-
calcWeakHashAndCopyName
- Throws:
IOException
-
wasLastName
-
wasLastName
public final boolean wasLastName(byte[] name) -
getLastName
- Throws:
IOException
-
skipString
- Throws:
IOException
-
skip
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.will be removed- Returns:
- not used anymore
- Throws:
IOException- throws if invalid JSON detected
-
readBase64
- Throws:
IOException
-
readKey
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
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
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
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
Will advance to next token and check if it's comma- Throws:
IOException- it's not comma
-
semicolon
Will advance to next token and check if it's semicolon- Throws:
IOException- it's not semicolon
-
startArray
Will advance to next token and check if it's array start- Throws:
IOException- it's not array start
-
endArray
Will advance to next token and check if it's array end- Throws:
IOException- it's not array end
-
startObject
Will advance to next token and check if it's object start- Throws:
IOException- it's not object start
-
endObject
Will advance to next token and check it it's object end- Throws:
IOException- it's not object end
-
startAttribute
- Throws:
IOException
-
checkArrayEnd
Check if the last read token is an array end- Throws:
IOException- it's not array end
-
checkObjectEnd
Check if the last read token is an object end- Throws:
IOException- it's not object end
-
readNull
- Throws:
IOException
-
next
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
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
Will advance to next token and bind the JSON to provided instance- Type Parameters:
T- type- Parameters:
manifest- type to read intoinstance- instance to bind- Returns:
- bound instance
- Throws:
IOException- unable to process JSON
-
next
Will advance to next token and bind the JSON to provided instance- Type Parameters:
T- type- Parameters:
binder- binder to useinstance- 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
-
iterateOver
-