public class JsonWriter extends Object implements WriterContext, Closeable, Flushable
JsonWriter.objectToJson(employee)
. This will
convert the passed in 'employee' instance into a JSON String.JsonWriter writer = new JsonWriter(stream); writer.write(employee); writer.close();This will write the 'employee' object to the passed in OutputStream.
That's it. This can be used as a debugging tool. Output an object
graph using the above code. Use the JsonWriter PRETTY_PRINT option to
format the JSON to be human-readable.
This will output any object graph deeply (or null). Object references are properly handled. For example, if you had A->B, B->C, and C->A, then A will be serialized with a B object in it, B will be serialized with a C object in it, and then C will be serialized with a reference to A (ref), not a redefinition of A.
Modifier and Type | Class and Description |
---|---|
static interface |
JsonWriter.JsonClassWriter
Implement this interface to customize the JSON output for a given class.
|
static interface |
JsonWriter.JsonClassWriterBase
Deprecated.
|
Constructor and Description |
---|
JsonWriter(OutputStream out) |
JsonWriter(OutputStream out,
WriteOptions writeOptions) |
Modifier and Type | Method and Description |
---|---|
void |
addNotCustomWriter(Class<?> c)
Deprecated.
|
void |
close() |
static boolean |
ensureJsonPrimitiveKeys(Map map)
Ensure that all keys within the Map are String instances
|
void |
flush() |
static String |
formatJson(String json)
Deprecated.
use JsonUtilities.formatJson(json);
|
static String |
formatJson(String json,
Map readingArgs,
WriteOptions writeOptions)
Deprecated.
use JsonUtilities.formatJson(json, readOption, writeOptions);
|
void |
newLine()
Add newline (\n) to output
|
static String |
objectToJson(Object item)
Deprecated.
|
static String |
objectToJson(Object item,
WriteOptions writeOptions)
Deprecated.
|
void |
tabIn()
Tab the output left (less indented)
|
void |
tabOut()
Tab the output right (more indented)
|
static String |
toJson(Object item,
WriteOptions writeOptions)
Convert a Java Object to a JSON String.
|
static void |
toJson(OutputStream stream,
Object item,
WriteOptions writeOptions)
Convert a Java Object to a JSON String.
|
protected void |
traceFields(Deque<Object> stack,
Object obj)
Reach-ability trace to visit all objects within the graph to be written.
|
protected void |
traceReferences(Object root)
Walk object graph and visit each instance, following each field, each Collection, Map and so on.
|
void |
write(Object obj)
Write the passed in Java object in JSON format.
|
boolean |
writeArrayElementIfMatching(Class<?> arrayComponentClass,
Object o,
boolean showType,
Writer output)
Write the passed in array element to the JSON output, if any only if, there is a custom writer
for the class of the instance 'o'.
|
protected boolean |
writeCustom(Class<?> arrayComponentClass,
Object o,
boolean showType,
Writer output)
Perform the actual custom writing for an array element that has a custom writer.
|
void |
writeImpl(Object obj,
boolean showType)
Main entry point (mostly used internally, but may be called from a Custom JSON writer).
|
void |
writeImpl(Object obj,
boolean showType,
boolean allowRef,
boolean allowCustom)
Main entry point (mostly used internally, but may be called from a Custom JSON writer).
|
void |
writeObject(Object obj,
boolean showType,
boolean bodyOnly)
Allows you to use the current JsonWriter to write an object out.
|
boolean |
writeUsingCustomWriter(Object o,
boolean showType,
Writer output)
Write the passed in object (o) to the JSON output stream, if and only if, there is a custom
writer associated to the Class of object (o).
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
getWriteOptions
public JsonWriter(OutputStream out)
out
- OutputStream to which the JSON will be written. Uses the default WriteOptions.WriteOptions
public JsonWriter(OutputStream out, WriteOptions writeOptions)
out
- OutputStream to which the JSON output will be written.writeOptions
- WriteOptions containing many feature options to control the JSON output. Can be null,
in which case the default WriteOptions will be used.Javadoc.
@Deprecated public static String objectToJson(Object item)
item
- Object (root) to serialized to JSON String.toJson(Object, WriteOptions)
@Deprecated public static String objectToJson(Object item, WriteOptions writeOptions)
item
- Object to convert to a JSON String.writeOptions
- Feature arguments indicating how dates are formatted,
what fields are written out (optional).WriteOptions
public static String toJson(Object item, WriteOptions writeOptions)
item
- Object to convert to a JSON String.writeOptions
- (optional can be null for defaults) Map of extra arguments indicating how dates are
formatted and what fields are written out (optional). For Date parameters, use the public
static DATE_TIME key, and then use the ISO_DATE or ISO_DATE_TIME indicators. Or you can specify
your own custom SimpleDateFormat String, or you can associate a SimpleDateFormat object,
in which case it will be used. This setting is for both java.util.Date and java.sql.Date.
If the DATE_FORMAT key is not used, then dates will be formatted as longs. This long can
be turned back into a date by using 'new Date(longValue)'.public static void toJson(OutputStream stream, Object item, WriteOptions writeOptions)
stream
- Outputstream that the generated JSON will be sent to.item
- Object root object from which to generate JSON .writeOptions
- (optional can be null for defaults) Map of extra arguments indicating how the JSON is
formatted. From date formats, to special treatment for longs, null fields, etc.@Deprecated public static String formatJson(String json)
json
- String input JSON@Deprecated public static String formatJson(String json, Map readingArgs, WriteOptions writeOptions)
json
- String input JSONreadingArgs
- (optional) Map of extra arguments for parsing json. Can be null.writeOptions
- (optional) Map of extra arguments for writing out json. Can be null.public void tabIn() throws IOException
IOException
public void newLine() throws IOException
IOException
public void tabOut() throws IOException
IOException
public boolean writeUsingCustomWriter(Object o, boolean showType, Writer output)
o
- Object to be (potentially written)showType
- boolean indicating whether to show @type.output
- Writer where the actual JSON is being written to.public boolean writeArrayElementIfMatching(Class<?> arrayComponentClass, Object o, boolean showType, Writer output)
arrayComponentClass
- Class type of the arrayo
- Object instance to writeshowType
- boolean indicating whether @type should be output.output
- Writer to write the JSON to (if there is a custom writer for o's Class).protected boolean writeCustom(Class<?> arrayComponentClass, Object o, boolean showType, Writer output) throws IOException
arrayComponentClass
- Class type of the arrayo
- Object instance to writeshowType
- boolean indicating whether @type should be output.output
- Writer to write the JSON to (if there is a custom writer for o's Class).IOException
@Deprecated public void addNotCustomWriter(Class<?> c)
c
- Class which should NOT have any custom writer associated to it. Use this
to prevent a custom writer from being used due to inheritance.public void write(Object obj)
obj
- Object any Java Object or JsonObject.protected void traceReferences(Object root)
root
- Object to be deeply traced. The objVisited and objsReferenced Maps will be written to
during the trace.protected void traceFields(Deque<Object> stack, Object obj)
stack
- Deque used to manage descent into graph (rather than using Java stack.) This allows for
much larger graph processing.obj
- Object root of graph
the JDK reflection operations. This allows a subset of the actual fields on an object to be serialized.public void writeImpl(Object obj, boolean showType) throws IOException
obj
- Object to be writtenshowType
- if set to true, the @type tag will be output. If false, it will be
dropped.IOException
- if one occurs on the underlying output stream.public void writeImpl(Object obj, boolean showType, boolean allowRef, boolean allowCustom) throws IOException
obj
- Object to be writtenshowType
- if set to true, the @type tag will be output. If false, it will beallowRef
- if set to true, @ref will be used, otherwise 2+ occurrence will be
output as full object.allowCustom
- if set to true, the object being called will be checked for a matching
custom writer to be used. This does not affect sub-objects, just the top-level 'obj'
being passed in.IOException
- if one occurs on the underlying output stream.public static boolean ensureJsonPrimitiveKeys(Map map)
map
- Map to inspect that all keys are primitive. This allows the output JSON
to be optimized into {"key1":value1, "key2": value2} format if all the
keys of the Map are Strings. If not, then a Map is written as two
arrays, a @keys array and an @items array. This allows support for Maps
with non-String keys.public void writeObject(Object obj, boolean showType, boolean bodyOnly) throws IOException
WriterContext
writeObject
in interface WriterContext
obj
- Object to be written in JSON formatshowType
- boolean true means show the "@type" field, false
eliminates it. Many times the type can be dropped because it can be
inferred from the field or array type.bodyOnly
- write only the body of the objectIOException
- if an error occurs writing to the output stream.public void close()
close
in interface Closeable
close
in interface AutoCloseable
Copyright © 2023. All rights reserved.