Class JsonParser
- java.lang.Object
-
- jodd.json.JsonParserBase
-
- jodd.json.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
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classJsonParser.Defaults
-
Field Summary
Fields Modifier and Type Field Description protected java.lang.StringclassMetadataNameprotected java.util.Map<Path,ValueConverter>convsprotected jodd.json.CharsInputinputstatic java.lang.StringKEYSMap keys.protected booleanlazyprotected booleanlooseModeprotected java.util.Map<Path,java.lang.Class>mappingsprotected MapToBeanmapToBeanprotected Pathpathprotected java.lang.ClassrootTypeprotected char[]textprotected inttextLenprotected inttotalprotected booleanuseAltPathsstatic java.lang.StringVALUESArray or map values.-
Fields inherited from class jodd.json.JsonParserBase
ARRAYLIST_SUPPLIER, classnameWhitelist, HASHMAP_SUPPLIER, LAZYLIST_SUPPLIER, LAZYMAP_SUPPLIER, listSupplier, mapSupplier, strictTypes
-
-
Constructor Summary
Constructors Constructor Description JsonParser()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description JsonParserallowAllClasses()Removes the whitelist of allowed classes.JsonParserallowClass(java.lang.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 JsonParsercreateLazyOne()Creates a lazy implementation of the JSON parser.protected voidgrowAndCopy()Grows text array whentext.length == textLen.protected voidgrowEmpty()Grows empty text array.JsonParserlazy(boolean lazy)Defines how JSON parser works.protected ValueConverterlookupValueConverter()Lookups for value converter for current path.JsonParserlooseMode(boolean looseMode)Enables 'loose' mode for parsing.JsonParsermap(java.lang.Class target)Maps a class to JSONs root.JsonParsermap(java.lang.String path, java.lang.Class target)Maps a class to given path.protected booleanmatch(char[] target)Matches char buffer with content on given location.<T> Tparse(char[] input)Parses input JSON char array.<T> Tparse(char[] input, java.lang.Class<T> targetType)Parses input JSON as given type.<T> Tparse(java.lang.String input)Parses input JSON string.<T> Tparse(java.lang.String input, java.lang.Class<T> targetType)Parses input JSON as given type.protected java.lang.ObjectparseArrayContent(java.lang.Class targetType, java.lang.Class componentType)Parses arrays, once when open bracket has been consumed.JsonArrayparseAsJsonArray(java.lang.String input)Parses input JSON toJsonArray, special case of parsing.JsonObjectparseAsJsonObject(java.lang.String input)Parses input JSON toJsonObject, special case ofparse(String, Class).<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.<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.protected java.lang.NumberparseNumber()Parses JSON numbers.protected java.lang.ObjectparseObjectContent(java.lang.Class targetType, java.lang.Class valueKeyType, java.lang.Class valueType)Parses object, once when open bracket has been consumed.protected java.lang.StringparseString()Parses a string.protected java.lang.StringparseStringContent(char quote)Parses string content, once when starting quote has been consumed.protected charparseUnicode()Parses 4 characters and returns unicode character.protected java.lang.StringparseUnquotedStringContent()Parses un-quoted string content.protected java.lang.ObjectparseValue(java.lang.Class targetType, java.lang.Class keyType, java.lang.Class componentType)Parses a JSON value.protected java.lang.ClassreplaceWithMappedTypeForPath(java.lang.Class target)Replaces type with mapped type for current path.protected voidreset()Resets JSON parser, so it can be reused.JsonParsersetClassMetadataName(java.lang.String name)Sets local class meta-data name.protected voidskipWhiteSpaces()Skips whitespaces.JsonParserstrictTypes(boolean strictTypes)Defines if type conversion is strict.protected voidsyntaxError(java.lang.String message, java.lang.Throwable cause)ThrowsJsonExceptionindicating a syntax error.JsonParseruseAltPaths()Enables usage of additional paths.JsonParserwithClassMetadata(boolean useMetadata)Sets usage of default class meta-data name.JsonParserwithValueConverter(java.lang.String path, ValueConverter valueConverter)DefinesValueConverterto use on given path.-
Methods inherited from class jodd.json.JsonParserBase
convertType, createMapToBean, injectValueIntoObject, newArrayInstance, newObjectInstance
-
-
-
-
Field Detail
-
KEYS
public static final java.lang.String KEYS
Map keys.- See Also:
- Constant Field Values
-
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
-
mapToBean
protected MapToBean mapToBean
-
mappings
protected java.util.Map<Path,java.lang.Class> mappings
-
convs
protected java.util.Map<Path,ValueConverter> convs
-
classMetadataName
protected java.lang.String classMetadataName
-
text
protected char[] text
-
textLen
protected int textLen
-
-
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 withnull.
-
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, appendvaluesto 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.
-
withValueConverter
public JsonParser withValueConverter(java.lang.String path, ValueConverter valueConverter)
DefinesValueConverterto use on given 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.
-
withClassMetadata
public JsonParser withClassMetadata(boolean useMetadata)
Sets usage of default class meta-data name. Using it may introduce a security hole, seesetClassMetadataName(String)for more details.- See Also:
setClassMetadataName(String)
-
allowClass
public JsonParser allowClass(java.lang.String classPattern)
Adds awildcardpattern for white-listing classes.- See Also:
setClassMetadataName(String)
-
allowAllClasses
public JsonParser allowAllClasses()
Removes the whitelist of allowed classes.- See Also:
setClassMetadataName(String)
-
parse
public <T> T parse(java.lang.String input, java.lang.Class<T> targetType)Parses input JSON as given type.
-
parseAsJsonObject
public JsonObject parseAsJsonObject(java.lang.String input)
Parses input JSON toJsonObject, special case ofparse(String, Class).
-
parseAsJsonArray
public JsonArray parseAsJsonArray(java.lang.String input)
Parses input JSON toJsonArray, 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 benullcomponentType- component type for maps and arrays, may benull
-
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 whentext.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, 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
protected void syntaxError(java.lang.String message, java.lang.Throwable cause)ThrowsJsonExceptionindicating a syntax error.
-
-