Interface OracleJsonValue

All Known Subinterfaces:
OracleJsonArray, OracleJsonBinary, OracleJsonDate, OracleJsonDecimal, OracleJsonDouble, OracleJsonFloat, OracleJsonIntervalDS, OracleJsonIntervalYM, OracleJsonNumber, OracleJsonObject, OracleJsonString, OracleJsonStructure, OracleJsonTimestamp, OracleJsonTimestampTZ, OracleJsonVector

public interface OracleJsonValue

The interface for JSON type in Oracle Database. This is the super type of all JSON type values: OracleJsonObject, OracleJsonArray, OracleJsonString, OracleJsonDecimal, OracleJsonFloat, OracleJsonDouble, OracleJsonTimestamp, OracleJsonTimestampTZ, OracleJsonDate, OracleJsonBinary, OracleJsonIntervalDS, OracleJsonIntervalYM, OracleJsonValue.TRUE, OracleJsonValue.FALSE, and OracleJsonValue.NULL. Use the method getOracleJsonType() to determine the specific type of a value.

Example:
 
  import oracle.sql.json.OracleJsonArray;
  import oracle.sql.json.OracleJsonDouble;
  import oracle.sql.json.OracleJsonFactory;
  import oracle.sql.json.OracleJsonObject;
  import oracle.sql.json.OracleJsonString;
  import oracle.sql.json.OracleJsonValue;
  import oracle.sql.json.OracleJsonValue.OracleJsonType;
  
  public class JsonValueExample {
  
    public static void main(String[] args) {
  
      OracleJsonFactory factory = new OracleJsonFactory();
  
      OracleJsonArray arr = factory.createArray();
      arr.add(factory.createString("foo"));
      arr.add(factory.createDouble(123.456d));
      
      OracleJsonObject obj = factory.createObject();
      obj.put("hello", "world");
      arr.add(obj);
  
      arr.add(OracleJsonValue.NULL);
      arr.add(OracleJsonValue.TRUE);
      
      System.out.println(arr.toString());
      
      for (OracleJsonValue value : arr) {
        OracleJsonType kind = value.getOracleJsonType();
        System.out.println(kind);
        switch (kind) {
        case DOUBLE:
          OracleJsonDouble jsonDouble = value.asJsonDouble();
          System.out.println(" - " + jsonDouble.doubleValue());
          break;
        case STRING:
          OracleJsonString jsonString = value.asJsonString();
          System.out.println(" - " + jsonString.getString());
          break;
        case OBJECT:
          OracleJsonObject jsonObject = value.asJsonObject();
          System.out.println(" - " + jsonObject.toString());
          break;
        case TRUE:
        case NULL:
          break; // do nothing
        default:
          throw new IllegalStateException("Unexpected");
        }
      }
    }
  }

