javax.json
Class JsonReader

java.lang.Object
  extended by javax.json.JsonReader
All Implemented Interfaces:
Closeable

public class JsonReader
extends Object
implements Closeable

A JSON reader that reads a JSON object or array from an input source.

For example, an empty JSON array can be created as follows:

 JsonReader jsonReader = new JsonReader(new StringReader("[]"));
 JsonArray array = jsonReader.readArray();
 jsonReader.close();
 
It uses JsonParser for parsing. The parser is created using one of the Json's createParser methods.

Author:
Jitendra Kotamraju

Constructor Summary
JsonReader(InputStream in)
          Creates a JSON reader from a byte stream.
JsonReader(InputStream in, String encoding)
          Creates a JSON reader from a byte stream.
JsonReader(InputStream in, String encoding, JsonConfiguration config)
          Creates a JSON reader from a byte stream.
JsonReader(Reader reader)
          Creates a JSON reader from a character stream
JsonReader(Reader reader, JsonConfiguration config)
          Creates a JSON reader from a character stream
 
Method Summary
 void close()
          Closes this reader and frees any resources associated with the reader.
 JsonStructure read()
          Returns a JSON array or object that is represented in the input source.
 JsonArray readArray()
          Returns a JSON array that is represented in the input source.
 JsonObject readObject()
          Returns a JSON object that is represented in the input source.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

JsonReader

public JsonReader(Reader reader)
Creates a JSON reader from a character stream

Parameters:
reader - a reader from which JSON is to be read

JsonReader

public JsonReader(Reader reader,
                  JsonConfiguration config)
Creates a JSON reader from a character stream

Parameters:
reader - a character stream from which JSON is to be read
config - configuration of the reader
Throws:
IllegalArgumentException - if a feature in the configuration is not known

JsonReader

public JsonReader(InputStream in)
Creates a JSON reader from a byte stream. The character encoding of the stream is determined as per the RFC.

Parameters:
in - a byte stream from which JSON is to be read

JsonReader

public JsonReader(InputStream in,
                  String encoding)
Creates a JSON reader from a byte stream. The bytes of the stream are decoded to characters using the specified encoding.

Parameters:
in - a byte stream from which JSON is to be read
encoding - the name of character encoding of the stream.
Throws:
JsonException - if the named encoding is not supported. The cause of the exception would be UnsupportedEncodingException
See Also:
Charset

JsonReader

public JsonReader(InputStream in,
                  String encoding,
                  JsonConfiguration config)
Creates a JSON reader from a byte stream. The bytes of the stream are decoded to characters using the specified encoding. The created reader is configured with the specified configuration.

Parameters:
in - a byte stream from which JSON is to be read
encoding - the name of character encoding of the stream.
config - configuration of the reader
Throws:
JsonException - if the named encoding is not supported. The cause of the exception would be UnsupportedEncodingException
IllegalArgumentException - if a feature in the configuration is not known
See Also:
Charset
Method Detail

read

public JsonStructure read()
Returns a JSON array or object that is represented in the input source. This method needs to be called only once for a reader instance.

Returns:
a Json object or array
Throws:
JsonException - if a JSON object or array cannot be created due to i/o error or incorrect representation
IllegalStateException - if this method, readObject, readArray or close method is already called

readObject

public JsonObject readObject()
Returns a JSON object that is represented in the input source. This method needs to be called only once for a reader instance.

Returns:
a Json object
Throws:
JsonException - if a JSON object or array cannot be created due to i/o error or incorrect representation
IllegalStateException - if this method, readObject, readArray or close method is already called

readArray

public JsonArray readArray()
Returns a JSON array that is represented in the input source. This method needs to be called only once for a reader instance.

Returns:
a Json array
Throws:
JsonException - if a JSON object or array cannot be created due to i/o error or incorrect representation
IllegalStateException - if this method, readObject, readArray or close method is already called

close

public void close()
Closes this reader and frees any resources associated with the reader. This doesn't close the underlying input source.

Specified by:
close in interface Closeable



Copyright © 2012 Oracle and/or its affiliates. All rights reserved.