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.SqlDateWriter |
static class |
JsonWriter.StringBufferWriter |
static class |
JsonWriter.StringBuilderWriter |
static class |
JsonWriter.TimestampWriter |
static class |
JsonWriter.TimeZoneWriter |
Constructor and Description |
---|
JsonWriter(OutputStream out) |
Modifier and Type | Method and Description |
---|---|
static void |
addNotCustomWriter(Class c) |
static void |
addWriter(Class c,
JsonWriter.JsonClassWriter writer) |
void |
close() |
void |
flush() |
static int |
getDistance(Class a,
Class b) |
static String |
objectToJson(Object item)
Convert a Java Object to a JSON String.
|
static String |
toJson(Object item)
Convert a Java Object to a JSON String.
|
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 JsonWriter(OutputStream out) throws IOException
IOException
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 static String toJson(Object item)
item
- Object to convert to a JSON String.public static String objectToJson(Object item) throws IOException
item
- Object to convert to a JSON String.IOException
- If an I/O error occurspublic 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 © 2013. All rights reserved.