public class SimpleJsonGenerator
extends java.lang.Object
Simple JSON generator. This generator is optimized for simplicity, not performance or richness in features, so code wanting to generate large or complex JSON documents should use a different JSON generator.
This generator has specifically been written in order to minimize the dependencies needed for generating a JSON document. It only uses the Java 8 SE API and the Apache Commons Lang 3 library.
This generator generates JSON document that comply with the JSON (ECMA-404)
standard. Compared to many other generator, this parser is very strict about
compliance. For example, number values that cannot be correctly represented
in JSON (e.g. Double.NaN
) are enclosed in quotes.
This parser serializes Java objects to JSON using the following rules:
null
is serialized to the JSON value
null
.Boolean
is serialized to the JSON values true
or
false
.Number
is serialized using its toString()
method. If the string representation returned by this method is
not a valid JSON number, the return value is serialized to a JSON
string.String
is serialized to a JSON string.Collection
, an array of objects, and any array of primitives is
serialized to a JSON array. Each array element is again subject to the
serialization rules stated in this list.Map
is serialized to a JSON object. If an entries key is a
String
, this string is serialized as a JSON string. If it is
null
, it is serialized as the JSON string "null"
.
If it is any other object, this object's toString()
method is
called and the return value is serialized as a JSON string. Please note that
this generator does not take any precautions against duplicate keys in
objects. Such objects are allowed by the JSON specification, but they are
going to be refused by many parsers (including the
SimpleJsonParser
).SimpleJsonParser
Modifier and Type | Method and Description |
---|---|
static java.lang.String |
generate(java.lang.Object object)
Returns the JSON representation of the specified object.
|
static void |
generate(java.lang.Object object,
java.lang.StringBuilder appendTo)
Generates the JSON representation of the specified object, appending it
to the specified string builder.
|
static void |
generate(java.lang.Object object,
java.io.Writer writer)
Generates the JSON representation of the specified object, writing it to
the specified reader.
|
public static java.lang.String generate(java.lang.Object object)
object
- object to be serialized to JSON. May be null
.public static void generate(java.lang.Object object, java.lang.StringBuilder appendTo)
object
- object to be serialized to JSON. May be null
.appendTo
- string builder to which the string representation shall be
appended.public static void generate(java.lang.Object object, java.io.Writer writer) throws java.io.IOException
Generates the JSON representation of the specified object, writing it to the specified reader. Please refer to the class description for details.
If this method throws an exception, the serialized form may have partially been written to the writer.
object
- object to be serialized to JSON. May be null
.writer
- writer to which the JSON representation of the object is
written.java.io.IOException
- if the writer
throws such an exception.Copyright © 2017–2019 aquenos GmbH. All rights reserved.