public final class JsonIo extends Object
OutputStream out = new FileOutputStream(file);
Closeable res = out;
try {
Writer writer = JsonIo.newJsonWriter(out);
res = writer;
// write JSON
} finally {
res.close();
}
To open a stream to read JSON, use:
InputStream in = new FileInputStream(file);
Closeable res = in;
try {
Reader reader = JsonIo.newJsonReader(in);
res = reader;
// read JSON
} finally {
res.close();
}
This type does a best-guess detection of the encoding of a JSON data source
by inspecting the first four bytes as described in RFC 4627.
Note: byte-order-marks are not supported; UTF-32 is not a standard Java
encoding and may no be supported by the runtime.
Streams are intollerant of malformed Unicode data by default.JsonCharsets| Modifier and Type | Method and Description |
|---|---|
static Charset |
detectJsonEncoding(InputStream in)
Detects the character encoding of the given JSON data stream.
|
static Writer |
newCompactJsonWriter(OutputStream out)
As
newCompactJsonWriter(OutputStream, int, CodingErrorAction) with
CodingErrorAction.REPORT and a default buffer size. |
static Writer |
newCompactJsonWriter(OutputStream out,
int bufferSize)
|
static Writer |
newCompactJsonWriter(OutputStream out,
int bufferSize,
CodingErrorAction errorHandling)
A writer that chooses between UTF-8 and UTF-16 by buffering a certain
amount of output and measuring how compact it will be when encoded using
either of those encodings.
|
static Reader |
newJsonReader(InputStream in)
As
newJsonReader(InputStream, CodingErrorAction) with a default of
CodingErrorAction.REPORT. |
static Reader |
newJsonReader(InputStream in,
CodingErrorAction errorHandling)
Detects the JSON encoding and decorates the input with the appropriate
Reader. |
static Writer |
newJsonWriter(OutputStream out)
|
static Writer |
newJsonWriter(OutputStream out,
CodingErrorAction errorHandling)
Creates a UTF-8 writer.
|
public static Reader newJsonReader(InputStream in, CodingErrorAction errorHandling) throws IOException, UnsupportedCharsetException
Reader.in - a JSON sourceerrorHandling - how to handle malformed character dataIOException - on errorUnsupportedCharsetException - UTF-32 is unsupported on Sun Java 5public static Reader newJsonReader(InputStream in) throws IOException, UnsupportedCharsetException
newJsonReader(InputStream, CodingErrorAction) with a default of
CodingErrorAction.REPORT.public static Charset detectJsonEncoding(InputStream in) throws IOException, UnsupportedCharsetException
InputStream.markSupported() is
called.in - a stream that supports markIOException - on errorUnsupportedCharsetException - UTF-32 is unsupported on Sun Java 5JsonCharsetspublic static Writer newJsonWriter(OutputStream out, CodingErrorAction errorHandling) throws IOException
out - the JSON targeterrorHandling - the action on malformed dataIOException - on IO errorpublic static Writer newJsonWriter(OutputStream out) throws IOException
IOExceptionpublic static Writer newCompactJsonWriter(OutputStream out)
newCompactJsonWriter(OutputStream, int, CodingErrorAction) with
CodingErrorAction.REPORT and a default buffer size.public static Writer newCompactJsonWriter(OutputStream out, int bufferSize)
public static Writer newCompactJsonWriter(OutputStream out, int bufferSize, CodingErrorAction errorHandling)
newJsonWriter(OutputStream) instead.
Calling Writer.flush() will cause the measuring phase to finish and
an encoding to be chosen if this has not happened already.out - the data targetbufferSize - the maximum size of the in-memory buffererrorHandling - how to react to malformed dataCopyright © 2014. All Rights Reserved.