Package oracle.spatial.georaster.util
Class JsonParser
- java.lang.Object
-
- oracle.spatial.georaster.util.JsonParser
-
- All Implemented Interfaces:
java.lang.AutoCloseable
public class JsonParser extends java.lang.Object implements java.lang.AutoCloseable
This class provides methods to read a JSON input.
Methods starting with get will return an item related to the previousnext()
return value.
Methods starting with read involve one or more internal calls tonext()
method to read complex values. This is an example of hot to use it:
try(JsonParser parser = new JsonParser(reader)){ while(parser.hasNext()){ switch(parser.next()){ case KEY_NAME: parser.getString(); break; case VALUE_STRING: parser.getString(); break; } } }
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
JsonParser.Event
This are all the possible tokens identified during parsing.class
JsonParser.JsonException
IO related exception.static class
JsonParser.JsonLocation
Position ofJsonParser
when this object was build.class
JsonParser.JsonParsingException
JSON syntax related exception
-
Constructor Summary
Constructors Constructor Description JsonParser(java.io.Reader reader)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
close()
java.math.BigDecimal
getBigDecimal()
boolean
getBoolean()
double
getDouble()
int
getInt()
JsonParser.JsonLocation
getLocation()
long
getLong()
java.lang.String
getString()
This method should be called to return JSON field names or JSON values.
If JSON value is a JSON Objects {linkreadJSONObject()
will return the JSON Object as string.
If JSON value is a JSON Array {linkreadJSONArray()
will return the JSON Array as string.java.lang.String
getStringValue()
Calling this method avoid calling {link #next()} after the previous call returned Event.KEY_NAME to read a JSON string values.boolean
hasNext()
boolean
isIntegralNumber()
This method help to identify if the JSON number is integral by not having a fractional or exponential part.JsonParser.Event
next()
This method returned the nextJsonParser.Event
recognized by the parser.
If event is Event.KEY_NAME callinggetString()
will return JSON field name.
If event is Event.VALUE_STRING callinggetString()
will return JSON field value as string.double[][]
readArrayOfDoubleArray()
This method simplify the process of reading a JSON value as a double[][].java.lang.String[][]
readArrayOfStringArray()
This method simplify the process of reading a JSON value as a String[][].double[]
readDoubleArray()
This method simplify the process of reading a JSON value as a double[].int[]
readIntArray()
This method simplify the process of reading a JSON value as int[].java.lang.String
readJSONArray()
This method will advance the parser to the end of array and return the array value as string.java.lang.String
readJSONObject()
This method will advance the parser to the end of JSON object and return the object value as string.long[]
readLongArray()
This method simplify the process of reading a JSON value as a long[].java.lang.String[]
readStringArray()
This method simplify the process of reading a JSON value as a String[].static java.lang.String
writeValueAsIndentedString(java.lang.String str)
Receive a JSON String and return a new indented JSON String.
-
-
-
Method Detail
-
hasNext
public boolean hasNext()
- Returns:
- true if calling
next()
will return a newJsonParser.Event
false if the parser has reached end of JSON input.
-
next
public JsonParser.Event next() throws JsonParser.JsonParsingException
This method returned the nextJsonParser.Event
recognized by the parser.
If event is Event.KEY_NAME callinggetString()
will return JSON field name.
If event is Event.VALUE_STRING callinggetString()
will return JSON field value as string.- Returns:
- An
Event
instance. - Throws:
JsonParser.JsonParsingException
- if an invalid character was read during parsing.
-
readStringArray
public java.lang.String[] readStringArray() throws JsonParser.JsonParsingException
This method simplify the process of reading a JSON value as a String[].- Returns:
- an array of strings, never returns null.
- Throws:
JsonParser.JsonParsingException
- if previous invocation tonext()
does not return Event.START_ARRAY.
-
readArrayOfStringArray
public java.lang.String[][] readArrayOfStringArray() throws JsonParser.JsonException, JsonParser.JsonParsingException
This method simplify the process of reading a JSON value as a String[][].- Returns:
- an array of array of strings, never returns null.
- Throws:
JsonParser.JsonParsingException
- if previous invocation tonext()
does not return Event.START_ARRAY.JsonParser.JsonException
-
readDoubleArray
public double[] readDoubleArray() throws JsonParser.JsonParsingException
This method simplify the process of reading a JSON value as a double[].- Returns:
- an array of numbers, never returns null.
- Throws:
JsonParser.JsonParsingException
- if previous invocation tonext()
does not return Event.START_ARRAY.
-
readArrayOfDoubleArray
public double[][] readArrayOfDoubleArray() throws JsonParser.JsonException, JsonParser.JsonParsingException
This method simplify the process of reading a JSON value as a double[][].- Returns:
- an array of array of numbers, never returns null.
- Throws:
JsonParser.JsonParsingException
- if previous invocation tonext()
does not return Event.START_ARRAY.JsonParser.JsonException
-
readIntArray
public int[] readIntArray() throws JsonParser.JsonParsingException
This method simplify the process of reading a JSON value as int[].- Returns:
- an array of integers, never returns null.
- Throws:
JsonParser.JsonParsingException
- if previous invocation tonext()
does not return Event.START_ARRAY.
-
readLongArray
public long[] readLongArray() throws JsonParser.JsonParsingException
This method simplify the process of reading a JSON value as a long[].- Returns:
- an array of long, never returns null.
- Throws:
JsonParser.JsonParsingException
- if previous invocation tonext()
does not return Event.START_ARRAY.
-
getString
public java.lang.String getString()
This method should be called to return JSON field names or JSON values.
If JSON value is a JSON Objects {linkreadJSONObject()
will return the JSON Object as string.
If JSON value is a JSON Array {linkreadJSONArray()
will return the JSON Array as string.- Returns:
- a String object, this method return null if previous call to {link #next()} returned Event.VALUE_NULL
-
getStringValue
public java.lang.String getStringValue() throws JsonParser.JsonParsingException
Calling this method avoid calling {link #next()} after the previous call returned Event.KEY_NAME to read a JSON string values.- Returns:
- the next JSON string value.
- Throws:
JsonParser.JsonParsingException
-
isIntegralNumber
public boolean isIntegralNumber()
This method help to identify if the JSON number is integral by not having a fractional or exponential part.- Returns:
- true if previous call to
next()
returned Event.VALUE_NUMBER and does not have fractional or exponential part.
-
getInt
public int getInt() throws JsonParser.JsonParsingException
- Returns:
- the JSON number as integer.
- Throws:
JsonParser.JsonParsingException
-
getLong
public long getLong() throws JsonParser.JsonParsingException
- Returns:
- the JSON number as long.
- Throws:
JsonParser.JsonParsingException
-
getDouble
public double getDouble() throws JsonParser.JsonParsingException
- Returns:
- the JSON number as double.
- Throws:
JsonParser.JsonParsingException
-
getBoolean
public boolean getBoolean() throws JsonParser.JsonParsingException
- Returns:
- the JSON true or false value as boolean.
- Throws:
JsonParser.JsonParsingException
-
getBigDecimal
public java.math.BigDecimal getBigDecimal() throws JsonParser.JsonParsingException
- Returns:
- the JSON number as BigDecimal.
- Throws:
JsonParser.JsonParsingException
-
readJSONArray
public java.lang.String readJSONArray() throws JsonParser.JsonParsingException
This method will advance the parser to the end of array and return the array value as string. To read array elements callreadStringArray()
,readDoubleArray()
,readIntArray()
,readLongArray()
...- Returns:
- string representation of a JSON Array.
- Throws:
JsonParser.JsonParsingException
- if the previous call tonext()
does not returned Event.START_ARRAY
-
readJSONObject
public java.lang.String readJSONObject() throws JsonParser.JsonParsingException
This method will advance the parser to the end of JSON object and return the object value as string.- Returns:
- a string representation of a JSON Object.
- Throws:
JsonParser.JsonParsingException
- if the previous call tonext()
does not returned Event.START_OBJECT
-
close
public void close()
- Specified by:
close
in interfacejava.lang.AutoCloseable
-
writeValueAsIndentedString
public static java.lang.String writeValueAsIndentedString(java.lang.String str) throws JsonParser.JsonParsingException
Receive a JSON String and return a new indented JSON String.- Parameters:
str
-- Returns:
- indented JSON
- Throws:
JsonParser.JsonParsingException
-
getLocation
public JsonParser.JsonLocation getLocation()
- Returns:
- current position of the parser in the JSON input .
-
-