public interface OracleJsonParser extends Closeable
Reads a JSON type value from an input source as a stream of
events. Call next()
to advance the parser to the next
event in the stream and use accessor methods such as getString()
and getInt()
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
Modifier and Type | Interface and Description |
---|---|
static class |
OracleJsonParser.Event |
Modifier and Type | Method and 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 corresponding
END_ARRAY event. |
BigDecimal |
getBigDecimal()
Returns the current value as a decimal value.
|
BigInteger |
getBigInteger()
Returns a value equal to
getBigDecimal().toBigInteger() . |
byte[] |
getBytes()
Return the current binary value as a byte array
|
void |
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 a
LocalDateTime value. |
long |
getLong()
Returns a value equal to getBigDecimal().longValue().
|
OracleJsonObject |
getObject()
Returns the current object and advances the current state to the
corresponding
END_OBJECT event.\ |
java.time.OffsetDateTime |
getOffsetDateTime()
Returns the current timestamptz as an
OffsetDateTime 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
corresponding
END_ARRAY . |
void |
skipObject()
Skips the current array value, advancing the parser to the
corresponding
END_OBJECT . |
<T> T |
wrap(Class<T> wrapper)
Returns a JSON-P (javax.json.stream) wrapper around this value.
|
boolean hasNext()
OracleJsonException
- if an io error occursOracleJsonParsingException
- if the JSON is invalidOracleJsonParser.Event next()
OracleJsonException
- if an io error occursOracleJsonParsingException
- if the JSON is invalidNoSuchElementException
- if there are no more parsing eventsString getString()
KEY_NAME
this method returns the key value.IllegalStateException
- if the current event is not
VALUE_STRING
, KEY_NAME
, VALUE_DECIMAL
,
VALUE_DOUBLE
, VALUE_FLOAT
, VALUE_BINARY
,
VALUE_INTERVALDS
, VALUE_INTERVALYM
, VALUE_DATE
, VALUE_TIMESTAMP
, or
VALUE_TIMESTAMPTZ
boolean isIntegralNumber()
getBigDecimal().scale()
is equal to 0.IllegalStateException
- if the current event is not
VALUE_DECIMAL
, VALUE_DOUBLE
, or VALUE_FLOAT
int getInt()
IllegalStateException
- if the current event is not
VALUE_DECIMAL
, VALUE_DOUBLE
, or VALUE_FLOAT
long getLong()
IllegalStateException
- if the current event is not
VALUE_DECIMAL
, VALUE_DOUBLE
, or VALUE_FLOAT
double getDouble()
IllegalStateException
- if the current event is not
VALUE_DECIMAL
, VALUE_DOUBLE
, or VALUE_FLOAT
float getFloat()
IllegalStateException
- if the current event is not
VALUE_DECIMAL
, VALUE_DOUBLE
, or VALUE_FLOAT
BigInteger getBigInteger()
getBigDecimal().toBigInteger()
.IllegalStateException
- if the current event is not
VALUE_DECIMAL
, VALUE_DOUBLE
, or VALUE_FLOAT
BigDecimal getBigDecimal()
IllegalStateException
- if the current event is not
VALUE_DECIMAL
, VALUE_DOUBLE
, or VALUE_FLOAT
java.time.OffsetDateTime getOffsetDateTime()
OffsetDateTime
value.IllegalStateException
- if the current event is not
VALUE_TIMESTAMPTZ
java.time.LocalDateTime getLocalDateTime()
LocalDateTime
value.IllegalStateException
- if the current event is not
VALUE_DATE
or VALUE_TIMESTAMP
java.time.Period getPeriod()
IllegalStateException
- if the current event is not
VALUE_INTERVALYM
java.time.Duration getDuration()
IllegalStateException
- if the current event is not
VALUE_INTERVALDS
byte[] getBytes()
IllegalStateException
- if the current event is not
VALUE_BINARY
.void getBytes(OutputStream out)
IllegalStateException
- if the current event is not
VALUE_BINARY
.OracleJsonValue getValue()
START_ARRAY
, the result is the same as call to
getArray()
. If the current event is START_OBJECT
,
the result is the same as a call to . In other cases,
the current value is read and returned.IllegalStateException
- if the current event is
END_OBJECT
or END_ARRAY
OracleJsonArray getArray()
END_ARRAY
event.IllegalStateException
- if the current event is not
START_ARRAY
OracleJsonObject getObject()
END_OBJECT
event.\IllegalStateException
- if the current event is not
START_OBJECT
void skipArray()
END_ARRAY
.IllegalStateException
- if the current event is not
START_OBJECT
void skipObject()
END_OBJECT
.IllegalStateException
- if the current event is not
START_OBJECT
<T> T wrap(Class<T> wrapper)
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.
wrapper
- the interface to view this object as. Must be assignable to
javax.json.stream.JsonParser
void close()
close
in interface AutoCloseable
close
in interface Closeable
OracleJsonException
- if an i/o error occurs