Skip navigation links
Oracle REST Data Services Java API Reference
19.4

F25730-01

Package oracle.dbtools.plugin.api.json

JavaScript Object Notation (JSON) APIs for reading and writing JSON character streams.

See: Description

Package oracle.dbtools.plugin.api.json Description

JavaScript Object Notation (JSON) APIs for reading and writing JSON character streams.

Usage

Reading a stream

JSON can be read from a character stream using the JSONStreams.jsonReader(Readable) method.
 @Provides
 class SomePlugin implements SomeService {
  @Inject SomePlugin(JSONStreams json) {
   this.json = json;
  }
 
  @Override
  public void someMethod(java.io.Reader stream) {
   final JSONReader reader = json.jsonReader(stream);
   while ( reader.hasNext() ) {
    JSONToken token = reader.next();
    switch(token.type()) {
     //process JSON tokens here
     ...
    }
   }
   
   private final JSONStreams json; 
  }
 

Reading from a String

JSON can be read from any CharSequence including the String class using the JSONStreams.jsonReader(CharSequence) method.
   final JSONStreams json = ... ;
   String text = ...; // contents of text is a JSON document
   final JSONReader reader = json.jsonReader(text);
   while ( reader.hasNext() ) {
    JSONToken token = reader.next();
    switch(token.type()) {
     //process JSON tokens here
     ...
    }
   }
 

Writing JSON

JSON can be written to any Appendable instance, including StringBuilder and all sub-classes of Writer,using the JSONStreams.jsonWriter(Appendable) method.
   final JSONStreams json = ... ;
   StringBuilder text = new StringBuilder();
   final JSONWriter writer = json.jsonWriter(text);
   writer.startObject().property("foo","bar").endObject();
   System.out.println(text); // emits: {"foo":"bar"}
 

Embedding JSON

To embed one JSON document as the value of a property in another document, the embedded JSON must be parsed into JSONToken instances using JSONReader and then appended to a JSONWriter instance
  JSONStreams json = ...;
  String embedded = "{\"a\":\"b\"}";
  JSONReader reader = json.jsonReader(embedded);
  StringBuilder output = new StringBuilder();
  JSONWriter writer = json.jsonWriter(output);
  writer.startObject();
  writer.propertyName("c");
  // append the nested content
  while (reader.hasNext()) {
   writer.append(reader.next());
  }
  writer.endObject();
  System.out.println(output); // emits: {"c":{"a":"b"}}
 
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.