public final class JSONLimitStreamReader extends JSONStreamReader
The JSONLimitStreamReader class is similar to the JSONStreamReader
class, but intended to be more robust against untrusted JSON sources.
For a more robust solution to building object structures, see
JSONLimitBuilder.
A simple example of how to use this class:
JSONLimitStreamReader reader = new JSONLimitStreamReader(JSON_TEXT);
reader.withLimits(BuilderLimits.secureDefaults());
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);
}
}
For a streaming equivalent for writing JSON data, see the JSONWriter
and JSONStringer classes.
JSONStreamReader.ParseStatebufferedAppender, lexer, objectStack, state| Constructor and Description |
|---|
JSONLimitStreamReader(java.io.InputStream inputStream,
java.nio.charset.Charset charset)
Construct a
JSONStreamReader from an InputStream and
a supplied Charset. |
JSONLimitStreamReader(java.io.Reader reader)
Construct a
JSONStreamReader from a Reader. |
JSONLimitStreamReader(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. |
java.math.BigDecimal |
nextBigDecimalValue()
|
java.math.BigInteger |
nextBigIntegerValue()
|
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.Number |
nextNumberValue()
|
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. |
void |
setExponentDigits(int exponentDigits)
The maximum number of exponent digits in a number.
|
void |
setKeyLength(int keyLength)
The maximum length of any key.
|
void |
setMantissaDigits(int mantissaDigits)
The maximum number of mantissa digits in a number.
|
void |
setStringLength(int stringLength)
The maximum length of any string value.
|
java.lang.String |
toString()
Returns a string representation of the stream reader, including its
current position in the source stream.
|
JSONLimitStreamReader |
withLimits(BuilderLimits limits)
Set keyLength, stringLength, mantissaDigits, and exponentDigits
using values from the supplied
BuilderLimits object. |
appendNextStringValue, currentState, decodeBigDecimal, decodeBigInteger, decodeDouble, decodeInt, decodeLong, decodeNull, decodeNumber, getParsePosition, getStackDepth, hasNext, nextBooleanValue, nextNullValue, nextState, skipToEndStructurepublic JSONLimitStreamReader(java.io.Reader reader)
JSONStreamReader from a Reader.reader - A reader.public JSONLimitStreamReader(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 JSONLimitStreamReader(java.lang.String s)
JSONStreamReader from a String.s - A source string.public JSONLimitStreamReader withLimits(BuilderLimits limits)
BuilderLimits object.
See BuilderLimits.secureDefaults() for some practical limits.
limits - the limits to be setpublic void setKeyLength(int keyLength)
keyLength - the maximum length of any keypublic void setStringLength(int stringLength)
stringLength - the maximum length of any string valuepublic void setMantissaDigits(int mantissaDigits)
mantissaDigits - the maximum number of mantissa digits of any
number valuepublic void setExponentDigits(int exponentDigits)
exponentDigits - the maximum number of exponent digits of any
number valuepublic 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.
nextKey in class JSONStreamReaderJSONException - 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.
nextValue in class JSONStreamReaderJSONExceptionpublic 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.
nextStringValue in class JSONStreamReaderJSONExceptionpublic 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.
nextNumberValue in class JSONStreamReaderNumber 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.
appendNextNumberValue in class JSONStreamReaderT - 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.
nextBigDecimalValue in class JSONStreamReaderBigDecimal 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.
nextBigIntegerValue in class JSONStreamReaderBigInteger 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.
nextDoubleValue in class JSONStreamReaderJSONExceptionpublic 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.
nextIntValue in class JSONStreamReaderJSONExceptionpublic 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.
nextLongValue in class JSONStreamReaderJSONExceptionpublic java.lang.String toString()
toString in class JSONStreamReader