Package jodd.json

Class JsonParser


public class JsonParser extends JsonParserBase
Simple, developer-friendly JSON parser. It focuses on easy usage and type mappings. Uses Jodd's type converters, so it is natural companion for Jodd projects.

This JSON parser also works in lazy(boolean) mode. This mode is for top performance usage: parsing is done very, very lazy. While you can use all the mappings and other tools, for best performance the lazy mode should be used only with maps and lists (no special mappings). Also, the performance has it's price: more memory consumption, because the original input is hold until the result is in use.

See: http://www.ietf.org/rfc/rfc4627.txt

  • Field Details

    • T_RUE

      private static final char[] T_RUE
    • F_ALSE

      private static final char[] F_ALSE
    • N_ULL

      private static final char[] N_ULL
    • KEYS

      public static final String KEYS
      Map keys.
      See Also:
    • VALUES

      public static final String VALUES
      Array or map values.
      See Also:
    • input

      protected CharsInput input
    • total

      protected int total
    • path

      protected Path path
    • useAltPaths

      protected boolean useAltPaths
    • lazy

      protected boolean lazy
    • looseMode

      protected boolean looseMode
    • rootType

      protected Class rootType
    • mapToBean

      protected MapToBean mapToBean
    • notFirstObject

      private boolean notFirstObject
    • jsonAnnotationManager

      private final JsonAnnotationManager jsonAnnotationManager
    • mappings

      protected Map<Path,Class> mappings
    • convs

      protected Map<Path,ValueConverter> convs
    • classMetadataName

      protected String classMetadataName
    • text

      protected char[] text
    • textLen

      protected int textLen
    • UNQUOTED_DELIMETERS

      private static final char[] UNQUOTED_DELIMETERS
    • MAX_LONG

      private static final BigInteger MAX_LONG
    • MIN_LONG

      private static final BigInteger MIN_LONG
  • Constructor Details

    • JsonParser

      public JsonParser()
  • Method Details

    • create

      public static JsonParser create()
      Static ctor.
    • createLazyOne

      public static JsonParser createLazyOne()
      Creates a lazy implementation of the JSON parser.
    • reset

      protected void reset()
      Resets JSON parser, so it can be reused.
    • useAltPaths

      public JsonParser useAltPaths()
      Enables usage of additional paths.
    • looseMode

      public JsonParser looseMode(boolean looseMode)
      Enables 'loose' mode for parsing. When 'loose' mode is enabled, JSON parsers swallows also invalid JSONs:
      • invalid escape character sequence is simply added to the output
      • strings can be quoted with single-quotes
      • strings can be unquoted, but may not contain escapes
    • strictTypes

      public JsonParser strictTypes(boolean strictTypes)
      Defines if type conversion is strict. If not, all exceptions will be caught and replaced with null.
    • lazy

      public JsonParser lazy(boolean lazy)
      Defines how JSON parser works. In non-lazy mode, the whole JSON is parsed as it is. In the lazy mode, not everything is parsed, but some things are left lazy. This way we gain performance, especially on partial usage of the whole JSON. However, be aware that parser holds the input memory until the returned objects are disposed.
    • map

      public JsonParser map(Class target)
      Maps a class to JSONs root.
    • map

      public JsonParser map(String path, Class target)
      Maps a class to given path. For arrays, append values to the path to specify component type (if not specified by generics).
    • replaceWithMappedTypeForPath

      protected Class replaceWithMappedTypeForPath(Class target)
      Replaces type with mapped type for current path.
    • withValueConverter

      public JsonParser withValueConverter(String path, ValueConverter valueConverter)
      Defines ValueConverter to use on given path.
    • lookupValueConverter

      protected ValueConverter lookupValueConverter()
      Lookups for value converter for current path.
    • setClassMetadataName

      public JsonParser setClassMetadataName(String name)
      Sets local class meta-data name.

      Note that by using the class meta-data name you may expose a security hole in case untrusted source manages to specify a class that is accessible through class loader and exposes set of methods and/or fields, access of which opens an actual security hole. Such classes are known as “deserialization gadget”s. Because of this, use of "default typing" is not encouraged in general, and in particular is recommended against if the source of content is not trusted. Conversely, default typing may be used for processing content in cases where both ends (sender and receiver) are controlled by same entity.

    • withClassMetadata

      public JsonParser withClassMetadata(boolean useMetadata)
      Sets usage of default class meta-data name. Using it may introduce a security hole, see setClassMetadataName(String) for more details.
      See Also:
    • allowClass

      public JsonParser allowClass(String classPattern)
      Adds a wildcard pattern for white-listing classes.
      See Also:
    • allowAllClasses

      public JsonParser allowAllClasses()
      Removes the whitelist of allowed classes.
      See Also:
    • parse

      public <T> T parse(String input, Class<T> targetType)
      Parses input JSON as given type.
    • parseAsJsonObject

      public JsonObject parseAsJsonObject(String input)
      Parses input JSON to JsonObject, special case of parse(String, Class).
    • parseAsJsonArray

      public JsonArray parseAsJsonArray(String input)
      Parses input JSON to JsonArray, special case of parsing.
    • parseAsList

      public <T> List<T> parseAsList(String string, Class<T> componentType)
      Parses input JSON to a list with specified component type.
    • parseAsMap

      public <K, V> Map<K,V> parseAsMap(String string, Class<K> keyType, Class<V> valueType)
      Parses input JSON to a list with specified key and value types.
    • parse

      public <T> T parse(String input)
      Parses input JSON string.
    • parse

      public <T> T parse(char[] input, Class<T> targetType)
      Parses input JSON as given type.
    • parse

      public <T> T parse(char[] input)
      Parses input JSON char array.
    • _parse

      private <T> T _parse(CharsInput charsInput)
    • parseValue

      protected Object parseValue(Class targetType, Class keyType, Class componentType)
      Parses a JSON value.
      Parameters:
      targetType - target type to convert, may be null
      componentType - component type for maps and arrays, may be null
    • resolveLazyValue

      private Object resolveLazyValue(Object value)
      Resolves lazy value during the parsing runtime.
    • skipObject

      private void skipObject()
      Skips over complete object. It is not parsed, just skipped. It will be parsed later, but only if required.
    • notPrecededByEvenNumberOfBackslashes

      private boolean notPrecededByEvenNumberOfBackslashes()
    • parseString

      protected String parseString()
      Parses a string.
    • parseStringContent

      protected String parseStringContent(char quote)
      Parses string content, once when starting quote has been consumed.
    • growEmpty

      protected void growEmpty()
      Grows empty text array.
    • growAndCopy

      protected void growAndCopy()
      Grows text array when text.length == textLen.
    • parseUnicode

      protected char parseUnicode()
      Parses 4 characters and returns unicode character.
    • parseUnquotedStringContent

      protected String parseUnquotedStringContent()
      Parses un-quoted string content.
    • parseNumber

      protected Number parseNumber()
      Parses JSON numbers.
    • isGreaterThanLong

      private static boolean isGreaterThanLong(BigInteger bigInteger)
    • parseArrayContent

      protected Object parseArrayContent(Class targetType, Class componentType)
      Parses arrays, once when open bracket has been consumed.
    • parseObjectContent

      protected Object parseObjectContent(Class targetType, Class valueKeyType, Class valueType)
      Parses object, once when open bracket has been consumed.
    • consume

      protected void consume(char c)
      Consumes char at current position. If char is different, throws the exception.
    • consumeOneOf

      protected char consumeOneOf(char c1, char c2)
      Consumes one of the allowed char at current position. If char is different, return 0. If matched, returns matched char.
    • skipWhiteSpaces

      protected final void skipWhiteSpaces()
      Skips whitespaces. For the simplification, whitespaces are considered any characters less or equal to 32 (space).
    • match

      protected final boolean match(char[] target)
      Matches char buffer with content on given location.
    • syntaxError

      protected void syntaxError(String message, Throwable cause)
      Throws JsonException indicating a syntax error.