public final class OracleJsonFactory extends Object
A factory for reading, writing, and creating SQL JSON values. The methods on this factory fall into three categories:
Description | Methods |
---|---|
Methods for reading and writing Oracle binary JSON | createJsonBinaryGenerator(OutputStream) createJsonBinaryValue(ByteBuffer) createJsonBinaryValue(InputStream) createJsonBinaryParser(ByteBuffer) createJsonBinaryParser(InputStream) |
Methods for creating new instances of the JSON type object-model | createObject() createObject(OracleJsonObject) createArray() createArray(OracleJsonArray) createString(String) createDecimal(BigDecimal) createDecimal(int) createDecimal(long) createDouble(double) createFloat(float) createTimestamp(LocalDateTime) createTimestampTZ(OffsetDateTime) createDate(LocalDateTime) createBinary(byte[]) createIntervalDS(Duration) createIntervalYM(Period) |
Methods for converting values to and from JSON text | createJsonTextGenerator(OutputStream) createJsonTextGenerator(Writer) createJsonTextParser(InputStream) createJsonTextParser(Reader) |
The following example generates Oracle binary JSON for the JSON object
{"hello":"world"}
OracleJsonFactory factory = new OracleJsonFactory();
OracleJsonObject obj = factory.createObject();
obj.put("hello", "world");
ByteArrayOutputStream out = new ByteArrayOutputStream();
JsonGenerator binaryGenerator = factory.createJsonBinaryGenerator(out);
binaryGenerator.write(obj);
binaryGenerator.close();
byte[] binaryJson = out.toByteArray();
Continuing with this example, the Oracle binary JSON can be read as follows:
OracleJsonObject bobj = factory.createJsonBinaryValue(ByteBuffer.wrap(binaryJson));
System.out.println(bobj.getString("hello"));
In this example, the returned object (bobj
) directly references the
underlying Oracle binary JSON (binaryJson
). It does not attempt to
convert the underlying binary JSON to some other internal data structure.
Consequently, the object is immutable and attempts to call mutator methods
such as bobj.put(...)
will raise an error. To make a modifiable copy
of the object, use createObject(OracleJsonObject)
.
The object can be
printed as JSON text using bobj.toString()
or by using a JSON text
generator:
OracleJsonGenerator jsonGenerator = factory.createJsonTextGenerator(System.out);
jsonGenerator.write(bobj);
jsonGenerator.close();
Using a text generator instead of toString()
gives more control over
how the JSON output is written and reuses memory more efficiently.
This factory does not support parsing JSON text. To parse JSON text use
javax.json.Json.createParser()
or another third-party JSON parser.
The following example shows how to convert JSON text to Oracle binary JSON
using a JSON-P parser:
JsonParser parser = Json.createParser(new StringReader("{\"hello\":\"world\"}"));
OracleJsonGenerator binaryGenerator = factory.createJsonBinaryGenerator(out);
binaryGenerator.writeParser(parser);
binaryGenerator.close();
parser.close();
This factory is thread safe but the objects created from it are not. Temporary memory used by parsers and generators may be pooled and reused by this factory. In general, a single factory may serve an entire application but performance may degrade if many threads access the same factory at once. It may be beneficial, for example, to keep multiple thread-local instances.
Constructor and Description |
---|
OracleJsonFactory() |
Modifier and Type | Method and Description |
---|---|
OracleJsonArray |
createArray()
Creates a new mutable JSON array.
|
OracleJsonArray |
createArray(OracleJsonArray other)
Creates a mutable copy of a JSON array.
|
OracleJsonBinary |
createBinary(byte[] value)
Creates a new JSON binary value.
|
OracleJsonValue |
createBoolean(boolean value)
Creates a new JSON boolean value.
|
OracleJsonDate |
createDate(java.time.LocalDateTime i)
Creates a new JSON date value.
|
OracleJsonDecimal |
createDecimal(BigDecimal value)
Creates a new JSON decimal.
|
OracleJsonDecimal |
createDecimal(int value)
Creates a new JSON decimal.
|
OracleJsonDecimal |
createDecimal(long value)
Creates a new JSON decimal.
|
OracleJsonDouble |
createDouble(double value)
Creates a new JSON double.
|
OracleJsonFloat |
createFloat(float value)
Creates a new JSON float.
|
OracleJsonIntervalDS |
createIntervalDS(java.time.Duration d)
Creates a new JSON interval value.
|
OracleJsonIntervalYM |
createIntervalYM(java.time.Period p)
Creates a new JSON interval value.
|
OracleJsonGenerator |
createJsonBinaryGenerator(OutputStream out)
Creates a JSON generator to write binary JSON to a byte stream.
|
OracleJsonParser |
createJsonBinaryParser(ByteBuffer in)
Creates a binary JSON parser from the given buffer.
|
OracleJsonParser |
createJsonBinaryParser(InputStream in)
Creates a binary JSON parser from the given byte
stream.
|
OracleJsonValue |
createJsonBinaryValue(ByteBuffer in)
Creates a
JsonValue from the given binary JSON
buffer. |
OracleJsonValue |
createJsonBinaryValue(InputStream in)
Creates a
OracleJsonValue from the given binary JSON
stream. |
OracleJsonGenerator |
createJsonTextGenerator(OutputStream out)
Creates a JSON generator to write JSON text to a byte
stream.
|
OracleJsonGenerator |
createJsonTextGenerator(Writer out)
Creates a JSON generator to write JSON to a character stream.
|
OracleJsonParser |
createJsonTextParser(InputStream in)
Creates a JSON text parser from the given byte stream.
|
OracleJsonParser |
createJsonTextParser(Reader in)
Creates a JSON text parser from the given character stream.
|
OracleJsonValue |
createJsonTextValue(InputStream in)
Creates a
OracleJsonValue from the given textual JSON
stream. |
OracleJsonValue |
createJsonTextValue(Reader in)
Creates a
OracleJsonValue from the given textual JSON
stream. |
OracleJsonValue |
createNull()
Returns
OracleJsonValue.NULL . |
OracleJsonObject |
createObject()
Creates a new mutable JSON object.
|
OracleJsonObject |
createObject(OracleJsonObject other)
Creates a mutable copy of a JSON object.
|
OracleJsonString |
createString(String value)
Creates a new JSON string.
|
OracleJsonTimestamp |
createTimestamp(java.time.LocalDateTime value)
Creates a new JSON timestamp value.
|
OracleJsonTimestampTZ |
createTimestampTZ(java.time.OffsetDateTime i)
Creates a new JSON timestamp value.
|
OracleJsonValue |
createValue(Datum datum)
Creates a new JSON value from a Datum.
|
public OracleJsonParser createJsonBinaryParser(InputStream in) throws OracleJsonException
in
- stream of binary JSONOracleJsonException
- if an error occurs reading the inputpublic OracleJsonParser createJsonTextParser(InputStream in) throws OracleJsonException
in
- stream of JSON textOracleJsonException
- if an error occurs reading the inputpublic OracleJsonParser createJsonTextParser(Reader in) throws OracleJsonException
in
- stream of JSON textOracleJsonException
- if an error occurs reading the inputpublic OracleJsonParser createJsonBinaryParser(ByteBuffer in) throws OracleJsonException
in
- the buffer containing binary JSONOracleJsonException
- if an error occurs reading the inputpublic OracleJsonValue createJsonBinaryValue(InputStream in) throws OracleJsonException
OracleJsonValue
from the given binary JSON
stream. This is a convenience method that is semantically
equivalent to obtaining a value from a JSON parser as follows:
try (OracleJsonParser parser = factory.createJsonBinaryParser(in)) {
parser.next();
OracleJsonValue value = parser.getValue();
}
This method does close the provided InputStream
.
in
- stream of binary JSONOracleJsonException
- if an error occurs reading the inputpublic OracleJsonValue createJsonTextValue(InputStream in) throws OracleJsonException
OracleJsonValue
from the given textual JSON
stream. This is a convenience method that is semantically
equivalent to obtaining a value from a JSON parser as follows:
try (OracleJsonParser parser = factory.createJsonTextParser(in)) {
parser.next();
OracleJsonValue value = parser.getValue();
}
This method does close the provided InputStream
.
in
- stream of textual JSONOracleJsonException
- if an error occurs reading the inputpublic OracleJsonValue createJsonTextValue(Reader in) throws OracleJsonException
OracleJsonValue
from the given textual JSON
stream. This is a convenience method that is semantically
equivalent to obtaining a value from a JSON parser as follows:
try (OracleJsonParser parser = factory.createJsonTextParser(in)) {
parser.next();
OracleJsonValue value = parser.getValue();
}
This method does close the provided InputStream
.
in
- stream of textual JSONOracleJsonException
- if an error occurs reading the inputpublic OracleJsonValue createJsonBinaryValue(ByteBuffer in) throws OracleJsonException
JsonValue
from the given binary JSON
buffer. This is a convenience method that is semantically
equivalent to obtaining a value from a JSON parser as follows:
try (OracleJsonParser parser = factory.createJsonBinaryParser(in)) {
parser.next();
OracleJsonValue value = parser.getValue();
}
The OracleJsonValue
returned by this function may directly
reference the provide ByteBuffer
. The buffer should not
be modified until returned OracleJsonValue
and all values
derived from it are no longer needed.in
- the buffer containing binary JSONOracleJsonException
- if an error occurs reading the inputpublic final OracleJsonGenerator createJsonBinaryGenerator(OutputStream out)
out
- i/o stream to which binary JSON is writtenpublic OracleJsonGenerator createJsonTextGenerator(OutputStream out)
out
- i/o stream to which UTF8 JSON is writtenpublic OracleJsonGenerator createJsonTextGenerator(Writer out)
out
- character stream to which JSON is writtenpublic OracleJsonObject createObject()
public OracleJsonArray createArray()
public OracleJsonObject createObject(OracleJsonObject other)
other
- the JSON object to copy. May be either mutable or immutable.public OracleJsonArray createArray(OracleJsonArray other)
other
- the JSON array to copy. May be either mutable or immutable.public OracleJsonString createString(String value)
value
- the string valuepublic OracleJsonDecimal createDecimal(BigDecimal value) throws OracleJsonException
value
- the decimal valueOracleJsonException
- if the specified value can not be converted to a JSON number.public OracleJsonDecimal createDecimal(int value)
value
- the value as an integerpublic OracleJsonDecimal createDecimal(long value)
value
- the value as a longpublic OracleJsonFloat createFloat(float value)
value
- the value as a floatpublic OracleJsonDouble createDouble(double value)
value
- the value as a doublepublic OracleJsonBinary createBinary(byte[] value)
value
- the value as a byte arraypublic OracleJsonValue createBoolean(boolean value)
value
- the value as a booleanOracleJsonValue.TRUE
or OracleJsonValue.FALSE
public OracleJsonValue createNull()
OracleJsonValue.NULL
.public OracleJsonTimestamp createTimestamp(java.time.LocalDateTime value)
value
- the timestamp as a LocalDateTimepublic OracleJsonDate createDate(java.time.LocalDateTime i)
value
- the date as a LocalDateTimepublic OracleJsonTimestampTZ createTimestampTZ(java.time.OffsetDateTime i)
value
- the timestamp as a OffsetDateTimepublic OracleJsonIntervalDS createIntervalDS(java.time.Duration d)
value
- the interval as a Durationpublic OracleJsonIntervalYM createIntervalYM(java.time.Period p)
value
- the interval as a Periodpublic OracleJsonValue createValue(Datum datum)
CHAR
, NUMBER
, BINARY_DOUBLE
,
BINARY_FLOAT
, RAW
, DATE
,
TIMESTAMP
, INTERVALDS
, INTERVALYM
,
and OracleJsonDatum
.datum
- the value to convertUnsupportedOperationException
- if the specified Datum type is not supportedOracleJsonException
- if the specified Datum can not be converted to OracleJsonValue