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.JsonClassWriterEx
Implement this interface to custom the JSON output for a given class.
|
Modifier and Type | Field and Description |
---|---|
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 |
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 |
---|---|
static void |
addNotCustomWriter(Class c) |
static void |
addWriter(Class c,
JsonWriter.JsonClassWriter writer) |
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 static Object |
getArg(String key) |
protected static Map |
getArgs() |
protected Map |
getObjectsReferenced()
Provide access to subclasses.
|
protected Map |
getObjectsVisited()
Provide access to subclasses.
|
protected String |
getSubstituteTypeName(String typeName) |
protected String |
getSubstituteTypeNameIfExists(String typeName) |
static boolean |
getWriteLongsAsStrings() |
static boolean |
isPrettyPrint() |
static 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) |
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) |
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 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)'.public static String objectToJson(Object item)
item
- Object (root) to serialized to JSON String.objectToJson(Object, java.util.Map)
protected static Map getArgs()
protected Map getObjectsReferenced()
protected Map getObjectsVisited()
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 static boolean isPublicEnumsOnly()
public static boolean isPrettyPrint()
public static 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 static void addWriter(Class c, JsonWriter.JsonClassWriter writer)
public static void addNotCustomWriter(Class c)
public void write(Object obj)
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
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.