json-io can be used directly on JSON Strings or with Java's Streams.
Example 1: Java object to JSON String
Employee emp;
// Emp fetched from database
String json = JsonIo.toJson(emp, writeOptions); // 'writeOptions' argument discussed in detail below
This example will convert the Employee
instance to a JSON String. If the JsonReader
were used on this String
,
it would reconstitute a Java Employee
instance.
Example 2: String to Java object
String json = // String JSON representing Employee instance
Employee employee = JsonIo.toObjects(json, readOptions, Employee.class); // 'readOptions' argument discussed below
This will convert the JSON String to a Java Object graph.
Example 3: Java Object to OutputStream
Employee emp;
// emp obtained from data store...
JsonIo.toJson(outputStream, emp, writeOptions);
In this example, a Java object is written to an OutputStream
in JSON format. The stream is closed when finished. If
you need to keep the OutputStream
open (e.g. NDJSON), then set writeOptions.closeStream(false).
Example:
WriteOptions writeOptions = new WriteOptions().closeStream(false);
JsonIo.toJson(outputStream, record1, writeOptions);
JsonIo.toJson(outputStream, record2, writeOptions);
...
JsonIo.toJson(outputStream, recordn, writeOptions);
outputStream.close();
Example 4: InputStream
to Java object
Employee emp = JsonIo.toObjects(stream, readOptions, Employee.class);
In this example, anInputStream
is supplying the JSON.
json-io provides the choice to use the generic "Map of Maps" representation of an object, akin to a Javascript
associative array. When reading from a JSON String
orInputStream
of JSON, use JsonIo:
String json = // or InputStream to JSON providing source
ReadOptions readOptions = new ReadOptionsBuilder().returnAsNativeJsonObjects().build();
JsonObject root = JsonIo.toObjects(json, readOptions);
See the ReadOptions
below for the feature control options. In the example above, rather than return the objects
converted into Java classes, you are being returned the raw JSON values being parsed. It is a graph that consists of
all JsonObject
instances, arrays, and primitive types.
When JsonObject
is returned, your root value will represent one of:
JSON object {...}
JsonObject
implements theMap
interface and represents any JSON object {...}. It will respondtrue
toisObject(),
false
toisArray()
, andfalse
toisPrimitive().
JSON array [...]
JsonObject
implements theList
interface and represents any JSON array [...]. It will respondtrue
toisArray(),
false
toisObject(),
andfalse
to isisPrimitive().
JSON primitive (boolean true/false, null, long, double, String).
JsonObject
implements.getValue()
and.setValue().
If the root of the JSON is aString,
Number
(Long
orDouble
),Boolean
, ornull,
not an object { ... } nor an array { ... }, then the value can be obtained by calling.getValue()
on theJsonObject.
It will respondfalse
toisObject()
,false
toisArray()
, andtrue
toisPrimitive().
This JsonObject
representation can be re-written to JSON String or Stream and the output JSON will match the
original input JSON stream. This permits you to receive JSON strings / streams that contain class references which
do not exist in the JVM that is parsing the JSON, to completely read the String/Stream, perhaps manipulate the content,
and then rewrite the String/stream.
Create a newWriteOptions
instance and turn various features on/off using the methods below. Example:
WriteOptions
writeOptions = new WriteOptions().prettyPrint(true).writeLongsAsStrings(true);
JsonWriter.toJson(root, writeOptions);
To pass these toJsonWriter.toJson(root, writeOptions)
set up aWriteOptions
using the feature settings below.
You can view the Javadoc on theWriteOptions
class for detailed information. TheWriteOptions
can be made
read-only by calling the.build()
method. You can have multipleWriteOptions
instances for different scenarios, and safely
re-use them once built (read-only). AWriteOptions
instance can be created from anotherWriteOptions
instance
(use "copy constructor" discussed below).
Please note that if you write your object graph out without showing types, the shape of the graph will still be
maintained. What this means, is that if two different places in your object graph point to the same object, the first
reference will write the actual object (with an id - @id
), the 2nd and later references will write a reference (@ref
)
to the first instance. This will read in just fine with JsonReader.toObjects()
, and the appropriate Map
reference
will be placed in all referenced locations. If reading this in Javascript, make sure to use the included jsonUtil.js
to parse the read in JSON so that it can perform the substitutions of the @ref
's. (See root folder for jsonUtil.js
).
Create newWriteOptions
instances.
- Start with default options and in malleable state.
- Copy all the settings from the passed in 'other'
WriteOptions.
TheWriteOptions
instance starts off in malleable state.
TheClassLoader
in theWriteOptons
is used to turnString
class names intoClass
instances.
- Returns the ClassLoader to resolve String class names when writing JSON.
- Sets the ClassLoader to resolve String class names when writing JSON. Returns
WriteOptions
for chained access.
A few additional fields are sometimes added to a JSON object {...} to give theJsonReader
help in determining what
class to instantiate and load. In addition, if a class is referenced by more than one field, array, collection element, or
Map key or value, then the initial occurrence of the instance will be output with an @id tag and a number n. Subsequent references will
be written as @ref:n. This will significantly shorten the JSON as the object will not be written multiple times, it also
allows for circular reference support, it saves memory when loading from JSON into Java objects, and finally retains
the original shape of the graph that was written (serialized) to JSON.
In a future release, we may be moving from using "@" to "." Backward compatibility will be retained.
- Returns
true
if showing short meta-keys (@i instead of @id, @ instead of @ref, @t instead of @type, @k instead of @keys, @v instead of @values),false
for full size.false
is the default.
- Sets the boolean
true
to turn on short meta-keys,false
for long. ReturnsWriteOptions
for chained access.
Aliasing is used to turn long java package names to simple class names, e.g.java.util.ArrayList
becomesArrayList
in the JSON. By default, json-io has most of the common JDK classes aliased to make the JSON content smaller. You can
add additional aliases for classes in your program.
- Alias Type Names, e.g. "ArrayList" instead of "java.util.ArrayList".
- Returns Map<String, String> containing String class names to alias names. Use this API to see default aliases.
- Puts the
Map
containing String class names to alias names. The passed inMap
will beputAll()
copied overwriting any entries that match values in the passed in Map. New entries in the Map are added. ReturnsWriteOptions
for chained access.
- Sets the alias for a given class name. Returns
WriteOptions
for chained access.
- Sets the alias for a given class. Returns
WriteOptions
for chained access.
Used to provide hint to JsonReader to know what Classes to instantiate. Frequently, json-io can determine what Java type (class) an object is because of a field of an object, the type of array, or if the root class is specified. Sometimes, though the type cannot be inferred. An example would be field on a class that is declared as an 'Object' type but more complex, derived instances are placed there. In that case, json-io will output an @type=typename to indicate the type of the class that the reader should instantiate and load.
Note: The meta properties like @type will soon be output using ".type" although support will be maintained for "@type" going forward. Also note, you can specify "short" meta-keys, and alias type names to further shorten the written JSON.
- Returns
true
if set to always show type (@type).
- Returns
true
if set to never show type (no @type).
- Returns
true
if set to show minimal type (@type). This is the default setting.
- Sets to always show type. Returns
WriteOptions
for chained access.
- Sets to never show type. Returns
WriteOptions
for chained access.
- Sets to show minimal type. This means that when the type of object can be inferred, a type field will not be output. Returns
WriteOptions
for chained access.
In order to have Multiple line, indented JSON output, or one-line output, turn on the pretty-print feature.
- Returns the pretty-print setting,
true
being on, using lots of vertical white-space and indentations,false
will output JSON in one line. The default isfalse.
- Sets the 'prettyPrint' setting,
true
to turn on,false
will turn off. The default setting isfalse.
ReturnsWriteOptions
for chained access.
Sometimes you want to close the stream automatically after output, other times you may want to leave it open to write
additional JSON to the stream. For example, NDJSON is a format of {...}\n{...}{...} To write this format, you can tell
JsonIo
not to close the stream after each write. See example at the beginning of the user guide.
- Returns
true
if set to automatically close stream after write (the default), orfalse
to leave stream open after writing to it.
- Sets the 'closeStream' setting,
true
to turn on,false
will turn off. The default setting isfalse.
ReturnsWriteOptions
for chained access.
Output long values as aString.
This is a fix for sending 17-19 digit long values to JavaScript. Javascript stores
numbers internally as aDouble
(IEEE 754) which cannot retain all 17-19 digits. By sending them as strings, the
values make it to Javascript and can still be displayed correctly. The default is off.
- Returns
true
indicating longs will be written as Strings,false
to write them out as native JSON longs.
- Set to boolean
true
to turn on writing longs as Strings,false
to write them as native JSON longs. The default setting isfalse.
This feature is important to marshal JSON with large long values (18 to 19 digits) to Javascript. Long/long values are represented in Javascript by storing them in aDouble
internally, which cannot represent a fulllong
value. Using this feature allows longs to be sent to Javascript with all their precision, however, they will be Strings when received in the Javascript. This will let you display them correctly, for example. ReturnsWriteOptions
for chained access.
You can turn on this setting (off by default) so that fields that have null values are not output to JSON. For certain applications, this can dramatically reduce the size of the JSON output.
- Returns
true
indicating fields with null values will not be written,false
will still output the field with an associated null value. The default isfalse.
- Sets the boolean where
true
indicates fields with null values will not be written to the JSON,false
will allow the field to still be written. ReturnsWriteOptions
for chained access.
If this feature is turned on (off by default) then the raw parsed types are returned, not the Java object instances. For certain applications, the objects may not be that complex, nor even have references between objects. Working with the raw JsonValues (JSON Objects, JsonArrays, and primitives) may be all that is needed. This will make parsing JSON much faster. Keep in mind that you are working with Maps, Arrays, and primitives, not well typed DTOs.
- Returns
true
if set to force Java Maps to be written out as two parallel arrays, once for keys, one array for values. The default isfalse.
- Sets the boolean 'forceMapOutputAsTwoArrays' setting. If Map's have String keys they are written as normal JSON objects. With this setting enabled, Maps are written as two parallel arrays. Returns
WriteOptions
for chained access.
Although the JSON spec does not support Nan and Infinity, it can be convenient to allow it in the JSON, and have the values be both written and read properly. This feature is off by default. Although the JSON with Nan and Infinity may work for your application, keep in mind, NaN, +Inf, -Inf are not necessarily going to be supported by other APIs and services.
- Returns
true
if set to allow serialization ofNaN
andInfinity
fordoubles
andfloats.
- true will allow
doubles
andfloats
to be output asNaN
andINFINITY
,false
and these values will come across asnull.
ReturnsWriteOptions
for chained access.
Most developers useEnums
as a discrete list of values. However, we have seen instances where additional fields
have been added to theEnum
classes. Additionally, we have seen both public and private fields in these cases.
The Enum options allow you to skip private fields (retain public only), keep the Enum to only it's "name" value, or
force the Enum to be written as an object, or a single value.
- Returns
true
if enums are to be written out as Strings (not a full JSON object) when possible.
- Sets the option to write out enums as a String. Returns
WriteOptions
for chained access.
- Returns
true
indicating that only public fields will be output on an Enum. The default is to only output public fields as well as to write it as a primitive (single value) instead of a JSON { } object when possible.
- Sets the option to write out all the member fields of an enum. Returns
WriteOptions
for chained access.
Want to customize JSON output for a particular class? For any Java class, you can author a custom writer
(JsonClassWriter
) and associate it to the class. Then your 'JsonClassWriter' class will be called on to write the
class out, given you the capability to selectively choose fields, format it differently, etc.
- Establishes the passed in
Map
as the complete list of custom writers to be used when writing JSON. ReturnsWriteOptions
for chained access.
- Adds a custom writer for a specific Class. Returns
WriteOptions
for chained access.
- Returns a
Map
of Class to custom JsonClassWriter's use to write JSON when the class is encountered during serialization.
- Checks to see if there is a custom writer associated with a given class. Returns
true
if there is,false
otherwise.
Customized writers are associated to a particular class AND it's derivatives. If you have a derivative class that is having its output customized and you do not want that, you can add that class to the "Not" customized list. Being on the "Not" customized list takes priority over the customized list, letting the default json-io JSON writer do its job.
- Checks if a class is on the not-customized list. Returns
true
if it is,false
otherwise.
- Returns a
Set
of all Classes on the not-customized list.
- Adds a class to the not-customized list. This class will use 'default' processing when written. This option is available as custom writers apply to the class and their derivatives. This allows you to shut off customization for a class that is picking it up due to inheritance. Returns
WriteOptions
for chained access.
- Initializes the list of classes on the non-customized list. Returns
WriteOptions
for chained access.
This feature allows you to indicate which fields should be output for a particular class. If fields are specified here, only these fields will be output for a particular class. Think of it as a white-list approach to specifying the fields. If a particular class has a large amount of fields and you only want to exclude a few, use 'Excluded' fields instead. If a field is listed in both the includes and excludes list, the exclude list takes precedence. That may change in a future release, and an exception will be thrown if you attempt to add the same field name to both the include and exclude list.
- Returns a
Set
of Strings field names associated to the passed in class to be included in the written JSON.
- Returns a
Map
of all Classes and their associated Sets of fields to be included when serialized to JSON.
- Adds a single field to be included in the written JSON for a specific class. Returns
WriteOptions
for chained access.
- Adds a
Collection
of fields to be included in written JSON for a specific class. ReturnsWriteOptions
for chained access.
- Adds multiple Classes and their associated fields to be included in the written JSON. Returns
WriteOptions
for chained access.
This feature allows you to indicate which fields should be not be output for a particular class. If fields are specified here, then these named fields will not be output for a particular class. Think of it as a black-list approach to specifying the fields. If a particular class has a large amount of fields and you only want to exclude a few, this is a great approach to trim off just those few fields (privacy considerations, ClassLoader fields, etc.) You can also add fields to an 'include' list, in which case only those fields will be output for the particular class. If a field is listed in both the includes and excludes list, the exclude list takes precedence. That may change in a future release, and an exception will be thrown if you attempt to add the same field name to both the include and exclude list.
- Returns a
Set
of Strings field names associated to the passed in class to be excluded in the written JSON.
- Returns a
Map
of all Classes and their associated Sets of fields to be excluded when serialized to JSON.
- Adds a single field to be excluded from the written JSON for a specific class. Returns
WriteOptions
for chained access.
- Adds a
Collection
of fields to be excluded in written JSON for a specific class. ReturnsWriteOptions
for chained access.
- Adds multiple Classes and their associated fields to be excluded from the written JSON. Returns
WriteOptions
for chained access.
This feature allows you to control the format forjava.util.Date and java.sql.Date
fields. The default output format
for these fields is numericlong
format, which is fast and small. This may work great for your application, but not all applications.
You can set the Date format via String format like long ISO date time format, e.g. "yyyy-MM-dd'T'HH:mm:ss". All the standard
JDK formatting options are available for use here. There are convenience methods for short and long ISO date formats.
- Changes the date-time format to the ISO date format: "yyyy-MM-dd". Returns
WriteOptions
for chained access.
- Changes the date-time format to the ISO date-time format: "yyyy-MM-dd'T'HH:mm:ss". Returns
WriteOptions
for chained access.
- Changes the
java.uti.Date
andjava.sql.Date
format output to along,
the number of seconds since Jan 1, 1970 at midnight. For speed, the default format islong.
ReturnsWriteOptions
for chained access.
- Returns
true
ifjava.util.Date
andjava.sql.Date
are being written inlong
(numeric) format.
- Changes the date-time format to the passed in format. Returns
WriteOptions
for chained access.
For small immutable classes, no need to use @id/@ref with them, as they are effectively primitives. All primitives, primitive wrappers, BigInteger, BigDecimal, Atomic*, java.util.Date, String, Class, are automatically marked as non-referenceable. You can add more immutable classes of your own. The side-effect of this is that it could change the "shape" of your graph, if you were counting on two different fields that point to the same String/BigDecimal/Integer to have the same == instance. Rarely a concern, however, if you have a lot of the same value, say Integer(0) and expect all those to collapse to the same value, they won't Each one would be read in as its own instance.
- Checks if a class is non-referenceable. Returns
true
if the passed in class is considered a non-referenceable class.
- Returns a
Collection
of all non-referenceable classes.
- Adds a class to be considered "non-referenceable."
Create a newReadOptions
instance and turn various features on/off using the methods below. Example:
ReadOptions readOptions = new ReadOptionsBuilder().build()
JsonWriter.toJson(root, writeOptions);
To pass these toJsonRead.toJava(root, readOptions)
set up aReadOptions
using the feature settings below.
You can view the Javadoc on theReadOptions
class for detailed information. TheReadOptions
can be made
read-only by calling the.build()
method. You can have multipleReadOptions
instances for different scenarios, and safely
re-use them once built (read-only). AReadOptions
instance can be created from anotherReadOptions
instance
(use "copy constructor" discussed below).
Create new instances ofReadOptions.
- Start with default options and in malleable state.
- Copy all settings from the passed in 'other'
ReadOptions.
TheReadOptions
instance starts off in malleable state.
TheClassLoader
in theReadOptons
is used to turnString
class names intoClass
instances.
- Returns the ClassLoader to resolve String class names into Class instances.
- Sets the ClassLoader to resolve String class names when reading JSON. Returns
ReadOptions
for chained access.
When reading JSON, what do you want to happen when a class name does not exist in your JVM? Should the parsing fail?
This feature allows you to control what happens here. You can tell it to fail on an unknown type (default behavior) and
aJsonIoException
will be thrown. Turn this feature off, and the parser will create aLinkedHashMap
and use that
to store the content from the JSON object being read. If you want, you can set your own class to be created
instead of aLinkedHashMap,
however, it should be noted thatMaps
are useful because any field encountered in the
JSON can beput
into aMap.
- Returns
true
if an 'unknownTypeClass' is set,false
if it is not set
- Set to
true
to indicate that an exception should be thrown if an unknown class type is encountered,false
otherwise. Iffail
isfalse,
then the default class used will beLinkedHashMap
for the particular JSON object encountered. You can set this to your own class using theunknownTypeClass()
feature. ReturnsReadOptions
for chained access.
- Get the Class used to store content when the indicated class cannot be loaded by the JVM. Defaults to
null.
Whennull
,LinkedHashMap
is used. It is usually best to use aMap
derivative, as it will be able to have the various field names encountered to be 'set' into it.
- Set the class to use (defaults to
LinkedHashMap.class
) when a JSON object is encountered and there is no corresponding Java class to instantiate to receive the values. ReturnsReadOptions
for chained access.
Set the maximum object nesting level so that noStackOverflowExceptions
happen. Instead, you will get aJsonIoException
letting
you know that the maximum object nesting level has been reached.
- Return the max nesting depth for JSON {...}
- Set the max depth to allow for nested JSON {...}. Set this to prevent security risk from
StackOverflow
attack- vectors. Returns
ReadOptions
for chained access.
Although the JSON spec does not support Nan and Infinity, it can be convenient to allow it in the JSON, and have the values be both written and read properly. This feature is off by default. Although the JSON with Nan and Infinity may work for your application, keep in mind, NaN, +Inf, -Inf are not necessarily going to be supported by other APIs and services.
- Returns
true
if set to allow serialization ofNaN
andInfinity
fordoubles
andfloats.
- true will allow
doubles
andfloats
to be output asNaN
andINFINITY
,false
and these values will come across asnull.
ReturnsReadOptions
for chained access.
Sometimes you want to close the stream automatically after reading, other times you may want to leave it open to read
additional JSON from the stream. For example, NDJSON is a format of {...}\n{...}{...} To read this format, you can tell
JsonIo
not to close the InputStream
after each read. See example at the beginning of the user guide.
- Returns
true
if set to automatically close stream after read (the default), orfalse
to leave stream open after reading from it.
- Sets the 'closeStream' setting,
true
to turn on,false
will turn off. The default setting isfalse.
ReturnsReadOptions
for chained access.
Aliasing is used to turn long java package names to simple class names, e.g. java.util.ArrayList
becomes ArrayList
in the JSON. By default, json-io has most of the common JDK classes aliased to make the JSON content smaller. You can
add additional aliases for classes in your program.
- Alias Type Names, e.g. "ArrayList" instead of "java.util.ArrayList".
- Returns
Map<String, String>
containing String class names to alias names. Use this API to see default aliases.
- Puts the
Map
containing String class names to alias names. The passed inMap
will beputAll()
copied overwriting any entries that match values in the passed in Map. New entries in the Map are added. ReturnsReadOptions
for chained access.
- Sets the alias for a given class name. Returns
ReadOptions
for chained access.
- Sets the alias for a given class. Returns
ReadOptions
for chained access.
Use this feature to turn classes likejava.util.Collections$UnmodifiableRandomAccessList
into anArrayList
when parsed and loaded
into memory. There are many derivative classes for collection singleton's, synchronized variations of collections, and unmodifiable
variations. All the common JDK substitutions are already in the coercion list. You can add more if needed.
- Return
true
if the passed in Class name is being coerced to another type,false
otherwise.
- Fetch the coerced class for the passed in fully qualified class name.
- Coerce a class from one type (named in the JSON) to another type. For example, converting
java.util.Collections$SingletonSet
to aLinkedHashSet.class.
When an attempt to set a value from the JSON onto a Java object is made, but the field from the JSON is not
available on the Java object, the missing field handler feature comes into play. You can set the handler to be
notified of the missing field. It will be notified of all missing fields at the end of deserialization.
SeeJsonReader.MissingFieldHandler
for more information.
- Fetch the method that will be called when a field in the JSON is read in, yet there is no corresponding field on the destination object to receive the value.
- Pass the
missing field handler
to be called when a field in the JSON is read in, yet there is no corresponding field on the destination object to receive the value.
For a class that is failing to instantiate or be read in correctly by the default processing of json-io, you can
associate aClassFactory
to aClass
to remedy the situation. TheClassFactory's
job is to create instances of the
associated class, and load the values from the JSON into the class from the JSON object being encountered at the
time (it is passed to theClassFactory
). This is the preferred method for custom handling a class as opposed to using aCustomReader.
- Fetch
Map
of all Class's associated toJsonReader.ClassFactory's.
- Get the
ClassFactory
associated to the passed in class.
- Associate multiple ClassFactory instances to Classes that needs help being constructed and read in. The
Map
of entries associateClass
toClassFactory.
TheClassFactory
class knows how to instantiate and load the class associated to it.
- Associate a
ClassFactory
to aClass
that needs help being constructed and read in. If you use this method more than once for the same class, the lastClassFactory
associated to the class will overwrite any prior associations.
Custom readers can be added to load the JSON content associated to a particular class if the default loading is
not working. Before using aCustomReader,
we recommend using a ClassFactory,
as that allows your code to not
only create the appropriate instance, but also to load it at the same time. CustomReaders
are used for
the associated class and any derived classes. If that picks up a class you do not want to associate to a CustomReader,
then can add that class to the Not-Custom-Reader list.
- Fetch
Map
ofClass
to customJsonClassReader's
used to read JSON when the class is encountered during serialization to JSON. This is the entireMap
of Custom Readers.
- Return
true
if there is a custom reader class associated to the passed in class,false
otherwise.
ReadOptions
setCustomReaderClasses(Map<? extends Class>, ? extends JsonReader.JsonClassReader> customReaderClasses
)
- Set all custom readers at once. Pass in a
Map
ofClass
toJsonReader.JsonClassReader.
Set the passed inMap
as the establishedMap
of custom readers to be used when reading JSON. Using this method more than once, will set the custom readers to only the values from theMap
in the last call made.
- Add a single association of a class to a custom reader. If you add another associated reader to the same class, the last one added will overwrite the prior association.
Add a class to this list that you do not want being associated to aCustomReader.
This list is only used when you
want to turn-off aCustomReader
for a particular class that is inadvertently being associated to a CustomReader
through inheritance.
- Pass in Class to check if it's on the not-customized list. Classes are added to this list when a class is being picked up through inheritance, and you don't want it to have a custom reader associated to it.
- Fetch the
Set
of all Classes on the not-customized list.
- Add a class to the not-customized list - the list of classes that you do not want to be picked up by a custom reader (that could happen through inheritance).
- Initialize the list of classes on the non-customized list. All prior associations will be dropped and this
Collection
will establish the new list.
For small immutable classes, no need to use @id/@ref with them, as they are effectively primitives. All primitives, primitive wrappers, BigInteger, BigDecimal, Atomic*, java.util.Date, String, Class, are automatically marked as non-referenceable. You can add more immutable classes of your own. The side-effect of this is that it could change the "shape" of your graph, if you were counting on two different fields that point to the same String/BigDecimal/Integer to have the same == instance. Rarely a concern, however, if you have a lot of the same value, say Integer(0) and expect all those to collapse to the same value, they won't Each one would be read in as its own instance.
- Checks if a class is non-referenceable. Returns
true
if the passed in class is considered a non-referenceable class.
- Returns a
Collection
of all non-referenceable classes.
- Adds a class to be considered "non-referenceable." Examples are the built-in primitives.
Included is a small Javascript utility (jsonUtil.js
in the root folder) that will take a JSON output
stream created by the JSON writer and substitute all@ref's
for the actual pointed to object. It's a one-line
call -resolveRefs(json)
. This will substitute@ref
tags in the JSON for the actual pointed-to (@id
) object.
Even though json-io is great for Java / Javascript serialization, here are some other uses for it:
Many projects use JsonWriter
to write an object to JSON, then use the JsonReader
to read it in, cloning the original object graph:
Employee emp;
// emp obtained from somewhere...
Employee deepCopy = (Employee) Json.Io.deepCopy(emp, null, null); // ReadOptions, WriteOptions can be null
Instead of System.out.println()
debugging, call JsonIo.toJson(obj, writeOptions)
and dump the JSON
string out. That will give you the full referenceable graph dump in JSON. Use the pretty-print feature of WriteOptions
to make the JSON more human-readable.