Package oracle.sql.json
Interface OracleJsonParser
-
- All Superinterfaces:
AutoCloseable
,Closeable
public interface OracleJsonParser extends Closeable
Reads a JSON type value from an input source as a stream of events. Call
Example:next()
to advance the parser to the next event in the stream and use accessor methods such asgetString()
andgetInt()
to access the data associated with the current event.import java.io.ByteArrayOutputStream; import java.nio.ByteBuffer; import oracle.sql.json.OracleJsonFactory; import oracle.sql.json.OracleJsonGenerator; import oracle.sql.json.OracleJsonParser; import oracle.sql.json.OracleJsonParser.Event; public class JsonParserExample { public static void main(String[] args) { OracleJsonFactory factory = new OracleJsonFactory(); // Generate binary JSON value {"hello":"world","arr":[1,2]} ByteArrayOutputStream out = new ByteArrayOutputStream(); OracleJsonGenerator generator = factory.createJsonBinaryGenerator(out); generator.writeStartObject(); generator.write("hello", "world"); generator.writeStartArray("arr"); generator.write(1); generator.write(2); generator.writeEnd(); generator.writeEnd(); generator.close(); byte[] binaryJson = out.toByteArray(); OracleJsonParser parser = factory.createJsonBinaryParser(ByteBuffer.wrap(binaryJson)); while (parser.hasNext()) { Event e = parser.next(); System.out.println(e); switch (e) { case START_OBJECT: case START_ARRAY: case END_ARRAY: case END_OBJECT: break; // do nothing case KEY_NAME: System.out.println(parser.getString()); break; case VALUE_STRING: System.out.println(parser.getString()); break; case VALUE_DECIMAL: System.out.println(parser.getBigDecimal()); break; default: break; } } parser.close(); } }
Running this example prints:
START_OBJECT KEY_NAME hello VALUE_STRING world KEY_NAME arr START_ARRAY VALUE_DECIMAL 1 VALUE_DECIMAL 2 END_ARRAY END_OBJECT
-
-
Nested Class Summary
Nested Classes Modifier and Type Interface Description static class
OracleJsonParser.Event
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description void
close()
Closes the parser and closes any resources associated with it.OracleJsonArray
getArray()
Returns the current array value and advances the current state to the correspondingEND_ARRAY
event.BigDecimal
getBigDecimal()
Returns the current value as a decimal value.BigInteger
getBigInteger()
Returns a value equal togetBigDecimal().toBigInteger()
.byte[]
getBytes()
Return the current binary value as a byte arrayvoid
getBytes(OutputStream out)
Return the current binary value to the specified output stream.double
getDouble()
Returns current event as a double.java.time.Duration
getDuration()
Return the current interval as a duration.float
getFloat()
Returns current event as a float.int
getInt()
Returns a value equal to getBigDecimal().intValue().java.time.LocalDateTime
getLocalDateTime()
Returns the current date or timestamp as aLocalDateTime
value.long
getLong()
Returns a value equal to getBigDecimal().longValue().OracleJsonObject
getObject()
Returns the current object and advances the current state to the correspondingEND_OBJECT
event.\java.time.OffsetDateTime
getOffsetDateTime()
Returns the current timestamptz as anOffsetDateTime
value.java.time.Period
getPeriod()
Return the current interval as a period.String
getString()
Gets a string for the current event.OracleJsonValue
getValue()
Return the value at the current parser event.boolean
hasNext()
Returns true if there are additional parsing events.boolean
isIntegralNumber()
Returns true if the current event is an integral number.OracleJsonParser.Event
next()
Return the next parsing event.void
skipArray()
Skips the current array value, advancing the parser to the correspondingEND_ARRAY
.void
skipObject()
Skips the current array value, advancing the parser to the correspondingEND_OBJECT
.<T> T
wrap(Class<T> wrapper)
Returns a JSON-P (javax.json.stream) wrapper around this value.
-
-
-
Method Detail
-
hasNext
boolean hasNext()
Returns true if there are additional parsing events.- Returns:
- true if there are more parsing events.
- Throws:
OracleJsonException
- if an io error occursOracleJsonParsingException
- if the JSON is invalid
-
next
OracleJsonParser.Event next()
Return the next parsing event.- Returns:
- the next event
- Throws:
OracleJsonException
- if an io error occursOracleJsonParsingException
- if the JSON is invalidNoSuchElementException
- if there are no more parsing events
-
getString
String getString()
Gets a string for the current event. If the current event isKEY_NAME
this method returns the key value.- Returns:
- the string value
- Throws:
IllegalStateException
- if the current event is notVALUE_STRING
,KEY_NAME
,VALUE_DECIMAL
,VALUE_DOUBLE
,VALUE_FLOAT
,VALUE_BINARY
,VALUE_INTERVALDS
,VALUE_INTERVALYM
,VALUE_DATE
,VALUE_TIMESTAMP
, orVALUE_TIMESTAMPTZ
-
isIntegralNumber
boolean isIntegralNumber()
Returns true if the current event is an integral number. Specifically, this method returns true whengetBigDecimal().scale()
is equal to 0.- Returns:
- true if the current number is an integral number.
- Throws:
IllegalStateException
- if the current event is notVALUE_DECIMAL
,VALUE_DOUBLE
, orVALUE_FLOAT
-
getInt
int getInt()
Returns a value equal to getBigDecimal().intValue().- Returns:
- the int value
- Throws:
IllegalStateException
- if the current event is notVALUE_DECIMAL
,VALUE_DOUBLE
, orVALUE_FLOAT
-
getLong
long getLong()
Returns a value equal to getBigDecimal().longValue().- Returns:
- the long value
- Throws:
IllegalStateException
- if the current event is notVALUE_DECIMAL
,VALUE_DOUBLE
, orVALUE_FLOAT
-
getDouble
double getDouble()
Returns current event as a double.- Returns:
- the double
- Throws:
IllegalStateException
- if the current event is notVALUE_DECIMAL
,VALUE_DOUBLE
, orVALUE_FLOAT
-
getFloat
float getFloat()
Returns current event as a float.- Returns:
- the float
- Throws:
IllegalStateException
- if the current event is notVALUE_DECIMAL
,VALUE_DOUBLE
, orVALUE_FLOAT
-
getBigInteger
BigInteger getBigInteger()
Returns a value equal togetBigDecimal().toBigInteger()
.- Returns:
- the integer
- Throws:
IllegalStateException
- if the current event is notVALUE_DECIMAL
,VALUE_DOUBLE
, orVALUE_FLOAT
-
getBigDecimal
BigDecimal getBigDecimal()
Returns the current value as a decimal value.- Returns:
- the decimal
- Throws:
IllegalStateException
- if the current event is notVALUE_DECIMAL
,VALUE_DOUBLE
, orVALUE_FLOAT
-
getOffsetDateTime
java.time.OffsetDateTime getOffsetDateTime()
Returns the current timestamptz as anOffsetDateTime
value.- Returns:
- the offset date time
- Throws:
IllegalStateException
- if the current event is notVALUE_TIMESTAMPTZ
-
getLocalDateTime
java.time.LocalDateTime getLocalDateTime()
Returns the current date or timestamp as aLocalDateTime
value.- Returns:
- the local date time
- Throws:
IllegalStateException
- if the current event is notVALUE_DATE
orVALUE_TIMESTAMP
-
getPeriod
java.time.Period getPeriod()
Return the current interval as a period.- Returns:
- the period
- Throws:
IllegalStateException
- if the current event is notVALUE_INTERVALYM
-
getDuration
java.time.Duration getDuration()
Return the current interval as a duration.- Returns:
- the duration
- Throws:
IllegalStateException
- if the current event is notVALUE_INTERVALDS
-
getBytes
byte[] getBytes()
Return the current binary value as a byte array- Returns:
- the byte array
- Throws:
IllegalStateException
- if the current event is notVALUE_BINARY
.
-
getBytes
void getBytes(OutputStream out)
Return the current binary value to the specified output stream.- Throws:
IllegalStateException
- if the current event is notVALUE_BINARY
.
-
getValue
OracleJsonValue getValue()
Return the value at the current parser event. If the current event isSTART_ARRAY
, the result is the same as call togetArray()
. If the current event isSTART_OBJECT
, the result is the same as a call to . In other cases, the current value is read and returned.- Returns:
- the value
- Throws:
IllegalStateException
- if the current event isEND_OBJECT
orEND_ARRAY
-
getArray
OracleJsonArray getArray()
Returns the current array value and advances the current state to the correspondingEND_ARRAY
event.- Returns:
- the array value
- Throws:
IllegalStateException
- if the current event is notSTART_ARRAY
-
getObject
OracleJsonObject getObject()
Returns the current object and advances the current state to the correspondingEND_OBJECT
event.\- Returns:
- the object value
- Throws:
IllegalStateException
- if the current event is notSTART_OBJECT
-
skipArray
void skipArray()
Skips the current array value, advancing the parser to the correspondingEND_ARRAY
.- Throws:
IllegalStateException
- if the current event is notSTART_OBJECT
-
skipObject
void skipObject()
Skips the current array value, advancing the parser to the correspondingEND_OBJECT
.- Throws:
IllegalStateException
- if the current event is notSTART_OBJECT
-
wrap
<T> T wrap(Class<T> wrapper)
Returns a JSON-P (javax.json.stream) wrapper around this value. For example:import javax.json.stream.JsonParser; ... OracleJsonParser oraParser = ...; JsonParser parser = oraParser.wrap(JsonParser.class);
The returned object is a logical view of this generator. Any changes to the state of this parser are observed by the returned wrapper object.
- Parameters:
wrapper
- the interface to view this object as. Must be assignable tojavax.json.stream.JsonParser
- Returns:
-
close
void close()
Closes the parser and closes any resources associated with it.- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Throws:
OracleJsonException
- if an i/o error occurs
-
-