Class JsonParser
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
-
Nested Class Summary
Nested Classes -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected Stringprotected Map<Path, ValueConverter> private static final char[]protected CharsInputprivate final JsonAnnotationManagerstatic final StringMap keys.protected booleanprotected booleanprotected MapToBeanprivate static final BigIntegerprivate static final BigIntegerprivate static final char[]private booleanprotected Pathprotected Classprivate static final char[]protected char[]protected intprotected intprivate static final char[]protected booleanstatic final StringArray or map values.Fields inherited from class JsonParserBase
ARRAYLIST_SUPPLIER, classnameWhitelist, HASHMAP_SUPPLIER, LAZYLIST_SUPPLIER, LAZYMAP_SUPPLIER, listSupplier, mapSupplier, strictTypes -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate <T> T_parse(CharsInput charsInput) Removes the whitelist of allowed classes.allowClass(String classPattern) Adds awildcardpattern for white-listing classes.protected voidconsume(char c) Consumes char at current position.protected charconsumeOneOf(char c1, char c2) Consumes one of the allowed char at current position.static JsonParsercreate()Static ctor.static JsonParserCreates a lazy implementation of the JSON parser.protected voidGrows text array whentext.length == textLen.protected voidGrows empty text array.private static booleanisGreaterThanLong(BigInteger bigInteger) lazy(boolean lazy) Defines how JSON parser works.protected ValueConverterLookups for value converter for current path.looseMode(boolean looseMode) Enables 'loose' mode for parsing.Maps a class to JSONs root.Maps a class to given path.protected final booleanmatch(char[] target) Matches char buffer with content on given location.private boolean<T> Tparse(char[] input) Parses input JSON char array.<T> TParses input JSON as given type.<T> TParses input JSON string.<T> TParses input JSON as given type.protected ObjectparseArrayContent(Class targetType, Class componentType) Parses arrays, once when open bracket has been consumed.parseAsJsonArray(String input) Parses input JSON toJsonArray, special case of parsing.parseAsJsonObject(String input) Parses input JSON toJsonObject, special case ofparse(String, Class).<T> List<T> parseAsList(String string, Class<T> componentType) Parses input JSON to a list with specified component type.<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.protected NumberParses JSON numbers.protected ObjectparseObjectContent(Class targetType, Class valueKeyType, Class valueType) Parses object, once when open bracket has been consumed.protected StringParses a string.protected StringparseStringContent(char quote) Parses string content, once when starting quote has been consumed.protected charParses 4 characters and returns unicode character.protected StringParses un-quoted string content.protected ObjectparseValue(Class targetType, Class keyType, Class componentType) Parses a JSON value.protected ClassreplaceWithMappedTypeForPath(Class target) Replaces type with mapped type for current path.protected voidreset()Resets JSON parser, so it can be reused.private ObjectresolveLazyValue(Object value) Resolves lazy value during the parsing runtime.setClassMetadataName(String name) Sets local class meta-data name.private voidSkips over complete object.protected final voidSkips whitespaces.strictTypes(boolean strictTypes) Defines if type conversion is strict.protected voidsyntaxError(String message, Throwable cause) ThrowsJsonExceptionindicating a syntax error.Enables usage of additional paths.withClassMetadata(boolean useMetadata) Sets usage of default class meta-data name.withValueConverter(String path, ValueConverter valueConverter) DefinesValueConverterto use on given path.Methods inherited from class JsonParserBase
convertType, createMapToBean, injectValueIntoObject, newArrayInstance, newObjectInstance
-
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
-
VALUES
-
input
-
total
protected int total -
path
-
useAltPaths
protected boolean useAltPaths -
lazy
protected boolean lazy -
looseMode
protected boolean looseMode -
rootType
-
mapToBean
-
notFirstObject
private boolean notFirstObject -
jsonAnnotationManager
-
mappings
-
convs
-
classMetadataName
-
text
protected char[] text -
textLen
protected int textLen -
UNQUOTED_DELIMETERS
private static final char[] UNQUOTED_DELIMETERS -
MAX_LONG
-
MIN_LONG
-
-
Constructor Details
-
JsonParser
public JsonParser()
-
-
Method Details
-
create
Static ctor. -
createLazyOne
Creates a lazy implementation of the JSON parser. -
reset
protected void reset()Resets JSON parser, so it can be reused. -
useAltPaths
Enables usage of additional paths. -
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
Defines if type conversion is strict. If not, all exceptions will be caught and replaced withnull. -
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
Maps a class to JSONs root. -
map
Maps a class to given path. For arrays, appendvaluesto the path to specify component type (if not specified by generics). -
replaceWithMappedTypeForPath
-
withValueConverter
DefinesValueConverterto use on given path. -
lookupValueConverter
Lookups for value converter for current path. -
setClassMetadataName
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
Sets usage of default class meta-data name. Using it may introduce a security hole, seesetClassMetadataName(String)for more details.- See Also:
-
allowClass
Adds awildcardpattern for white-listing classes.- See Also:
-
allowAllClasses
-
parse
-
parseAsJsonObject
Parses input JSON toJsonObject, special case ofparse(String, Class). -
parseAsJsonArray
-
parseAsList
-
parseAsMap
-
parse
Parses input JSON string. -
parse
Parses input JSON as given type. -
parse
public <T> T parse(char[] input) Parses input JSON char array. -
_parse
-
parseValue
-
resolveLazyValue
-
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
Parses a string. -
parseStringContent
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 whentext.length == textLen. -
parseUnicode
protected char parseUnicode()Parses 4 characters and returns unicode character. -
parseUnquotedStringContent
Parses un-quoted string content. -
parseNumber
Parses JSON numbers. -
isGreaterThanLong
-
parseArrayContent
-
parseObjectContent
-
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, return0. 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
ThrowsJsonExceptionindicating a syntax error.
-