Class JsonParser
- All Implemented Interfaces:
AnnotationProvider
,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 a
attribute 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
, orDoubles
depending 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 anJsonMap
or 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
Modifier and TypeClassDescriptionstatic class
Builder class.static class
Default parser, strict mode.Nested classes/interfaces inherited from class org.apache.juneau.parser.Parser
Parser.Null
-
Field Summary
Modifier and TypeFieldDescriptionstatic final JsonParser
Default parser, all default settings.static final JsonParser
Default parser, all default settings.Fields inherited from class org.apache.juneau.Context
CONTEXT_APPLY_FILTER
Fields inherited from interface org.apache.juneau.AnnotationProvider
DISABLE_ANNOTATION_CACHING
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptioncopy()
Creates a builder from this context object.static JsonParser.Builder
create()
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 boolean
Validate end.Methods inherited from class org.apache.juneau.parser.ReaderParser
getFileCharset, getStreamCharset, isReaderParser, properties
Methods 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, parseIntoMap
Methods inherited from class org.apache.juneau.BeanContextable
getBeanContext
Methods inherited from class org.apache.juneau.Context
createBuilder, firstAnnotation, firstAnnotation, firstAnnotation, firstAnnotation, firstDeclaredAnnotation, forEachAnnotation, forEachAnnotation, forEachAnnotation, forEachAnnotation, forEachDeclaredAnnotation, hasAnnotation, hasAnnotation, hasAnnotation, hasAnnotation, init, isDebug, lastAnnotation, lastAnnotation, lastAnnotation, lastAnnotation, lastDeclaredAnnotation, toString
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
Methods inherited from interface org.apache.juneau.AnnotationProvider
firstAnnotation, firstAnnotation, firstAnnotation, firstAnnotation, firstDeclaredAnnotation, forEachAnnotation, forEachAnnotation, forEachAnnotation, forEachAnnotation, forEachDeclaredAnnotation, lastAnnotation, lastAnnotation, lastAnnotation, lastAnnotation, lastDeclaredAnnotation
-
Field Details
-
DEFAULT
Default parser, all default settings. -
DEFAULT_STRICT
Default parser, all default settings.
-
-
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:Context
Creates 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:Context
Create 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:
createSession
in classReaderParser
- Returns:
- A new session builder.
-
getSession
Description copied from class:Context
Returns a session to use for this context.Note that subclasses may opt to return a reusable non-modifiable session.
- Overrides:
getSession
in classReaderParser
- Returns:
- A new session object.
-
getJsonClassMeta
Description copied from interface:JsonMetaProvider
Returns the language-specific metadata on the specified class.- Specified by:
getJsonClassMeta
in interfaceJsonMetaProvider
- Parameters:
cm
- The class to return the metadata on.- Returns:
- The metadata.
-
getJsonBeanPropertyMeta
Description copied from interface:JsonMetaProvider
Returns the language-specific metadata on the specified bean property.- Specified by:
getJsonBeanPropertyMeta
in interfaceJsonMetaProvider
- Parameters:
bpm
- The bean property to return the metadata on.- Returns:
- The metadata.
-
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:
-