public class JsonWriter extends Object implements Closeable, Flushable
JsonWriter.toJson(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 class |
JsonWriter.BigDecimalWriter |
static class |
JsonWriter.BigIntegerWriter |
static class |
JsonWriter.CalendarWriter |
static class |
JsonWriter.ClassWriter |
static class |
JsonWriter.DateWriter |
static interface |
JsonWriter.JsonClassWriter |
static class |
JsonWriter.JsonStringWriter |
static class |
JsonWriter.LocaleWriter |
static class |
JsonWriter.StringBufferWriter |
static class |
JsonWriter.StringBuilderWriter |
static class |
JsonWriter.TimestampWriter |
static class |
JsonWriter.TimeZoneWriter |
Modifier and Type | Field and Description |
---|---|
static String |
DATE_FORMAT |
static String |
ISO_DATE_FORMAT |
static String |
ISO_DATE_TIME_FORMAT |
static String |
PRETTY_PRINT |
static String |
TYPE |
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() |
void |
flush() |
static String |
formatJson(String json)
Format the passed in JSON string in a nice, human readable format.
|
static int |
getDistance(Class a,
Class b) |
boolean |
isPrettyPrint() |
static String |
objectToJson(Object item) |
static String |
objectToJson(Object item,
Map<String,Object> optionalArgs)
Convert a Java Object to a JSON String.
|
static String |
toJson(Object item)
Deprecated.
|
static String |
toJson(Object item,
Map<String,Object> optionalArgs)
Deprecated.
|
void |
write(Object obj) |
boolean |
writeArrayElementIfMatching(Class arrayComponentClass,
Object o,
boolean showType,
Writer out) |
boolean |
writeIfMatching(Object o,
boolean showType,
Writer out) |
static void |
writeJsonUtf8String(String s,
Writer out)
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 JsonWriter(OutputStream out) throws IOException
IOException
JsonWriter(java.io.OutputStream, java.util.Map)
public JsonWriter(OutputStream out, Map<String,Object> optionalArgs) throws IOException
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)'.IOException
@Deprecated public static String toJson(Object item)
@Deprecated public static String toJson(Object item, Map<String,Object> optionalArgs)
public static String objectToJson(Object item) throws IOException
IOException
objectToJson(Object, java.util.Map)
public static String objectToJson(Object item, Map<String,Object> optionalArgs) throws IOException
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)'.IOException
- If an I/O error occurspublic static String formatJson(String json) throws IOException
json
- String input JSONIOException
public boolean isPrettyPrint()
public boolean writeIfMatching(Object o, boolean showType, Writer out) throws IOException
IOException
public boolean writeArrayElementIfMatching(Class arrayComponentClass, Object o, boolean showType, Writer out) throws IOException
IOException
public static void addWriter(Class c, JsonWriter.JsonClassWriter writer)
public static void addNotCustomWriter(Class c)
public void write(Object obj) throws IOException
IOException
public static void writeJsonUtf8String(String s, Writer out) throws IOException
s
- String to be written in utf8 format on the output stream.IOException
- if an error occurs writing to the output stream.public void close()
close
in interface Closeable
close
in interface AutoCloseable
Copyright © 2014. All rights reserved.