Interface OracleJsonParser

  • All Superinterfaces:
    java.lang.AutoCloseable, java.io.Closeable

    public interface OracleJsonParser
    extends java.io.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 
      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 corresponding END_ARRAY event.
      java.math.BigDecimal getBigDecimal()
      Returns the current value as a decimal value.
      java.math.BigInteger getBigInteger()
      Returns a value equal to getBigDecimal().toBigInteger().
      byte[] getBytes()
      Return the current binary value as a byte array
      void getBytes​(java.io.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 anOffsetDateTime value.
      java.time.Period getPeriod()
      Return the current interval as a period.
      java.lang.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​(java.lang.Class<T> wrapper)
      Returns a JSON-P 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
      • getString

        java.lang.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:
        java.lang.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:
        java.lang.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:
        java.lang.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:
        java.lang.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:
        java.lang.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:
        java.lang.IllegalStateException - if the current event is not VALUE_DECIMAL, VALUE_DOUBLE, or VALUE_FLOAT
      • getBigInteger

        java.math.BigInteger getBigInteger()
        Returns a value equal to getBigDecimal().toBigInteger().
        Returns:
        the integer
        Throws:
        java.lang.IllegalStateException - if the current event is not VALUE_DECIMAL, VALUE_DOUBLE, or VALUE_FLOAT
      • getBigDecimal

        java.math.BigDecimal getBigDecimal()
        Returns the current value as a decimal value.
        Returns:
        the decimal
        Throws:
        java.lang.IllegalStateException - if the current event is not VALUE_DECIMAL, VALUE_DOUBLE, or VALUE_FLOAT
      • getOffsetDateTime

        java.time.OffsetDateTime getOffsetDateTime()
        Returns the current timestamptz as anOffsetDateTime value.
        Returns:
        the offset date time
        Throws:
        java.lang.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:
        java.lang.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:
        java.lang.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:
        java.lang.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:
        java.lang.IllegalStateException - if the current event is not VALUE_BINARY.
      • getBytes

        void getBytes​(java.io.OutputStream out)
        Return the current binary value to the specified output stream.
        Throws:
        java.lang.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:
        java.lang.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:
        java.lang.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:
        java.lang.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:
        java.lang.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:
        java.lang.IllegalStateException - if the current event is not START_OBJECT
      • wrap

        <T> T wrap​(java.lang.Class<T> wrapper)
        Returns a JSON-P wrapper around this value. For example:
            
              import jakarta.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 (deprecated) or jakarta.json.stream.JsonParser
        Returns:
      • close

        void close()
        Closes the parser and closes any resources associated with it.
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
        Throws:
        OracleJsonException - if an i/o error occurs