public class JsonIo extends Object
String json = JsonIo.toJson(root, writeOptions)
JsonIo.toJson(OutputStream, root, writeOptions)
BillingInfo billInfo = JsonIo.toObjects(String | InputStream, readOptions, BillingInfo.class)
JsonObject JsonIo.toJsonValues(String | InputStream, readOptions)
Map
interface to make it easy to put/get values to/from it. List
interface, making it
easy to set/get values to/from it.Constructor and Description |
---|
JsonIo() |
Modifier and Type | Method and Description |
---|---|
static <T> T |
deepCopy(Object source,
ReadOptions readOptions,
WriteOptions writeOptions)
Copy an object graph using JSON.
|
static String |
formatJson(String json)
Format the passed in JSON into multi-line, indented format, commonly used in JSON online editors.
|
static String |
formatJson(String json,
ReadOptions readOptions,
WriteOptions writeOptions)
Format the passed in JSON into multi-line, indented format, commonly used in JSON online editors.
|
static String |
toJson(Object srcObject,
WriteOptions writeOptions)
Convert the passed in Java source object to JSON.
|
static void |
toJson(OutputStream out,
Object source,
WriteOptions writeOptions)
Convert the passed in Java source object to JSON.
|
static JsonObject |
toJsonValues(InputStream inputStream,
ReadOptions readOptions)
Note that the return type will match one of these JSON types: JsonObject, JsonArray, or JsonPrimitive, all
of which implement JsonValue.
|
static JsonObject |
toJsonValues(String json,
ReadOptions readOptions)
Note that the return type will match one of these JSON types: JsonObject, JsonArray, or JsonPrimitive, all
of which implement JsonValue.
|
static <T> T |
toObjects(InputStream in,
ReadOptions readOptions,
Class<T> rootType)
Convert the passed in JSON to Java Objects.
|
static <T> T |
toObjects(String json,
ReadOptions readOptions,
Class<T> rootType)
Convert the passed in JSON to Java Objects.
|
public static String toJson(Object srcObject, WriteOptions writeOptions)
srcObject
- Java instance to convert to JSON format.writeOptions
- Feature options settings to control the JSON output. Can be null,
in which case, default settings will be used.JsonIoException
- A runtime exception thrown if any errors happen during serializationpublic static void toJson(OutputStream out, Object source, WriteOptions writeOptions)
ByteArrayOutputStream baos = new ByteArrayOutputStream(originalOutputStream);
JsonIo.toJson(baos, source, writeOptions);
baos.flush();
String json = new String(baos.toByteArray(), StandardCharsets.UTF_8);
out
- OutputStream destination for the JSON output.source
- Root Java object to begin creating the JSON.writeOptions
- Feature options settings to control the JSON output. Can be null,
in which case, default settings will be used.JsonIoException
- A runtime exception thrown if any errors happen during serializationpublic static <T> T toObjects(String json, ReadOptions readOptions, Class<T> rootType)
json
- String containing JSON content.readOptions
- Feature options settings to control the JSON processing. Can be null,
in which case, default settings will be used.rootType
- Class of the root type of object that will be returned. Can be null, in which
case a best-guess will be made for the Class type of the return object. If it
has an @type meta-property that will be used, otherwise the JSON types { ... }
will return a Map, [...] will return Object[] or Collection, and the primtive
types will be returned (String, long, Double, boolean, or null).JsonIoException
- A runtime exception thrown if any errors happen during serializationpublic static <T> T toObjects(InputStream in, ReadOptions readOptions, Class<T> rootType)
in
- InputStream bringing JSON content.readOptions
- Feature options settings to control the JSON processing. Can be null,
in which case, default settings will be used.rootType
- Class of the root type of object that will be returned. Can be null, in which
case a best-guess will be made for the Class type of the return object. If it
has an @type meta-property that will be used, otherwise the JSON types { ... }
will return a Map, [...] will return Object[] or Collection, and the primtive
types will be returned (String, long, double, boolean, or null).JsonIoException
- A runtime exception thrown if any errors happen during serializationpublic static JsonObject toJsonValues(String json, ReadOptions readOptions)
json
- json stringreadOptions
- options to use when reading. Can be null, in which case the defaults will be used.public static JsonObject toJsonValues(InputStream inputStream, ReadOptions readOptions)
inputStream
- bytes representing UTF-8 stringreadOptions
- options to use when reading. Can be null, in which case the defaults will be used.public static String formatJson(String json, ReadOptions readOptions, WriteOptions writeOptions)
readOptions
- ReadOptions to control the feature options. Can be null to take the defaults.writeOptions
- WriteOptions to control the feature options. Can be null to take the defaults.json
- String JSON content.public static String formatJson(String json)
json
- String JSON content.public static <T> T deepCopy(Object source, ReadOptions readOptions, WriteOptions writeOptions)
source
- Object root object to copyreadOptions
- ReadOptions feature settings. Can be null for default ReadOptions.writeOptions
- WriteOptions feature settings. Can be null for default WriteOptions.Copyright © 2023. All rights reserved.