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 voidwriteJsonText(boolean[] array, Appendable out) Writes the given array ofbooleansas a JSON list to the givenAppendable.static voidwriteJsonText(byte[] array, Appendable out) Writes the given array ofbytesas a JSON list to the givenAppendable.static voidwriteJsonText(char[] array, Appendable out) Writes the given array of characters as a JSON list to the givenAppendable.static voidwriteJsonText(double[] array, Appendable out) Writes the given array ofdoublesas a JSON list to the givenAppendable.static voidwriteJsonText(float[] array, Appendable out) Writes the given array offloatsas a JSON list to the givenAppendable.static voidwriteJsonText(int[] array, Appendable out) Writes the given array ofintsas a JSON list to the givenAppendable.static voidwriteJsonText(long[] array, Appendable out) Writes the given array oflongsas a JSON list to the givenAppendable.static voidwriteJsonText(short[] array, Appendable out) Writes the given array ofshortsas a JSON list to the givenAppendable.static voidwriteJsonText(Object[] array, Appendable out) Writes the given array ofObjectsas a JSON list to the givenAppendable.static voidwriteJsonText(Collection<?> collection, Appendable out) Writes the givenCollectionas a JSON list to the givenAppendable.static voidwriteJsonText(Map<?, ?> map, Appendable out) Writes the givenMapas a JSON object to the givenAppendable.static StringwriteJsonTextAsString(boolean[] array) Returns the given array ofbooleansas a JSON string.static StringwriteJsonTextAsString(byte[] array) Returns the given array ofbytesas a JSON string.static StringwriteJsonTextAsString(char[] array) Returns the given array ofcharsas a JSON string.static StringwriteJsonTextAsString(double[] array) Returns the given array ofdoublesas a JSON string.static StringwriteJsonTextAsString(float[] array) Returns the given array offloatsas a JSON string.static StringwriteJsonTextAsString(int[] array) Returns the given array ofintsas a JSON string.static StringwriteJsonTextAsString(long[] array) Returns the given array oflongsas a JSON string.static StringwriteJsonTextAsString(short[] array) Returns the given array ofshortsas a JSON string.static StringwriteJsonTextAsString(Object[] array) Returns the given array ofObjectsas a JSON string.static StringwriteJsonTextAsString(Collection<?> collection) Returns the givenCollectionas a JSON string.static StringwriteJsonTextAsString(Map<?, ?> map) Returns the givenMapas a JSON string.
-
Method Details
-
writeJsonText
Writes the givenMapas a JSON object to the givenAppendable. Note that the keys of the given map will be represented asStrings(via theirtoStringmethod) regardless of their actual type.- Parameters:
map- the map to write as a JSON textout- theAppendableto which to write the JSON text- Throws:
IOException- in the event of an I/O error when writing to the givenAppendable
-
writeJsonTextAsString
Returns the givenMapas a JSON string. Note that the keys of the given map will be represented asStrings(via theirtoStringmethod) 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 givenCollectionas a JSON list to the givenAppendable.- Parameters:
collection- the collection to write as a JSON valueout- theAppendableto which to write the JSON text- Throws:
IOException- in the event of an I/O error when writing to the givenAppendable
-
writeJsonTextAsString
Returns the givenCollectionas 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 ofbytesas a JSON list to the givenAppendable.- Parameters:
array- the array to write as a JSON valueout- theAppendableto 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 ofbytesas 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 ofshortsas a JSON list to the givenAppendable.- Parameters:
array- the array to write as a JSON valueout- theAppendableto 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 ofshortsas 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 ofintsas a JSON list to the givenAppendable.- Parameters:
array- the array to write as a JSON valueout- theAppendableto 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 ofintsas 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 oflongsas a JSON list to the givenAppendable.- Parameters:
array- the array to write as a JSON valueout- theAppendableto 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 oflongsas 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 offloatsas a JSON list to the givenAppendable.- Parameters:
array- the array to write as a JSON valueout- theAppendableto 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 offloatsas 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 ofdoublesas a JSON list to the givenAppendable.- Parameters:
array- the array to write as a JSON valueout- theAppendableto 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 ofdoublesas 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 ofbooleansas a JSON list to the givenAppendable.- Parameters:
array- the array to write as a JSON valueout- theAppendableto 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 ofbooleansas 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- theAppendableto 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 ofcharsas 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 ofObjectsas a JSON list to the givenAppendable.- Parameters:
array- the array to write as a JSON valueout- theAppendableto 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 ofObjectsas a JSON string.- Parameters:
array- the array to return as a JSON text- Returns:
- a JSON string representing the given array
-