Gson这个Java类库可以把Java对象转换成JSON,也可以把JSON字符串转换成一个相等的Java对象。Gson支持任意复杂Java对象包括没有源代码的对象。
代码例子:
util.Date和sql.Date被转成长整形用于序列化传输
public class SQLDateSer ializer implements JsonSerializer
public JsonElement serialize(java.sql.Date src, Type typeOfSrc,
JsonSerializationContext context) {
return new JsonPrimitive(src.getTime());
}
}
public class UtilDateSerializer implements JsonSerializer
public JsonElement serialize(java.util.Date src, Type typeOfSrc,
JsonSerializationContext context) {
return new JsonPrimitive(src.getTime());
}
}
public class SQLDateDeserializer implements JsonDeserializer
public java.sql.Date deserialize(JsonElement json, Type typeOfT,
JsonDeserializationContext context) throws JsonParseException {
return new java.sql.Date(json.getAsJsonPrimitive().getAsLong());
}
}
public class UtilDateDeserializer implements JsonDeserializer
public java.util.Date deserialize(JsonElement json, Type typeOfT,
JsonDeserializationContext context) throws JsonParseException {
return new java.util.Date(json.getAsJsonPrimitive().getAsLong());
}
}
序列化反序列化类Worker.class
private String name;
private java.sql.Date dob;
private java.util.Date inTime;
序列化反序列化类Factory.class
private String name;
private Worker worker;
private Set
private List
序列化反序列化帮助类
public class JsonUtils {
public static String bean2json(Object bean) {
Gson gson = new GsonBuilder().registerTypeAdapter(java.sql.Date.class,
new SQLDateSerializer()).registerTypeAdapter(
java.util.Date.class, new UtilDateSerializer()).setDateFormat(
DateFormat.LONG).setPrettyPrinting().create();
// gson = new Gson();
return gson.toJson(bean);
}
public static
Gson gson = new GsonBuilder().registerTypeAdapter(java.sql.Date.class,
new SQLDateDeserializer()).registerTypeAdapter(
java.util.Date.class, new UtilDateDeserializer())
.setDateFormat(DateFormat.LONG).create();
// gson = new Gson();
return gson.fromJson(json, type);
}
}
测试代码:
public class GsonTestor {
private Worker worker;
private Factory factory;
public void setUp() {
worker = new Worker();
worker.setName("你想");
worker.setDob(new java.sql.Date(Calendar.getInstance()
.getTimeInMillis()));
worker.setInTime(Calendar.getInstance().getTime());
Worker w2 = new Worker();
w2.setName("改过");
w2.setDob(new java.sql.Date(Calendar.getInstance().getTimeInMillis()));
w2.setInTime(Calendar.getInstance().getTime());
Worker w3 = new Worker();
w3.setName("古古怪怪");
w3.setDob(new java.sql.Date(Calendar.getInstance().getTimeInMillis()));
w3.setInTime(Calendar.getInstance().getTime());
List
lws.add(worker);
lws.add(w2);
Set
sws.add(w3);
sws.add(worker);
sws.add(w2);
factory = new Factory();
factory.setName("IBM");
factory.setWorker(worker);
factory.setLws(lws);
factory.setWorkers(sws);
}
public void testSerializer() {
String factoryJson = JsonUtils.bean2json(factory);
P.rintln(factoryJson);
Factory factoryCopy = (Factory) JsonUtils.json2bean(factoryJson,
Factory.class);
P.rintln(factoryCopy.getWorker().getInTime());
P.rintln(JsonUtils.bean2json(worker));
P.rintln(new java.sql.Date(1247625062171L));
P.rintln(new java.util.Date(1247625621406L));
String json = "{/"name/":/"改过/",/"dob/":1247626770406,/"inTime/":1247626770406}";
Worker w4 = (Worker) JsonUtils.json2bean(json, Worker.class);
P.rintln(w4.getName());
worker.setName(null);
String workerJson = JsonUtils.bean2json(worker);
;
P.rintln(workerJson);
}
public void testGenericType() {
List
holder.add("Hello Gson");
String json = JsonUtils.bean2json(holder);
P.rintln(json);
Type type = new TypeToken>() {
}.getType();
Object obj = JsonUtils.json2bean(json, type);
P.rintln(obj);
P.rintln(obj.getClass());
}
}
source code: http://code.google.com/p/google-gson/