public class JsonIo extends Object
String json = JsonIo.toJson(JavaObject | JsonObject root, writeOptions)
JsonIo.toJson(OutputStream, JavaObject | JsonObject root, writeOptions)
BillingInfo billInfo = JsonIo.toObjects(String | InputStream, readOptions, BillingInfo.class)
BillingInfo billInfo = JsonIo.toObjects(JsonObject, readOptions, BillingInfo.class)
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 void |
main(String[] args)
Call this method to see all the conversions offered.
|
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 <T> T |
toObjects(InputStream in,
ReadOptions readOptions,
Class<T> rootType)
Convert the passed in JSON to Java Objects.
|
static <T> T |
toObjects(JsonObject jsonObject,
ReadOptions readOptions,
Class<T> rootType)
Convert a root JsonObject that represents parsed JSON, into an actual Java object.
|
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. Can be a JsonObject that was loaded earlier
via .toObjects() with readOptions.returnAsNativeJsonObjects().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. The OutputStream will be closed by default. If
you don't want this, set writeOptions.closeStream(false). This is useful for creating NDJSON,
where multiple JSON objects are written to the stream, separated by a newline.source
- Java instance to convert to JSON format. Can be a JsonObject that was loaded earlier
via .toObjects() with readOptions.returnAsNativeJsonObjects().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. By default, it will be closed. If you don't want
it closed after reading, set readOptions.closeStream(false).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 a @type meta-property that will be used, otherwise a JsonObject will be returned.JsonIoException
- A runtime exception thrown if any errors happen during serializationpublic static <T> T toObjects(JsonObject jsonObject, ReadOptions readOptions, Class<T> rootType)
rootType
- The class that represents, in Java, the root of the underlying JSON from which the JsonObject
was loaded.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.public static void main(String[] args)
args
- Copyright © 2024. All rights reserved.