Package org.glassfish.json
Class JsonParserImpl
- java.lang.Object
-
- org.glassfish.json.JsonParserImpl
-
- All Implemented Interfaces:
java.io.Closeable
,java.lang.AutoCloseable
,JsonParser
public class JsonParserImpl extends java.lang.Object implements JsonParser
JSON parser implementation. NoneContext, ArrayContext, ObjectContext is used to go to next parser state.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private class
JsonParserImpl.ArrayContext
private class
JsonParserImpl.Context
private class
JsonParserImpl.NoneContext
private class
JsonParserImpl.ObjectContext
private static class
JsonParserImpl.Stack
-
Nested classes/interfaces inherited from interface javax.json.stream.JsonParser
JsonParser.Event
-
-
Field Summary
Fields Modifier and Type Field Description private BufferPool
bufferPool
private JsonParserImpl.Context
currentContext
private JsonParser.Event
currentEvent
private JsonParserImpl.Stack
stack
private JsonTokenizer
tokenizer
-
Constructor Summary
Constructors Constructor Description JsonParserImpl(java.io.InputStream in, java.nio.charset.Charset encoding, BufferPool bufferPool)
JsonParserImpl(java.io.InputStream in, BufferPool bufferPool)
JsonParserImpl(java.io.Reader reader, BufferPool bufferPool)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
close()
Closes this parser and frees any resources associated with the parser.JsonArray
getArray()
Returns aJsonArray
and advance the parser to the the correspondingEND_ARRAY
.private JsonArray
getArray(JsonArrayBuilder builder)
java.util.stream.Stream<JsonValue>
getArrayStream()
Returns a stream of theJsonArray
elements.java.math.BigDecimal
getBigDecimal()
Returns a JSON number as aBigDecimal
.int
getInt()
Returns a JSON number as an integer.JsonLocation
getLastCharLocation()
JsonLocation
getLocation()
Return the location that corresponds to the parser's current state in the JSON input source.long
getLong()
Returns a JSON number as a long.JsonObject
getObject()
Returns aJsonObject
and advances the parser to the correspondingEND_OBJECT
.private JsonObject
getObject(JsonObjectBuilder builder)
java.util.stream.Stream<java.util.Map.Entry<java.lang.String,JsonValue>>
getObjectStream()
Returns a stream of theJsonObject
's name/value pairs.java.lang.String
getString()
Returns aString
for the name in a name/value pair, for a string value or a number value.JsonValue
getValue()
Returns aJsonValue
at the current parser position.java.util.stream.Stream<JsonValue>
getValueStream()
Returns a stream ofJsonValue
from a sequence of JSON values.boolean
hasNext()
Returnstrue
if there are more parsing states.(package private) boolean
isDefinitelyInt()
(package private) boolean
isDefinitelyLong()
boolean
isIntegralNumber()
Returns true if the JSON number at the current parser state is a integral number.JsonParser.Event
next()
Returns the event for the next parsing state.private JsonParsingException
parsingException(JsonTokenizer.JsonToken token, java.lang.String expectedTokens)
void
skipArray()
Advance the parser toEND_ARRAY
.void
skipObject()
Advance the parser toEND_OBJECT
.
-
-
-
Field Detail
-
bufferPool
private final BufferPool bufferPool
-
currentContext
private JsonParserImpl.Context currentContext
-
currentEvent
private JsonParser.Event currentEvent
-
stack
private final JsonParserImpl.Stack stack
-
tokenizer
private final JsonTokenizer tokenizer
-
-
Constructor Detail
-
JsonParserImpl
public JsonParserImpl(java.io.Reader reader, BufferPool bufferPool)
-
JsonParserImpl
public JsonParserImpl(java.io.InputStream in, BufferPool bufferPool)
-
JsonParserImpl
public JsonParserImpl(java.io.InputStream in, java.nio.charset.Charset encoding, BufferPool bufferPool)
-
-
Method Detail
-
getString
public java.lang.String getString()
Description copied from interface:JsonParser
Returns aString
for the name in a name/value pair, for a string value or a number value. This method should only be called when the parser state isJsonParser.Event.KEY_NAME
,JsonParser.Event.VALUE_STRING
, orJsonParser.Event.VALUE_NUMBER
.- Specified by:
getString
in interfaceJsonParser
- Returns:
- a name when the parser state is
JsonParser.Event.KEY_NAME
a string value when the parser state isJsonParser.Event.VALUE_STRING
a number value when the parser state isJsonParser.Event.VALUE_NUMBER
-
isIntegralNumber
public boolean isIntegralNumber()
Description copied from interface:JsonParser
Returns true if the JSON number at the current parser state is a integral number. ABigDecimal
may be used to store the value internally and this method semantics are defined using itsscale()
. If the scale is zero, then it is considered integral type. This integral type information can be used to invoke an appropriate accessor method to obtain a numeric value as in the following example:JsonParser parser = ... if (parser.isIntegralNumber()) { parser.getInt(); // or other methods to get integral value } else { parser.getBigDecimal(); }
- Specified by:
isIntegralNumber
in interfaceJsonParser
- Returns:
- true if this number is a integral number, otherwise false
-
getInt
public int getInt()
Description copied from interface:JsonParser
Returns a JSON number as an integer. The returned value is equal tonew BigDecimal(getString()).intValue()
. Note that this conversion can lose information about the overall magnitude and precision of the number value as well as return a result with the opposite sign. This method should only be called when the parser state isJsonParser.Event.VALUE_NUMBER
.- Specified by:
getInt
in interfaceJsonParser
- Returns:
- an integer for a JSON number
- See Also:
BigDecimal.intValue()
-
isDefinitelyInt
boolean isDefinitelyInt()
-
isDefinitelyLong
boolean isDefinitelyLong()
-
getLong
public long getLong()
Description copied from interface:JsonParser
Returns a JSON number as a long. The returned value is equal tonew BigDecimal(getString()).longValue()
. Note that this conversion can lose information about the overall magnitude and precision of the number value as well as return a result with the opposite sign. This method is only called when the parser state isJsonParser.Event.VALUE_NUMBER
.- Specified by:
getLong
in interfaceJsonParser
- Returns:
- a long for a JSON number
- See Also:
BigDecimal.longValue()
-
getBigDecimal
public java.math.BigDecimal getBigDecimal()
Description copied from interface:JsonParser
Returns a JSON number as aBigDecimal
. TheBigDecimal
is created usingnew BigDecimal(getString())
. This method should only called when the parser state isJsonParser.Event.VALUE_NUMBER
.- Specified by:
getBigDecimal
in interfaceJsonParser
- Returns:
- a
BigDecimal
for a JSON number
-
getArray
public JsonArray getArray()
Description copied from interface:JsonParser
Returns aJsonArray
and advance the parser to the the correspondingEND_ARRAY
.- Specified by:
getArray
in interfaceJsonParser
- Returns:
- the
JsonArray
at the current parser position
-
getObject
public JsonObject getObject()
Description copied from interface:JsonParser
Returns aJsonObject
and advances the parser to the correspondingEND_OBJECT
.- Specified by:
getObject
in interfaceJsonParser
- Returns:
- the
JsonObject
at the current parser position
-
getValue
public JsonValue getValue()
Description copied from interface:JsonParser
Returns aJsonValue
at the current parser position. If the parser state isSTART_ARRAY
, the behavior is the same asJsonParser.getArray()
. If the parser state isSTART_OBJECT
, the behavior is the same asJsonParser.getObject()
. For all other cases, if applicable, the JSON value is read and returned.- Specified by:
getValue
in interfaceJsonParser
- Returns:
- the
JsonValue
at the current parser position.
-
getArrayStream
public java.util.stream.Stream<JsonValue> getArrayStream()
Description copied from interface:JsonParser
Returns a stream of theJsonArray
elements. The parser state must beSTART_ARRAY
. The elements are read lazily, on an as-needed basis, as required by the stream operations. If the stream operations do not consume all of the array elements,JsonParser.skipArray()
can be used to skip the unprocessed array elements.- Specified by:
getArrayStream
in interfaceJsonParser
- Returns:
- a stream of elements of the
JsonArray
-
getObjectStream
public java.util.stream.Stream<java.util.Map.Entry<java.lang.String,JsonValue>> getObjectStream()
Description copied from interface:JsonParser
Returns a stream of theJsonObject
's name/value pairs. The parser state must beSTART_OBJECT
. The name/value pairs are read lazily, on an as-needed basis, as required by the stream operations. If the stream operations do not consume all of the object's name/value pairs,JsonParser.skipObject()
can be used to skip the unprocessed elements.- Specified by:
getObjectStream
in interfaceJsonParser
- Returns:
- a stream of name/value pairs of the
JsonObject
-
getValueStream
public java.util.stream.Stream<JsonValue> getValueStream()
Description copied from interface:JsonParser
Returns a stream ofJsonValue
from a sequence of JSON values. The values are read lazily, on an as-needed basis, as needed by the stream operations.- Specified by:
getValueStream
in interfaceJsonParser
- Returns:
- a Stream of
JsonValue
-
skipArray
public void skipArray()
Description copied from interface:JsonParser
Advance the parser toEND_ARRAY
. If the parser is in array context, i.e. it has previously encountered aSTART_ARRAY
without encountering the correspondingEND_ARRAY
, the parser is advanced to the correspondingEND_ARRAY
. If the parser is not in any array context, nothing happens.- Specified by:
skipArray
in interfaceJsonParser
-
skipObject
public void skipObject()
Description copied from interface:JsonParser
Advance the parser toEND_OBJECT
. If the parser is in object context, i.e. it has previously encountered aSTART_OBJECT
without encountering the correspondingEND_OBJECT
, the parser is advanced to the correspondingEND_OBJECT
. If the parser is not in any object context, nothing happens.- Specified by:
skipObject
in interfaceJsonParser
-
getArray
private JsonArray getArray(JsonArrayBuilder builder)
-
getObject
private JsonObject getObject(JsonObjectBuilder builder)
-
getLocation
public JsonLocation getLocation()
Description copied from interface:JsonParser
Return the location that corresponds to the parser's current state in the JSON input source. The location information is only valid in the current parser state (or until the parser is advanced to a next state).- Specified by:
getLocation
in interfaceJsonParser
- Returns:
- a non-null location corresponding to the current parser state in JSON input source
-
getLastCharLocation
public JsonLocation getLastCharLocation()
-
hasNext
public boolean hasNext()
Description copied from interface:JsonParser
Returnstrue
if there are more parsing states. This method returnsfalse
if the parser reaches the end of the JSON text.- Specified by:
hasNext
in interfaceJsonParser
- Returns:
true
if there are more parsing states.
-
next
public JsonParser.Event next()
Description copied from interface:JsonParser
Returns the event for the next parsing state.- Specified by:
next
in interfaceJsonParser
- Returns:
- the event for the next parsing state
-
close
public void close()
Description copied from interface:JsonParser
Closes this parser and frees any resources associated with the parser. This method closes the underlying input source.- Specified by:
close
in interfacejava.lang.AutoCloseable
- Specified by:
close
in interfacejava.io.Closeable
- Specified by:
close
in interfaceJsonParser
-
parsingException
private JsonParsingException parsingException(JsonTokenizer.JsonToken token, java.lang.String expectedTokens)
-
-