Class JsonParser
- All Implemented Interfaces:
JsonMetaProvider
- Direct Known Subclasses:
Json5Parser,JsonParser.Strict
Media types
Handles
Description
This parser uses a state machine, which makes it very fast and efficient. It parses JSON in about 70% of the time that it takes the built-in Java DOM parsers to parse equivalent XML.
This parser handles all valid JSON syntax. In addition, when strict mode is disable, the parser also handles the following:
-
Javascript comments (both
/*and//) are ignored. - Both single and double quoted strings.
-
Automatically joins concatenated strings (e.g.
)."aaa" +'bbb' - Unquoted attributes and values.
Also handles negative, decimal, hexadecimal, octal, and double numbers, including exponential notation.
This parser handles the following input, and automatically returns the corresponding Java class.
-
JSON objects (
"{...}" ) are converted toJsonMaps. Note: If aattribute is specified on the object, then an attempt is made to convert the object to an instance of the specified Java bean class. See the_type ='xxx' BeanContext.Builder.typePropertyName(String)setting for more information about parsing beans from JSON. -
JSON arrays (
"[...]" ) are converted toJsonLists. -
JSON string literals (
"'xyz'" ) are converted toStrings. -
JSON numbers (
"123" , including octal/hexadecimal/exponential notation) are converted toIntegers,Longs,Floats, orDoublesdepending on whether the number is decimal, and the size of the number. -
JSON booleans (
"false" ) are converted toBooleans. -
JSON nulls (
"null" ) are converted tonull . -
Input consisting of only whitespace or JSON comments are converted to
null .
Input can be any of the following:
-
"{...}" - Converted to anJsonMapor an instance of a Java bean if a_type attribute is present. -
"[...]" - Converted to anJsonList. -
"123..." - Converted to aNumber(eitherInteger,Long,Float, orDouble). -
"true" /"false" - Converted to aBoolean. -
"null" - Returnsnull . -
"'xxx'" - Converted to aString. -
"\"xxx\"" - Converted to aString. -
"'xxx' + \"yyy\"" - Converted to a concatenatedString.
TIP: If you know you're parsing a JSON object or array, it can be easier to parse it using the
JsonMap(CharSequence) or JsonList(CharSequence) constructors instead of using this class.
The end result should be the same.
Notes:
- This class is thread safe and reusable.
See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classBuilder class.static classDefault parser, strict mode.Nested classes/interfaces inherited from class org.apache.juneau.parser.Parser
Parser.Null -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final JsonParserDefault parser, all default settings.static final JsonParserDefault parser, all default settings.protected final booleanFields inherited from class org.apache.juneau.parser.Parser
autoCloseStreams, consumes, debugOutputLines, listener, strict, trimStrings, unbufferedFields inherited from class org.apache.juneau.BeanContextable
beanContextFields inherited from class org.apache.juneau.Context
CONTEXT_APPLY_FILTER -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptioncopy()Creates a builder from this context object.static JsonParser.Buildercreate()Creates a new builder for this object.Create a session builder based on the properties defined on this context.Returns the language-specific metadata on the specified bean property.getJsonClassMeta(ClassMeta<?> cm) Returns the language-specific metadata on the specified class.Returns a session to use for this context.protected final booleanValidate end.Methods inherited from class org.apache.juneau.parser.ReaderParser
getFileCharset, getStreamCharset, isReaderParser, propertiesMethods inherited from class org.apache.juneau.parser.Parser
canHandle, createParserBuilder, doParse, getDebugOutputLines, getListener, getMediaTypes, getPrimaryMediaType, isAutoCloseStreams, isStrict, isTrimStrings, isUnbuffered, parse, parse, parse, parse, parse, parse, parseArgs, parseIntoCollection, parseIntoMapMethods inherited from class org.apache.juneau.BeanContextable
getBeanContextMethods inherited from class org.apache.juneau.Context
createBuilder, getAnnotationProvider, init, isDebug, toString
-
Field Details
-
DEFAULT
Default parser, all default settings. -
DEFAULT_STRICT
Default parser, all default settings. -
validateEnd
-
-
Constructor Details
-
JsonParser
Constructor.- Parameters:
builder- The builder for this object.
-
-
Method Details
-
create
Creates a new builder for this object.- Returns:
- A new builder.
-
copy
Description copied from class:ContextCreates a builder from this context object.Builders are used to define new contexts (e.g. serializers, parsers) based on existing configurations.
-
createSession
Description copied from class:ContextCreate a session builder based on the properties defined on this context.Use this method for creating sessions where you want to override basic settings. Otherwise, use
Context.getSession()directly.- Overrides:
createSessionin classReaderParser- Returns:
- A new session builder.
-
getJsonBeanPropertyMeta
Description copied from interface:JsonMetaProviderReturns the language-specific metadata on the specified bean property.- Specified by:
getJsonBeanPropertyMetain interfaceJsonMetaProvider- Parameters:
bpm- The bean property to return the metadata on.- Returns:
- The metadata.
-
getJsonClassMeta
Description copied from interface:JsonMetaProviderReturns the language-specific metadata on the specified class.- Specified by:
getJsonClassMetain interfaceJsonMetaProvider- Parameters:
cm- The class to return the metadata on.- Returns:
- The metadata.
-
getSession
Description copied from class:ContextReturns a session to use for this context.Note that subclasses may opt to return a reusable non-modifiable session.
- Overrides:
getSessionin classReaderParser- Returns:
- A new session object.
-
isValidateEnd
Validate end.- Returns:
true if after parsing a POJO from the input, verifies that the remaining input in the stream consists of only comments or whitespace.- See Also:
-