Class JsonParser
- All Implemented Interfaces:
JsonMetaProvider,ArrayRecordReadable,RecordReadable,TokenReadable
- Direct Known Subclasses:
Json5Parser,JsonlParser
This parser strictly enforces RFC 8259 JSON syntax. Double-quoted strings only; single-quoted strings,
comments, trailing commas, and unquoted keys are rejected. For JSON5 syntax (single quotes, comments,
etc.), use Json5Parser.
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 RFC 8259 JSON syntax.
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' MarshallingContext.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 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(double quotes only).
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.
Example input (Map of name/age):
{
Complex (nested object + array):
{
Notes:
- This class is thread safe and reusable.
See Also:
- JSON
Json5Parser- For JSON5 syntax (single quotes, comments, etc.)
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classJsonParser.Builder<SELF extends JsonParser.Builder<SELF>>Builder class.Nested classes/interfaces inherited from class org.apache.juneau.marshall.parser.ReaderParser
ReaderParser.DefaultBuilderNested classes/interfaces inherited from class org.apache.juneau.marshall.parser.Parser
Parser.Null -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final JsonParserDefault parser, all default settings.protected final booleanFields inherited from class org.apache.juneau.marshall.parser.Parser
autoCloseStreams, consumes, debugOutputLines, listener, nulls, trimStrings, unbufferedFields inherited from class org.apache.juneau.marshall.MarshallingContextable
marshallingContextFields inherited from class org.apache.juneau.marshall.Context
CONTEXT_APPLY_FILTER -
Constructor Summary
Constructors -
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 booleanValidate end.readArrayRecords(Object input) Convenience delegator for the streaming array-elementRecordReader(uses default session args; seereadTokens(Object)).readTokens(Object input) Convenience delegator that opens aJsonTokenReaderover the input using default session arguments (mirrorsParser.read(Object, Class)).Methods inherited from class org.apache.juneau.marshall.parser.ReaderParser
getStreamCharset, isReaderParser, propertiesMethods inherited from class org.apache.juneau.marshall.parser.Parser
canHandle, createParserBuilder, doRead, getDebugOutputLines, getListener, getMediaTypes, getNulls, getPrimaryMediaType, isAutoCloseStreams, isTrimStrings, isUnbuffered, read, read, read, read, read, read, readArgs, readIntoCollection, readIntoMap, readWithOuterMethods inherited from class org.apache.juneau.marshall.MarshallingContextable
getDurationFormat, getMarshallingContext, getPeriodFormatMethods inherited from class org.apache.juneau.marshall.Context
createBuilder, getAnnotationProvider, init, isDebug, toStringMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface org.apache.juneau.marshall.stream.ArrayRecordReadable
isArrayRecordStreaming, readArrayRecordsMethods inherited from interface org.apache.juneau.marshall.stream.RecordReadable
isRecordStreamingMethods inherited from interface org.apache.juneau.marshall.stream.TokenReadable
readRecords
-
Field Details
-
DEFAULT
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.
-
readTokens
Convenience delegator that opens aJsonTokenReaderover the input using default session arguments (mirrorsParser.read(Object, Class)).The real implementation lives on
JsonParserSession.readTokens(Object). Callers that need request-derived configuration (locale, timezone, schema, swaps) should callcreateSession()and invokeJsonParserSession.readTokens(Object)on the built session instead.- Specified by:
readTokensin interfaceTokenReadable- Parameters:
input- The input. AcceptsReader,CharSequence,InputStream,, orbyte []File.- Returns:
- A new
JsonTokenReader. - Throws:
IOException- If a problem occurred opening the underlying input.
-
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:
-
readArrayRecords
Convenience delegator for the streaming array-elementRecordReader(uses default session args; seereadTokens(Object)). Real impl onJsonParserSession.readArrayRecords(Object).- Specified by:
readArrayRecordsin interfaceArrayRecordReadable- Parameters:
input- The input.- Returns:
- A new element-streamed
RecordReader. - Throws:
IOException- If a problem occurred opening the underlying input.
-