public class JsonUtil extends Object
Common JSON libraries such as jackson or gson all support to serialise such deep object trees.
However, none of the known libraries supports to to deserialise json strings that describe
Object[] or Map
The general problem is that the class information of a given object instance is lost during
serialisation. E.g. it is not possible to deserialise the object array
new SportMatch[] {new FootballMatch("Arsenal","Chelsea"), new TennisMatch("Nadal","Djokovic")}
based on the json representation
[{oponent1:"Arsenal", oponent2:"Chelsea"},{oponent1:"Nadal", oponent2:"Djokovic"}]
This library overcomes this issue and also serialises class information where the class type
cannot be deducted otherwise. When serialising it allows to choose whether to serialise class
type information for the root object. Not serialising root object class type information is
appropriate if you can predict the root object class type at deserialization time. E.g.
// if you can predict the type
String json = JsonUtil.serialize(new SportMatch[] {new FootballMatch("Arsenal","Chelsea"), new TennisMatch("Nadal","Djokovic")},false);
SportMatch[] o = JsonUtil.deserialize(json, SportMatch[].class);
// if you cannot predict the type
String json = JsonUtil.serialize(new SportMatch[] {new FootballMatch("Arsenal","Chelsea"), new TennisMatch("Nadal","Djokovic")},true);
Object o = JsonUtil.deserialize(json);
| Constructor and Description |
|---|
JsonUtil() |
| Modifier and Type | Method and Description |
|---|---|
static Object |
deserialize(String json) |
static <T> T |
deserialize(String json,
Class<T> type) |
static boolean |
deserializeBoolean(String json) |
static byte |
deserializeByte(String json) |
static <T> Collection<T> |
deserializeCollection(String json,
Class<? extends Collection> type,
Class<T> elementType) |
static double |
deserializeDouble(String json) |
static float |
deserializeFloat(String json) |
static <K,V> HashMap<K,V> |
deserializeHashMap(String json,
Class<K> key,
Class<V> value) |
static int |
deserializeInt(String json) |
static long |
deserializeLong(String json) |
static short |
deserializeShort(String json) |
static String |
serialize(boolean object) |
static String |
serialize(byte object) |
static String |
serialize(double object) |
static String |
serialize(float object) |
static String |
serialize(int object) |
static String |
serialize(long object) |
static String |
serialize(Object object,
boolean serializeType) |
static String |
serialize(short object) |
static String |
serializeCollection(Collection<?> object,
boolean serializeType,
boolean serializeElementType) |
public static String serialize(boolean object)
public static String serialize(byte object)
public static String serialize(short object)
public static String serialize(int object)
public static String serialize(long object)
public static String serialize(float object)
public static String serialize(double object)
public static String serializeCollection(Collection<?> object, boolean serializeType, boolean serializeElementType)
public static boolean deserializeBoolean(String json)
public static byte deserializeByte(String json)
public static short deserializeShort(String json)
public static int deserializeInt(String json)
public static long deserializeLong(String json)
public static float deserializeFloat(String json)
public static double deserializeDouble(String json)
public static <K,V> HashMap<K,V> deserializeHashMap(String json, Class<K> key, Class<V> value)
public static <T> Collection<T> deserializeCollection(String json, Class<? extends Collection> type, Class<T> elementType)
Copyright © 2018. All rights reserved.