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 `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.

Example:

      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
`static class` `OracleJsonParser.Event`

### Method Summary

All Methods Instance Methods Abstract Methods
`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.

### 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 occurs
  `OracleJsonParsingException` - if the JSON is invalid

#### next

    OracleJsonParser.Event next()

Return the next parsing event.

Returns:
  the next event
Throws:
  `OracleJsonException` - if an io error occurs
  `OracleJsonParsingException` - if the JSON is invalid
  `NoSuchElementException` - if there are no more parsing events

#### getString

    String getString()

Gets a string for the current event. If the current event is `KEY_NAME` this method returns the key value.

Returns:
  the string value
Throws:
  `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`

#### isIntegralNumber

    boolean isIntegralNumber()

Returns true if the current event is an integral number. Specifically, this method returns true when `getBigDecimal().scale()` is equal to 0.

Returns:
  true if the current number is an integral number.
Throws:
  `IllegalStateException` - if the current event is not `VALUE_DECIMAL`, `VALUE_DOUBLE`, or `VALUE_FLOAT`

#### getInt

    int getInt()

Returns a value equal to getBigDecimal().intValue().

Returns:
  the int value
Throws:
  `IllegalStateException` - if the current event is not `VALUE_DECIMAL`, `VALUE_DOUBLE`, or `VALUE_FLOAT`

#### getLong

    long getLong()

Returns a value equal to getBigDecimal().longValue().

Returns:
  the long value
Throws:
  `IllegalStateException` - if the current event is not `VALUE_DECIMAL`, `VALUE_DOUBLE`, or `VALUE_FLOAT`

#### getDouble

    double getDouble()

Returns current event as a double.

Returns:
  the double
Throws:
  `IllegalStateException` - if the current event is not `VALUE_DECIMAL`, `VALUE_DOUBLE`, or `VALUE_FLOAT`

#### getFloat

    float getFloat()

Returns current event as a float.

Returns:
  the float
Throws:
  `IllegalStateException` - if the current event is not `VALUE_DECIMAL`, `VALUE_DOUBLE`, or `VALUE_FLOAT`

#### getBigInteger

    BigInteger getBigInteger()

Returns a value equal to `getBigDecimal().toBigInteger()`.

Returns:
  the integer
Throws:
  `IllegalStateException` - if the current event is not `VALUE_DECIMAL`, `VALUE_DOUBLE`, or `VALUE_FLOAT`

#### getBigDecimal

    BigDecimal getBigDecimal()

Returns the current value as a decimal value.

Returns:
  the decimal
Throws:
  `IllegalStateException` - if the current event is not `VALUE_DECIMAL`, `VALUE_DOUBLE`, or `VALUE_FLOAT`

#### getOffsetDateTime

    java.time.OffsetDateTime getOffsetDateTime()

Returns the current timestamptz as an`OffsetDateTime` value.

Returns:
  the offset date time
Throws:
  `IllegalStateException` - if the current event is not `VALUE_TIMESTAMPTZ`

#### getLocalDateTime

    java.time.LocalDateTime getLocalDateTime()

Returns the current date or timestamp as a `LocalDateTime` value.

Returns:
  the local date time
Throws:
  `IllegalStateException` - if the current event is not `VALUE_DATE` or `VALUE_TIMESTAMP`

#### getPeriod

    java.time.Period getPeriod()

Return the current interval as a period.

Returns:
  the period
Throws:
  `IllegalStateException` - if the current event is not `VALUE_INTERVALYM`

#### getDuration

    java.time.Duration getDuration()

Return the current interval as a duration.

Returns:
  the duration
Throws:
  `IllegalStateException` - if the current event is not `VALUE_INTERVALDS`

#### getBytes

    byte[] getBytes()

Return the current binary value as a byte array

Returns:
  the byte array
Throws:
  `IllegalStateException` - if the current event is not `VALUE_BINARY`.

#### getBytes

    void getBytes(OutputStream out)

Return the current binary value to the specified output stream.

Throws:
  `IllegalStateException` - if the current event is not `VALUE_BINARY`.

#### getValue

    OracleJsonValue getValue()

Return the value at the current parser event. If the current event is `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.

Returns:
  the value
Throws:
  `IllegalStateException` - if the current event is `END_OBJECT` or `END_ARRAY`

#### getArray

    OracleJsonArray getArray()

Returns the current array value and advances the current state to the corresponding `END_ARRAY` event.

Returns:
  the array value
Throws:
  `IllegalStateException` - if the current event is not `START_ARRAY`

#### getObject

    OracleJsonObject getObject()

Returns the current object and advances the current state to the corresponding `END_OBJECT` event.\

Returns:
  the object value
Throws:
  `IllegalStateException` - if the current event is not `START_OBJECT`

#### skipArray

    void skipArray()

Skips the current array value, advancing the parser to the corresponding `END_ARRAY`.

Throws:
  `IllegalStateException` - if the current event is not `START_OBJECT`

#### skipObject

    void skipObject()

Skips the current array value, advancing the parser to the corresponding `END_OBJECT`.

Throws:
  `IllegalStateException` - if the current event is not `START_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 to `javax.json.stream.JsonParser`
Returns:

#### close

    void close()

Closes the parser and closes any resources associated with it.

Specified by:
  `close` in interface `AutoCloseable`
Specified by:
  `close` in interface `Closeable`
Throws:
  `OracleJsonException` - if an i/o error occurs

Copyright © 2008, 2020, Oracle and/or its affiliates. All rights reserved.