public class JSONStreamReader
extends java.lang.Object
For a streaming equivalent for writing JSON data, see the JSONWriter
and JSONStringer classes.
A simple example of how to use this class:
JSONStreamReader reader = new JSONStreamReader(JSON_TEXT);
while(reader.hasNext()) {
ParseState state = reader.nextState();
switch(state) {
case KEY:
System.out.println("KEY = " + reader.nextKey() );
break;
case NULL_VALUE:
case BOOLEAN_VALUE:
case NUMBER_VALUE:
case STRING_VALUE:
System.out.println(state + " = " + reader.nextValue());
break;
default:
System.out.println(state);
}
}
A more complete example can be found by reading the JSONObjectBuilder
source code.
| Modifier and Type | Class and Description |
|---|---|
static class |
JSONStreamReader.ParseState
States of the internal state machine.
|
| Modifier and Type | Field and Description |
|---|---|
protected BufferedAppendable |
bufferedAppender |
protected JSONLexer |
lexer |
protected ALStack<JSONLexer.Token> |
objectStack |
protected JSONStreamReader.ParseState |
state |
| Constructor and Description |
|---|
JSONStreamReader(java.io.InputStream inputStream,
java.nio.charset.Charset charset)
Construct a
JSONStreamReader from an InputStream and
a supplied Charset. |
JSONStreamReader(java.io.Reader reader)
Construct a
JSONStreamReader from a Reader. |
JSONStreamReader(java.lang.String s)
Construct a
JSONStreamReader from a String. |
| Modifier and Type | Method and Description |
|---|---|
<T extends java.lang.Appendable> |
appendNextNumberValue(T writer)
If the
ParseState was JSONStreamReader.ParseState.NUMBER_VALUE,
append the number sequence to the given Appendable. |
<T extends java.lang.Appendable> |
appendNextStringValue(T writer)
If the
ParseState was JSONStreamReader.ParseState.STRING_VALUE,
append the decoded value to the given Appendable. |
JSONStreamReader.ParseState |
currentState()
Return the current state of the parser, including any of the
internal states.
|
protected java.math.BigDecimal |
decodeBigDecimal(java.lang.String val)
Parse a number as a
BigDecimal strictly according to the JSON
specification. |
protected java.math.BigInteger |
decodeBigInteger(java.lang.String val,
boolean isDbl)
Parse a number as a
BigInteger strictly according to the JSON
specification. |
protected double |
decodeDouble(java.lang.String val,
boolean isDbl)
Decode a number as a double strictly according to the JSON specification.
|
protected int |
decodeInt(java.lang.String val,
boolean isDbl)
Parse a number as an int strictly according to the JSON specification.
|
protected long |
decodeLong(java.lang.String val,
boolean isDbl)
Parse a number as a long strictly according to the JSON specification.
|
protected java.lang.Object |
decodeNull()
Return an object that represents a JSON null value.
|
protected java.lang.Number |
decodeNumber(java.lang.String val,
boolean isDbl)
Decode a number strictly according to the JSON specification.
|
ParsePosition |
getParsePosition()
Indicates the current position of the scanner.
|
int |
getStackDepth()
Get the depth of nested objects, arrays, or values.
|
boolean |
hasNext()
Are there more parse states remaining in the source stream.
|
java.math.BigDecimal |
nextBigDecimalValue()
|
java.math.BigInteger |
nextBigIntegerValue()
|
boolean |
nextBooleanValue()
If the
ParseState was JSONStreamReader.ParseState.BOOLEAN_VALUE,
return the value as a boolean. |
double |
nextDoubleValue()
If the
ParseState was JSONStreamReader.ParseState.NUMBER_VALUE,
return the value as a double. |
int |
nextIntValue()
If the
ParseState was JSONStreamReader.ParseState.NUMBER_VALUE,
return the value as an int. |
java.lang.String |
nextKey()
If the
ParseState was JSONStreamReader.ParseState.KEY, return the text
of the key name. |
long |
nextLongValue()
If the
ParseState was JSONStreamReader.ParseState.NUMBER_VALUE,
return the value as a long. |
java.lang.Object |
nextNullValue()
If the
ParseState was JSONStreamReader.ParseState.NULL_VALUE,
return the value as a NULL object. |
java.lang.Number |
nextNumberValue()
|
JSONStreamReader.ParseState |
nextState()
Advance the parser onto the next state in the source stream, and
return a
JSONStreamReader.ParseState representing the state that was encountered. |
java.lang.String |
nextStringValue()
|
java.lang.Object |
nextValue()
If the
ParseState was JSONStreamReader.ParseState.NULL_VALUE,
JSONStreamReader.ParseState.BOOLEAN_VALUE, JSONStreamReader.ParseState.NUMBER_VALUE, or
JSONStreamReader.ParseState.STRING_VALUE, return the value as an Object. |
JSONStreamReader.ParseState |
skipToEndStructure()
Skip over the content of the current object or array, including any
nested objects or arrays.
|
java.lang.String |
toString()
Returns a string representation of the stream reader, including its
current position in the source stream.
|
protected final JSONLexer lexer
protected final ALStack<JSONLexer.Token> objectStack
protected final BufferedAppendable bufferedAppender
protected JSONStreamReader.ParseState state
public JSONStreamReader(java.io.Reader reader)
JSONStreamReader from a Reader.reader - A reader.public JSONStreamReader(java.io.InputStream inputStream,
java.nio.charset.Charset charset)
JSONStreamReader from an InputStream and
a supplied Charset.inputStream - the input stream containing the JSON datacharset - the character set with which to interpret the
input streampublic JSONStreamReader(java.lang.String s)
JSONStreamReader from a String.s - A source string.public boolean hasNext()
true if there are more parse states remaining, otherwise
falsepublic JSONStreamReader.ParseState nextState() throws JSONException
JSONStreamReader.ParseState representing the state that was encountered.JSONException - there was a problem with the source streampublic java.lang.String nextKey()
throws JSONException
ParseState was JSONStreamReader.ParseState.KEY, return the text
of the key name.
This method advances the parser onto the next state.
JSONException - there was a problem with the source streampublic java.lang.Object nextValue()
throws JSONException
ParseState was JSONStreamReader.ParseState.NULL_VALUE,
JSONStreamReader.ParseState.BOOLEAN_VALUE, JSONStreamReader.ParseState.NUMBER_VALUE, or
JSONStreamReader.ParseState.STRING_VALUE, return the value as an Object.
The values that can be returned here are:
JSONObject.NULLBoolean.TRUE or Boolean.FALSEDouble, Long, Integer,
BigInteger, or BigDecimalStringThis method advances the parser onto the next state.
JSONExceptionpublic java.lang.String nextStringValue()
throws JSONException
ParseState was JSONStreamReader.ParseState.STRING_VALUE,
return the value as a String.
This method advances the parser onto the next state.
JSONExceptionpublic <T extends java.lang.Appendable> T appendNextStringValue(T writer)
throws JSONException
ParseState was JSONStreamReader.ParseState.STRING_VALUE,
append the decoded value to the given Appendable.
This method is suitable for cases where very long String data is
expected, for instance for base-64 encoded data.
This method advances the parser onto the next state.
T - the type of Appendable, returned to the callerwriter - the writer to which the String value will be writtenJSONExceptionpublic boolean nextBooleanValue()
throws JSONException
ParseState was JSONStreamReader.ParseState.BOOLEAN_VALUE,
return the value as a boolean.
If the JSON value is not parseable as a boolean, as defined by the JSON grammar, a JSONException will be thrown.
This method advances the parser onto the next state.
JSONExceptionpublic java.lang.Object nextNullValue()
throws JSONException
ParseState was JSONStreamReader.ParseState.NULL_VALUE,
return the value as a NULL object.
If the JSON value is not parseable as a null, as defined by the JSON grammar, a JSONException will be thrown.
This method advances the parser onto the next state.
JSONObject.NULL valueJSONExceptionpublic java.lang.Number nextNumberValue()
throws JSONException
ParseState was JSONStreamReader.ParseState.NUMBER_VALUE,
return the value as a Number.
The number type returned is one of:
DoubleLongIntegerBigDecimalBigIntegerThis method advances the parser onto the next state.
Number classJSONExceptionpublic <T extends java.lang.Appendable> T appendNextNumberValue(T writer)
throws JSONException
ParseState was JSONStreamReader.ParseState.NUMBER_VALUE,
append the number sequence to the given Appendable.
This method is suitable for cases where the caller wishes to perform their own conversion of number values into a corresponding Object type.
This method advances the parser onto the next state.
T - the type of Appendable, returned to the callerwriter - the writer to which the number sequence will be writtenJSONExceptionpublic java.math.BigDecimal nextBigDecimalValue()
throws JSONException
ParseState was JSONStreamReader.ParseState.NUMBER_VALUE,
return the value as a BigDecimal.
This method advances the parser onto the next state.
BigDecimal classJSONExceptionpublic java.math.BigInteger nextBigIntegerValue()
throws JSONException
ParseState was JSONStreamReader.ParseState.NUMBER_VALUE,
return the value as a BigInteger.
If the JSON value is not parseable as a big integer, as defined by the JSON grammar, a JSONException will be thrown.
This method advances the parser onto the next state.
BigInteger classJSONExceptionpublic double nextDoubleValue()
throws JSONException
ParseState was JSONStreamReader.ParseState.NUMBER_VALUE,
return the value as a double.
This method advances the parser onto the next state.
JSONExceptionpublic int nextIntValue()
throws JSONException
ParseState was JSONStreamReader.ParseState.NUMBER_VALUE,
return the value as an int.
If the JSON value is not parseable as an int, as defined by the JSON
grammar, a JSONException will be thrown.
This method advances the parser onto the next state.
JSONExceptionpublic long nextLongValue()
throws JSONException
ParseState was JSONStreamReader.ParseState.NUMBER_VALUE,
return the value as a long.
If the JSON value is not parseable as a long, as defined by the JSON
grammar, a JSONException will be thrown.
This method advances the parser onto the next state.
JSONExceptionpublic int getStackDepth()
public ParsePosition getParsePosition()
ParsePosition representing the current location of
the scannerpublic JSONStreamReader.ParseState currentState()
JSONStreamReader.ParseStatepublic JSONStreamReader.ParseState skipToEndStructure() throws JSONException
ParseState of the object or arrayJSONExceptionprotected java.lang.Number decodeNumber(java.lang.String val,
boolean isDbl)
throws JSONException
val - the String value to be decoded as a numberisDbl - true to indicate the value is a decimal or
exponent value, otherwise falseJSONExceptionprotected double decodeDouble(java.lang.String val,
boolean isDbl)
throws JSONException
val - the String value to be decoded as a doubleisDbl - true to indicate the value is a decimal or
exponent value, otherwise falseJSONExceptionprotected int decodeInt(java.lang.String val,
boolean isDbl)
throws JSONException
val - the String value to be decoded as an intisDbl - true to indicate the value is a decimal or
exponent value, otherwise falseJSONExceptionprotected long decodeLong(java.lang.String val,
boolean isDbl)
throws JSONException
val - the String value to be decoded as a longisDbl - true to indicate the value is a decimal or
exponent value, otherwise falseJSONExceptionprotected java.math.BigDecimal decodeBigDecimal(java.lang.String val)
throws JSONException
BigDecimal strictly according to the JSON
specification.val - the String value to be decoded as a big decimal valueJSONExceptionprotected java.math.BigInteger decodeBigInteger(java.lang.String val,
boolean isDbl)
throws JSONException
BigInteger strictly according to the JSON
specification.val - the String value to be decoded as a big integerisDbl - true to indicate the value is a decimal or
exponent value, otherwise falseJSONExceptionprotected java.lang.Object decodeNull()
throws JSONException
JSONExceptionpublic java.lang.String toString()
toString in class java.lang.Object