public class JsonWriter extends Object implements 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. You can copy that JSON output into this site
which formats it with a lot of whitespace to make it human readable:
http://jsonformatter.curiousconcept.com
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 custom the JSON output for a given class.
|
static interface |
JsonWriter.JsonClassWriterBase |
static interface |
JsonWriter.JsonClassWriterEx
Implement this interface to custom the JSON output for a given class.
|
Modifier and Type | Field and Description |
---|---|
static String |
CUSTOM_WRITER_MAP |
static String |
DATE_FORMAT |
static String |
ENUM_PUBLIC_ONLY |
static String |
FIELD_SPECIFIERS |
static String |
ISO_DATE_FORMAT |
static String |
ISO_DATE_TIME_FORMAT |
static String |
NOT_CUSTOM_WRITER_MAP |
static String |
PRETTY_PRINT |
static String |
SHORT_META_KEYS |
static String |
TYPE |
static String |
TYPE_NAME_MAP |
static String |
WRITE_LONGS_AS_STRINGS |
Constructor and Description |
---|
JsonWriter(OutputStream out) |
JsonWriter(OutputStream out,
Map<String,Object> optionalArgs) |
Modifier and Type | Method and Description |
---|---|
void |
addNotCustomWriter(Class c)
For no custom writing to occur for the passed in Class.
|
void |
addWriter(Class c,
JsonWriter.JsonClassWriterBase writer)
Add a custom writer which will manage writing objects of the
passed in Class in JSON format.
|
void |
close() |
static boolean |
ensureJsonPrimitiveKeys(Map map) |
void |
flush() |
static String |
formatJson(String json)
Format the passed in JSON string in a nice, human readable format.
|
protected Map |
getObjectsReferenced()
Provide access to subclasses.
|
protected Map |
getObjectsVisited()
Provide access to subclasses.
|
protected String |
getSubstituteTypeName(String typeName) |
protected String |
getSubstituteTypeNameIfExists(String typeName) |
boolean |
getWriteLongsAsStrings() |
boolean |
isPrettyPrint() |
boolean |
isPublicEnumsOnly() |
protected void |
newLine() |
static String |
objectToJson(Object item) |
static String |
objectToJson(Object item,
Map<String,Object> optionalArgs)
Convert a Java Object to a JSON String.
|
protected void |
tabIn() |
protected void |
tabOut() |
protected void |
traceFields(Deque<Object> stack,
Object obj,
Map<Class,List<Field>> fieldSpecifiers)
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) |
protected boolean |
writeCustom(Class arrayComponentClass,
Object o,
boolean showType,
Writer output) |
boolean |
writeIfMatching(Object o,
boolean showType,
Writer output) |
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).
|
static void |
writeJsonUtf8String(String s,
Writer output)
Write out special characters "\b, \f, \t, \n, \r", as such, backslash as \\
quote as \" and values less than an ASCII space (20hex) as "\\u00xx" format,
characters in the range of ASCII space to a '~' as ASCII, and anything higher in UTF-8.
|
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
public static final String ENUM_PUBLIC_ONLY
public static final String WRITE_LONGS_AS_STRINGS
public static final String TYPE_NAME_MAP
public static final String SHORT_META_KEYS
public JsonWriter(OutputStream out)
out
- OutputStream to which the JSON will be written.JsonWriter(java.io.OutputStream, java.util.Map)
public JsonWriter(OutputStream out, Map<String,Object> optionalArgs)
out
- OutputStream to which the JSON output will be written.optionalArgs
- (optional) Map of extra arguments indicating how dates are formatted,
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)'.protected Map getObjectsReferenced()
protected Map getObjectsVisited()
public static String objectToJson(Object item)
item
- Object (root) to serialized to JSON String.objectToJson(Object, java.util.Map)
public static String objectToJson(Object item, Map<String,Object> optionalArgs)
item
- Object to convert to a JSON String.optionalArgs
- (optional) Map of extra arguments indicating how dates are formatted,
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 String formatJson(String json)
json
- String input JSONpublic boolean isPublicEnumsOnly()
public boolean isPrettyPrint()
public boolean getWriteLongsAsStrings()
protected void tabIn() throws IOException
IOException
protected void newLine() throws IOException
IOException
protected void tabOut() throws IOException
IOException
public boolean writeArrayElementIfMatching(Class arrayComponentClass, Object o, boolean showType, Writer output)
protected boolean writeCustom(Class arrayComponentClass, Object o, boolean showType, Writer output) throws IOException
IOException
public void addWriter(Class c, JsonWriter.JsonClassWriterBase writer)
c
- Class to associate a custom JSON writer toowriter
- JsonClassWriterBase which implements the appropriate
subclass of JsonClassWriterBase (JsonClassWriter or JsonClassWriterEx).public void addNotCustomWriter(Class c)
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, Map<Class,List<Field>> fieldSpecifiers)
stack
- Deque used to manage decent into graph (rather than using Java stack.) This allows for
much larger graph processing.obj
- Object root of graphfieldSpecifiers
- Map of optional field specifiers, which are used to override the field list returned by
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
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 allowed to be checked for a matching
custom writer to be used. This does not affect subobjects, just the top-level 'obj'
being passed in.IOException
public static boolean ensureJsonPrimitiveKeys(Map map)
public static void writeJsonUtf8String(String s, Writer output) throws IOException
s
- String to be written in UTF-8 format on the output stream.output
- Writer to which the UTF-8 string will be written toIOException
- if an error occurs writing to the output stream.public void close()
close
in interface Closeable
close
in interface AutoCloseable
Copyright © 2015. All rights reserved.