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 Detail

      • VALUES

        public static final java.lang.String VALUES
        Array or map values.
        See Also:
        Constant Field Values
      • input

        protected jodd.json.CharsInput input
      • total

        protected int total
      • path

        protected Path path
      • useAltPaths

        protected boolean useAltPaths
      • lazy

        protected boolean lazy
      • looseMode

        protected boolean looseMode
      • rootType

        protected java.lang.Class rootType
      • mappings

        protected java.util.Map<Path,​java.lang.Class> mappings
      • classMetadataName

        protected java.lang.String classMetadataName
      • text

        protected char[] text
      • textLen

        protected int textLen
    • Constructor Detail

      • JsonParser

        public JsonParser()
    • Method Detail

      • 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​(java.lang.Class target)
        Maps a class to JSONs root.
      • map

        public JsonParser map​(java.lang.String path,
                              java.lang.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 java.lang.Class replaceWithMappedTypeForPath​(java.lang.Class target)
        Replaces type with mapped type for current path.
      • lookupValueConverter

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

        public JsonParser setClassMetadataName​(java.lang.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.

      • parse

        public <T> T parse​(java.lang.String input,
                           java.lang.Class<T> targetType)
        Parses input JSON as given type.
      • parseAsJsonArray

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

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

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

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

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

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

        protected java.lang.Object parseValue​(java.lang.Class targetType,
                                              java.lang.Class keyType,
                                              java.lang.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
      • parseString

        protected java.lang.String parseString()
        Parses a string.
      • parseStringContent

        protected java.lang.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 java.lang.String parseUnquotedStringContent()
        Parses un-quoted string content.
      • parseNumber

        protected java.lang.Number parseNumber()
        Parses JSON numbers.
      • parseArrayContent

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

        protected java.lang.Object parseObjectContent​(java.lang.Class targetType,
                                                      java.lang.Class valueKeyType,
                                                      java.lang.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​(java.lang.String message,
                                   java.lang.Throwable cause)
        Throws JsonException indicating a syntax error.