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 | Field and Description |
---|---|
static String |
CLASSLOADER
If set, use the specified ClassLoader
|
static String |
CUSTOM_READER_MAP
If set, this maps class ==> CustomReader
|
static String |
CUSTOM_WRITER_MAP
If set, this maps class ==> CustomWriter
|
static String |
DATE_FORMAT
Set the date format to use within the JSON output
|
static String |
ENUM_PUBLIC_ONLY
If set, indicates that private variables of ENUMs are not to be serialized
|
static String |
FAIL_ON_UNKNOWN_TYPE
Will fail JSON parsing if 'type' class defined but is not on classpath.
|
static String |
FIELD_NAME_BLACK_LIST
Set value to a
Map<Class, List<String>> which will be used to control which fields on a class are not output. |
static String |
FIELD_SPECIFIERS
Set value to a
Map<Class, List<String>> which will be used to control which fields on a class are output |
static String |
FORCE_MAP_FORMAT_ARRAY_KEYS_ITEMS
If set to true all maps are transferred to the format @keys[],@items[] regardless of the key_type
|
static String |
ISO_DATE_FORMAT
Constant for use as DATE_FORMAT value
|
static String |
ISO_DATE_TIME_FORMAT
Constant for use as DATE_FORMAT value
|
static String |
MAX_PARSE_DEPTH
Default maximum parsing depth
|
static String |
MISSING_FIELD_HANDLER
If set, this object will be called when a field is present in the JSON but missing from the corresponding class
|
static String |
NOT_CUSTOM_READER_MAP
If set, this indicates that no custom reader should be used for the specified class ==> CustomReader
|
static String |
NOT_CUSTOM_WRITER_MAP
If set, this maps class ==> CustomWriter
|
static String |
PRETTY_PRINT
Force nicely formatted JSON output
|
static String |
SHORT_META_KEYS
If set, then @type -> @t, @keys -> @k, @items -> @i
|
static String |
SKIP_NULL_FIELDS
If set, null fields are not written
|
static String |
TYPE
Force @type always
|
static String |
TYPE_NAME_MAP
If set, this map will be used when writing @type values - allows short-hand abbreviations type names
|
static String |
UNKNOWN_OBJECT
What to do when an object is found and 'type' cannot be determined.
|
static String |
USE_MAPS
If set, the read-in JSON will be turned into a Map of Maps (JsonObject) representation
|
static String |
WRITE_LONGS_AS_STRINGS
If set, longs are written in quotes (Javascript safe)
|
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 ReadOptionsBuilder |
getReadOptionsBuilder(Map<String,Object> optionalArgs)
Convert an old-style Map of options to a ReadOptionsBuilder.
|
static WriteOptionsBuilder |
getWriteOptionsBuilder(Map<String,Object> optionalArgs)
Convert an old-style Map of options to a WriteOptionsBuilder.
|
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 (Map) 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 final String CUSTOM_READER_MAP
public static final String NOT_CUSTOM_READER_MAP
public static final String USE_MAPS
public static final String UNKNOWN_OBJECT
public static final String FAIL_ON_UNKNOWN_TYPE
public static final String TYPE_NAME_MAP
public static final String MISSING_FIELD_HANDLER
public static final String CLASSLOADER
public static final String MAX_PARSE_DEPTH
public static final String CUSTOM_WRITER_MAP
public static final String NOT_CUSTOM_WRITER_MAP
public static final String DATE_FORMAT
public static final String ISO_DATE_FORMAT
public static final String ISO_DATE_TIME_FORMAT
public static final String TYPE
public static final String PRETTY_PRINT
public static final String FIELD_SPECIFIERS
Map<Class, List<String>>
which will be used to control which fields on a class are outputpublic static final String FIELD_NAME_BLACK_LIST
Map<Class, List<String>>
which will be used to control which fields on a class are not output. Black list has always priority to FIELD_SPECIFIERSpublic static final String ENUM_PUBLIC_ONLY
public static final String WRITE_LONGS_AS_STRINGS
public static final String SHORT_META_KEYS
public static final String SKIP_NULL_FIELDS
public static final String FORCE_MAP_FORMAT_ARRAY_KEYS_ITEMS
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)
readOptions
- ReadOptions to control the feature options. Can be null to take the defaults.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
- public static ReadOptionsBuilder getReadOptionsBuilder(Map<String,Object> optionalArgs)
optionalArgs
- Map of old json-io optionspublic static WriteOptionsBuilder getWriteOptionsBuilder(Map<String,Object> optionalArgs)
optionalArgs
- Map of old json-io optionsCopyright © 2024. All rights reserved.