Skip navigation links
Oracle REST Data Services Java API Reference
19.4

F25730-01

Package oracle.dbtools.plugin.api.json.objects

API for creating in memory representations of JSON object graphs (JSONObjects) and for serializing object graphs to JSON representations.

See: Description

Package oracle.dbtools.plugin.api.json.objects Description

API for creating in memory representations of JSON object graphs (JSONObjects) and for serializing object graphs to JSON representations. Developers may find this an easier to use API compared to the JSONStreams stream based API, however since JSONObjects requires storing a representation of an entire JSON document in memory it requires greater memory overhead and is not suitable for parsing or generating documents that may exceed available memory in size. In general, developers are encouraged to use the JSONStreams API in preference to this API. See below for instructions on how to:

Reading a JSON Document

  @Provides
  class SomeService {
    @Inject SomeService(JSONObjects json) {
     this.json = json;
    }
    
    void doSomething(InputStream content) throws IOException {
     JSONObject object = (JSONObject)json.read(content); // assumes root is an object
     ... // process the JSONObject instance
    }
  }
 

Iterating over a JSONObject's properties

   JSONObject object = (JSONObject)json.read(content); // assumes root is an object
   for ( String propertyName: object.propertyNames() ) {
    Object value = object.get(propertyName);
    ... // process the value
   }
 

JSON Value Types

The value will be one of the following:

The instanceof operator can be used to determine which actual type a value is

Iterating over a JSONArray's elements

   JSONArray array = (JSONArray)json.read(content); // assumes root is an array
   for ( Object value: array.values() ) {
    ... // process the value
   }
 

Modifying a JSONObject

JSONObjects are immutable objects, to create a modified JSONObject instance, the JSONObject.Builder type must be used.

   JSONObject existing = ... // acquire a handle to existing JSONObject
   JSONObject modified = existing.modify()
                                 .add("someProperty","someValue")
                                 .remove("someOtherProperty")
                                 .build();
 

Modifying a JSONArray

JSONArrays are immutable objects, to create a modified JSONArray instance, the JSONArray.Builder type must be used.

   JSONArray existing = ... // acquire a handle to existing JSONArray
   JSONArray modified = existing.modify()
                                 .add("someValue")
                                 .remove(0)
                                 .build();
 

Creating JSONObjects and JSONArrays

   JSONObjects json = ... // acquire handle to JSONObjects service
   JSONArray array = json.array().add("something").add(2).addNull().build();
   
   // array => ["something",2,null]
   
   JSONObject object = json.object().add("foo","bar").add("nested",array).build();
   
   // object => {"foo": "bar", "nested": ["something",2,null]}
 

Writing in-memory JSON representations

JSONNode graphs can be written to streams using the JSONObjects.write(Appendable, JSONNode) and JSONObjects.write(java.io.OutputStream, JSONNode) methods.

  java.io.Writer out = ... // acquire handler to Appendable instance
  JSONObjects json = ... // acquire handle to JSONObjects service
  
  // create a JSON graph
  JSONArray array = json.array().add("something").add(2).addNull().build();
  JSONObject object = json.object().add("foo","bar").add("nested",array).build();
  
  // write the graph to the stream
  json.write(out,object);
  
  // out contents => {"foo": "bar", "nested": ["something",2,null]}
 

Serializing Java Object Graphs

Any Java object graph conforming to the rules outlined below may be serialized to a JSON representation using the JSONObjects.write(java.io.OutputStream, Object) method:

Supported Types

The nodes in the object graph can be any of the following sub-types:

Scalar Types

Container Types

Cycles in the graph are not permitted.
Author:
cdivilly
Skip navigation links
Oracle REST Data Services Java API Reference
19.4

F25730-01

Copyright © 2010, 2019, Oracle and/or its affiliates. All rights reserved.