Package com.eatthepath.json
Class JsonSerializer
java.lang.Object
com.eatthepath.json.JsonSerializer
A JSON serializer is responsible for producing complete and legal JSON texts (objects or arrays). It can serialize
a Map
as a JSON object or any Collection
or array as a JSON list. The keys of a Map
are
always serialized as strings. Values within a Map
, Collection
, or array are converted to JSON values
as follows:
Object type | JSON representation |
---|---|
null |
null |
Map |
object |
Collection |
list |
byte[] |
list |
short[] |
list |
int[] |
list |
long[] |
list |
float[] |
list |
double[] |
list |
boolean[] |
list |
char[] |
list |
Object [] |
list |
Number |
number (or null ) |
Boolean |
true or false (or null ) |
all others | the result of obj.toString() |
- Author:
- Jon Chambers
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionstatic void
writeJsonText
(boolean[] array, Appendable out) Writes the given array ofbooleans
as a JSON list to the givenAppendable
.static void
writeJsonText
(byte[] array, Appendable out) Writes the given array ofbytes
as a JSON list to the givenAppendable
.static void
writeJsonText
(char[] array, Appendable out) Writes the given array of characters as a JSON list to the givenAppendable
.static void
writeJsonText
(double[] array, Appendable out) Writes the given array ofdoubles
as a JSON list to the givenAppendable
.static void
writeJsonText
(float[] array, Appendable out) Writes the given array offloats
as a JSON list to the givenAppendable
.static void
writeJsonText
(int[] array, Appendable out) Writes the given array ofints
as a JSON list to the givenAppendable
.static void
writeJsonText
(long[] array, Appendable out) Writes the given array oflongs
as a JSON list to the givenAppendable
.static void
writeJsonText
(short[] array, Appendable out) Writes the given array ofshorts
as a JSON list to the givenAppendable
.static void
writeJsonText
(Object[] array, Appendable out) Writes the given array ofObjects
as a JSON list to the givenAppendable
.static void
writeJsonText
(Collection<?> collection, Appendable out) Writes the givenCollection
as a JSON list to the givenAppendable
.static void
writeJsonText
(Map<?, ?> map, Appendable out) Writes the givenMap
as a JSON object to the givenAppendable
.static String
writeJsonTextAsString
(boolean[] array) Returns the given array ofbooleans
as a JSON string.static String
writeJsonTextAsString
(byte[] array) Returns the given array ofbytes
as a JSON string.static String
writeJsonTextAsString
(char[] array) Returns the given array ofchars
as a JSON string.static String
writeJsonTextAsString
(double[] array) Returns the given array ofdoubles
as a JSON string.static String
writeJsonTextAsString
(float[] array) Returns the given array offloats
as a JSON string.static String
writeJsonTextAsString
(int[] array) Returns the given array ofints
as a JSON string.static String
writeJsonTextAsString
(long[] array) Returns the given array oflongs
as a JSON string.static String
writeJsonTextAsString
(short[] array) Returns the given array ofshorts
as a JSON string.static String
writeJsonTextAsString
(Object[] array) Returns the given array ofObjects
as a JSON string.static String
writeJsonTextAsString
(Collection<?> collection) Returns the givenCollection
as a JSON string.static String
writeJsonTextAsString
(Map<?, ?> map) Returns the givenMap
as a JSON string.
-
Method Details
-
writeJsonText
Writes the givenMap
as a JSON object to the givenAppendable
. Note that the keys of the given map will be represented asStrings
(via theirtoString
method) regardless of their actual type.- Parameters:
map
- the map to write as a JSON textout
- theAppendable
to which to write the JSON text- Throws:
IOException
- in the event of an I/O error when writing to the givenAppendable
-
writeJsonTextAsString
Returns the givenMap
as a JSON string. Note that the keys of the given map will be represented asStrings
(via theirtoString
method) regardless of their actual type.- Parameters:
map
- the map to return as a JSON text- Returns:
- a JSON string representing the given map
-
writeJsonText
Writes the givenCollection
as a JSON list to the givenAppendable
.- Parameters:
collection
- the collection to write as a JSON valueout
- theAppendable
to which to write the JSON text- Throws:
IOException
- in the event of an I/O error when writing to the givenAppendable
-
writeJsonTextAsString
Returns the givenCollection
as a JSON string.- Parameters:
collection
- the collection to return as a JSON text- Returns:
- a JSON string representing the given collection
-
writeJsonText
Writes the given array ofbytes
as a JSON list to the givenAppendable
.- Parameters:
array
- the array to write as a JSON valueout
- theAppendable
to which to write the JSON text- Throws:
IOException
- in the event of an I/O error when writing to the givenAppendable
-
writeJsonTextAsString
Returns the given array ofbytes
as a JSON string.- Parameters:
array
- the array to return as a JSON text- Returns:
- a JSON string representing the given array
-
writeJsonText
Writes the given array ofshorts
as a JSON list to the givenAppendable
.- Parameters:
array
- the array to write as a JSON valueout
- theAppendable
to which to write the JSON text- Throws:
IOException
- in the event of an I/O error when writing to the givenAppendable
-
writeJsonTextAsString
Returns the given array ofshorts
as a JSON string.- Parameters:
array
- the array to return as a JSON text- Returns:
- a JSON string representing the given array
-
writeJsonText
Writes the given array ofints
as a JSON list to the givenAppendable
.- Parameters:
array
- the array to write as a JSON valueout
- theAppendable
to which to write the JSON text- Throws:
IOException
- in the event of an I/O error when writing to the givenAppendable
-
writeJsonTextAsString
Returns the given array ofints
as a JSON string.- Parameters:
array
- the array to return as a JSON text- Returns:
- a JSON string representing the given array
-
writeJsonText
Writes the given array oflongs
as a JSON list to the givenAppendable
.- Parameters:
array
- the array to write as a JSON valueout
- theAppendable
to which to write the JSON text- Throws:
IOException
- in the event of an I/O error when writing to the givenAppendable
-
writeJsonTextAsString
Returns the given array oflongs
as a JSON string.- Parameters:
array
- the array to return as a JSON text- Returns:
- a JSON string representing the given array
-
writeJsonText
Writes the given array offloats
as a JSON list to the givenAppendable
.- Parameters:
array
- the array to write as a JSON valueout
- theAppendable
to which to write the JSON text- Throws:
IOException
- in the event of an I/O error when writing to the givenAppendable
-
writeJsonTextAsString
Returns the given array offloats
as a JSON string.- Parameters:
array
- the array to return as a JSON text- Returns:
- a JSON string representing the given array
-
writeJsonText
Writes the given array ofdoubles
as a JSON list to the givenAppendable
.- Parameters:
array
- the array to write as a JSON valueout
- theAppendable
to which to write the JSON text- Throws:
IOException
- in the event of an I/O error when writing to the givenAppendable
-
writeJsonTextAsString
Returns the given array ofdoubles
as a JSON string.- Parameters:
array
- the array to return as a JSON text- Returns:
- a JSON string representing the given array
-
writeJsonText
Writes the given array ofbooleans
as a JSON list to the givenAppendable
.- Parameters:
array
- the array to write as a JSON valueout
- theAppendable
to which to write the JSON text- Throws:
IOException
- in the event of an I/O error when writing to the givenAppendable
-
writeJsonTextAsString
Returns the given array ofbooleans
as a JSON string.- Parameters:
array
- the array to return as a JSON text- Returns:
- a JSON string representing the given array
-
writeJsonText
Writes the given array of characters as a JSON list to the givenAppendable
.- Parameters:
array
- the array to write as a JSON valueout
- theAppendable
to which to write the JSON text- Throws:
IOException
- in the event of an I/O error when writing to the givenAppendable
-
writeJsonTextAsString
Returns the given array ofchars
as a JSON string.- Parameters:
array
- the array to return as a JSON text- Returns:
- a JSON string representing the given array
-
writeJsonText
Writes the given array ofObjects
as a JSON list to the givenAppendable
.- Parameters:
array
- the array to write as a JSON valueout
- theAppendable
to which to write the JSON text- Throws:
IOException
- in the event of an I/O error when writing to the givenAppendable
-
writeJsonTextAsString
Returns the given array ofObjects
as a JSON string.- Parameters:
array
- the array to return as a JSON text- Returns:
- a JSON string representing the given array
-