JSON Methodology
The JSON data type produced depends on the Java object type being serialized.
-
Primitives and primitive objects are converted to JSON primitives.
-
Beans and Maps are converted to JSON objects.
-
Collections and arrays are converted to JSON arrays.
-
Anything else is converted to JSON strings.
POJO type |
JSON type |
Example |
Serialized form |
String |
String |
serialize("foobar"); |
'foobar'
|
Number |
Number |
serialize(123); |
123
|
Boolean |
Boolean |
serialize(true); |
true
|
Null |
Null |
serialize(null); |
null
|
Beans with properties of any type on this list |
Object |
serialize(new MyBean()); |
{p1:'val1',p2:true}
|
Maps with values of any type on this list |
Object |
serialize(new TreeMap()); |
{key1:'val1',key2:true}
|
Collections and arrays of any type on this list |
Array |
serialize(new Object[]{1,"foo",true}); |
[1,'foo',true]
|
In addition, swaps can be used to convert non-serializable POJOs into serializable forms, such as converting
Calendar object to ISO8601 strings, or byte[] arrays to Base-64
encoded strings.