Class JsonReader<TContext>

java.lang.Object
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.

  • 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

      public final TContext context
    • buffer

      protected byte[] buffer
    • chars

      protected char[] chars
    • stream

      private InputStream stream
    • readLimit

      private int readLimit
    • bufferLenWithExtraSpace

      private int bufferLenWithExtraSpace
    • keyCache

      private final StringCache keyCache
    • valuesCache

      private final StringCache valuesCache
    • typeLookup

      private final TypeLookup typeLookup
    • originalBuffer

      private final byte[] originalBuffer
    • originalBufferLenWithExtraSpace

      private final int originalBufferLenWithExtraSpace
    • 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
    • maxStringBuffer

      private final int maxStringBuffer
    • eof

      private static final EOFException eof
    • error

      private final StringBuilder error
    • errorFormatter

      private final Formatter errorFormatter
    • lastNameLen

      private int lastNameLen
  • Constructor Details

  • Method Details

    • 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
    • reset

      @Deprecated final void reset(int size)
      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

      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
    • readFully

      private static int readFully(byte[] buffer, InputStream stream, int offset) throws IOException
      Throws:
      IOException
    • withStackTrace

      boolean withStackTrace()
    • 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
    • prepareNextBlock

      private int prepareNextBlock() throws IOException
      Throws:
      IOException
    • isEndOfStream

      final boolean isEndOfStream() throws IOException
      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

      public String positionDescription()
    • positionDescription

      public String positionDescription(int offset)
    • positionDescription

      private void positionDescription(int offset, StringBuilder error)
    • 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()
    • prepareBuffer

      final char[] prepareBuffer(int start, int len) throws ParsingException
      Throws:
      ParsingException
    • allWhitespace

      final boolean allWhitespace(int start, int end)
    • findNonWhitespace

      final int findNonWhitespace(int end)
    • 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
    • parseString

      final int parseString() throws IOException
      Throws:
      IOException
    • hexToInt

      private int hexToInt(byte value) throws ParsingException
      Throws:
      ParsingException
    • wasWhiteSpace

      private boolean wasWhiteSpace()
    • 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()
    • calcHashAndCopyName

      private int calcHashAndCopyName(long hash, int ci) throws IOException
      Throws:
      IOException
    • calcWeakHashAndCopyName

      private int calcWeakHashAndCopyName(int hash, int ci) throws IOException
      Throws:
      IOException
    • wasLastName

      public final boolean wasLastName(String name)
    • wasLastName

      public final boolean wasLastName(byte[] name)
    • getLastName

      public final String getLastName() throws IOException
      Throws:
      IOException
    • skipString

      private byte skipString() 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
    • readNull

      @Nullable private Object readNull(Class<?> manifest) throws IOException
      Throws:
      IOException
    • 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)