Running this example prints:

  ["foo",123.456,{"hello":"world"},null,true]
  STRING
   - foo
  DOUBLE
   - 123.456
  OBJECT
   - {"hello":"world"}
  NULL
  TRUE
  
  • Field Details

  • Method Details

    • getOracleJsonType

      OracleJsonValue.OracleJsonType getOracleJsonType()
      Returns the type of this JSON value.
      Returns:
      the value type
    • toString

      String toString()
      Returns the JSON text for this value.
      Overrides:
      toString in class Object
      Returns:
      JSON text
    • wrap

      <T> T wrap(Class<T> wrapper)
      Returns a JSON-P (javax.json) wrapper around this value. For example:
          
            import javax.json.JsonObject;
            ...
            OracleJsonObject oraObject = ...;
            JsonObject  jsonObject = oraObject.wrap(JsonObject.class);
          
          

      The returned object is a logical view of the underlying value. Any changes to the value will be observed by the returned wrapper object. All instances of javax.json.JsonValue produced by JDBC implement the java.sql.Wrapper interface which can be used to map back to an instance of oracle.sql.json.OracleJsonValue. For example:

          
            import javax.json.JsonObject;
            import java.sql.Wrapper
            ...
            JsonObject jsonObject = ...;
            OracleJsonObject oraObject = ((Wrapper)jsonObject).unwrap(OracleJsonObject.class);
          
          

      The following table summarizes the object-model mappings between oracle.sql.json.OracleJsonValue and javax.json.JsonValue.

      oracle.sql.json javax.json
      oracle.sql.json.OracleJsonObject javax.json.JsonObject
      oracle.sql.json.OracleJsonArray javax.json.JsonArray
      oracle.sql.json.OracleJsonString
      oracle.sql.json.OracleJsonTimestamp
      oracle.sql.json.OracleJsonDate
      oracle.sql.json.OracleJsonBinary
      oracle.sql.json.OracleJsonIntervalDS
      oracle.sql.json.OracleJsonIntervalYM
      javax.json.JsonString
      oracle.sql.json.OracleJsonDecimal
      oracle.sql.json.OracleJsonDouble
      oracle.sql.json.OracleJsonFloat
      javax.json.JsonNumber
      oracle.sql.json.OracleJsonValue.TRUE javax.json.JsonValue.TRUE
      oracle.sql.json.OracleJsonValue.FALSE javax.json.JsonValue.FALSE
      oracle.sql.json.OracleJsonValue.NULL javax.json.JsonValue.NULL
      Parameters:
      wrapper - the interface to view this object as. Must be assignable to javax.json.JsonValue
    • asJsonObject

      default OracleJsonObject asJsonObject()
      Returns this value as OracleJsonObject. This method is equivalent to (OracleJsonObject)this.
      Returns:
      the OracleJsonObject
      Throws:
      ClassCastException - if this value is not an instance of OracleJsonObject.
    • asJsonArray

      default OracleJsonArray asJsonArray()
      Returns this value as OracleJsonArray. This method is equivalent to (OracleJsonArray)this.
      Returns:
      the OracleJsonArray
      Throws:
      ClassCastException - if this value is not an instance of OracleJsonArray.
    • asJsonString

      default OracleJsonString asJsonString()
      Returns this value as OracleJsonString. This method is equivalent to (OracleJsonString)this.
      Returns:
      the OracleJsonString
      Throws:
      ClassCastException - if this value is not an instance of OracleJsonString.
    • asJsonDecimal

      default OracleJsonDecimal asJsonDecimal()
      Returns this value as OracleJsonDecimal. This method is equivalent to (OracleJsonDecimal)this.
      Returns:
      the OracleJsonDecimal
      Throws:
      ClassCastException - if this value is not an instance of OracleJsonDecimal.
    • asJsonDouble

      default OracleJsonDouble asJsonDouble()
      Returns this value as OracleJsonDouble. This method is equivalent to (OracleJsonDouble)this.
      Returns:
      the OracleJsonDouble
      Throws:
      ClassCastException - if this value is not an instance of OracleJsonDouble.
    • asJsonFloat

      default OracleJsonFloat asJsonFloat()
      Returns this value as OracleJsonFloat. This method is equivalent to (OracleJsonFloat)this.
      Returns:
      the OracleJsonFloat
      Throws:
      ClassCastException - if this value is not an instance of OracleJsonFloat.
    • asJsonNumber

      default OracleJsonNumber asJsonNumber()
      Returns this value as OracleJsonNumber. This method is equivalent to (OracleJsonNumber)this.
      Returns:
      the OracleJsonNumber
      Throws:
      ClassCastException - if this value is not an instance of OracleJsonNumber.
    • asJsonIntervalDS

      default OracleJsonIntervalDS asJsonIntervalDS()
      Returns this value as OracleJsonIntervalDS. This method is equivalent to (OracleJsonIntervalDS)this.
      Returns:
      the OracleJsonIntervalDS
      Throws:
      ClassCastException - if this value is not an instance of OracleJsonIntervalDS.
    • asJsonIntervalYM

      default OracleJsonIntervalYM asJsonIntervalYM()
      Returns this value as OracleJsonIntervalYM. This method is equivalent to (OracleJsonIntervalYM)this.
      Returns:
      the OracleJsonIntervalYM
      Throws:
      ClassCastException - if this value is not an instance of OracleJsonIntervalYM.
    • asJsonTimestamp

      default OracleJsonTimestamp asJsonTimestamp()
      Returns this value as OracleJsonTimestamp. This method is equivalent to (OracleJsonTimestamp)this.
      Returns:
      the OracleJsonTimestamp
      Throws:
      ClassCastException - if this value is not an instance of OracleJsonTimestamp.
    • asJsonTimestampTZ

      default OracleJsonTimestampTZ asJsonTimestampTZ()
      Returns this value as OracleJsonTimestampTZ. This method is equivalent to (OracleJsonTimestampTZ)this.
      Returns:
      the OracleJsonTimestampTZ
      Throws:
      ClassCastException - if this value is not an instance of OracleJsonTimestampTZ.
    • asJsonDate

      default OracleJsonDate asJsonDate()
      Returns this value as OracleJsonDate. This method is equivalent to (OracleJsonDate)this.
      Returns:
      the OracleJsonDate
      Throws:
      ClassCastException - if this value is not an instance of OracleJsonDate.
    • asJsonBinary

      default OracleJsonBinary asJsonBinary()
      Returns this value as OracleJsonBinary. This method is equivalent to (OracleJsonBinary)this.
      Returns:
      the OracleJsonBinary
      Throws:
      ClassCastException - if this value is not an instance of OracleJsonBinary.
    • asJsonVector

      default OracleJsonVector asJsonVector()
      Returns this value as OracleJsonVector. This method is equivalent to (OracleJsonVector)this.
      Returns:
      the OracleJsonVector
      Throws:
      ClassCastException - if this value is not an instance of OracleJsonVector.