javax.json
Class JsonWriter

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

public class JsonWriter
extends Object
implements Closeable

A JSON writer that writes a JSON object or array structure to an output source.

For example, an empty JSON object can be written as follows:

 JsonWriter jsonWriter = new JsonWriter(...);
 jsonWriter.writeObject(new JsonBuilder().beginObject().endObject().build());
 jsonWriter.close();
 
It uses JsonGenerator for writing. The generator is created using one of the Json's createGenerator methods

Author:
Jitendra Kotamraju

Constructor Summary
JsonWriter(OutputStream out)
          Creates a JSON writer which can be used to write a JSON object or array structure to the specified byte stream.
JsonWriter(OutputStream out, JsonConfiguration config)
          Creates a JSON writer which can be used to write a JSON object or array structure to the specified byte stream.
JsonWriter(OutputStream out, String encoding)
          Creates a JSON writer which can be used to write a JSON object or array structure to the specified byte stream.
JsonWriter(OutputStream out, String encoding, JsonConfiguration config)
          Creates a JSON writer which can be used to write a JSON object or array structure to the specified byte stream.
JsonWriter(Writer writer)
          Creates a JSON writer which can be used to write a JSON object or array structure to the specified character stream.
JsonWriter(Writer writer, JsonConfiguration config)
          Creates a JSON writer which can be used to write a JSON object or array structure to the specified character stream.
 
Method Summary
 void close()
          Closes this JSON writer and frees any resources associated with the writer.
 void write(JsonStructure value)
          Writes the specified JSON object or array to the output source.
 void writeArray(JsonArray array)
          Writes the specified JSON array to the output source.
 void writeObject(JsonObject object)
          Writes the specified JSON object to the output source.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

JsonWriter

public JsonWriter(Writer writer)
Creates a JSON writer which can be used to write a JSON object or array structure to the specified character stream.

Parameters:
writer - to which JSON object or array is written

JsonWriter

public JsonWriter(Writer writer,
                  JsonConfiguration config)
Creates a JSON writer which can be used to write a JSON object or array structure to the specified character stream. The created writer is configured with the specified configuration.

Parameters:
writer - to which JSON object or array is written
config - configuration of the writer
Throws:
IllegalArgumentException - if a feature in the configuration is not known

JsonWriter

public JsonWriter(OutputStream out)
Creates a JSON writer which can be used to write a JSON object or array structure to the specified byte stream. Characters written to the stream are encoded into bytes using UTF-8 encoding.

Parameters:
out - to which JSON object or array is written

JsonWriter

public JsonWriter(OutputStream out,
                  JsonConfiguration config)
Creates a JSON writer which can be used to write a JSON object or array structure to the specified byte stream. Characters written to the stream are encoded into bytes using UTF-8 encoding. The created writer is configured with the specified configuration.

Parameters:
out - to which JSON object or array is written
config - configuration of the writer
Throws:
IllegalArgumentException - if a feature in the configuration is not known

JsonWriter

public JsonWriter(OutputStream out,
                  String encoding)
Creates a JSON writer which can be used to write a JSON object or array structure to the specified byte stream. Characters written to the stream are encoded into bytes using the specified encoding.

Parameters:
out - to which JSON object or array is written
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

JsonWriter

public JsonWriter(OutputStream out,
                  String encoding,
                  JsonConfiguration config)
Creates a JSON writer which can be used to write a JSON object or array structure to the specified byte stream. Characters written to the stream are encoded into bytes using the specified encoding. The created writer is configured with the specified configuration.

Parameters:
out - to which JSON object or array is written
encoding - the name of character encoding of the stream.
config - configuration of the writer
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

writeArray

public void writeArray(JsonArray array)
Writes the specified JSON array to the output source. This method needs to be called only once for a writer instance.

Parameters:
array - JSON array that is to be written to the output source
Throws:
JsonException - if the specified JSON object cannot be written due to i/o error
IllegalStateException - if this method, writeObject, write or close method is already called

writeObject

public void writeObject(JsonObject object)
Writes the specified JSON object to the output source. This method needs to be called only once for a writer instance.

Parameters:
object - JSON object that is to be written to the output source
Throws:
JsonException - if the specified JSON object cannot be written due to i/o error
IllegalStateException - if this method, writeArray, write or close method is already called

write

public void write(JsonStructure value)
Writes the specified JSON object or array to the output source. This method needs to be called only once for a writer instance.

Parameters:
value - JSON array or object that is to be written to the output source
Throws:
JsonException - if the specified JSON object cannot be written due to i/o error
IllegalStateException - if this method, writeObject, writeArray or close method is already called

close

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

Specified by:
close in interface Closeable



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