fastjson和gson反序列化时的嵌套集合和抽象类处理
2016-09-30 10:42
4151 查看
gson参考:http://www.letiantian.me/2014-05-22-java-gson-json/
fastjson参考:https://github.com/alibaba/fastjson/wiki/ObjectDeserializer_cn
这个对象使用了apache Pair,这是个抽象类,有两个实现 ImmutablePair 和 MutablePair。 ImmutablePair 是没有默认构造函数的,抽象类也不能直接创建对象,所以反序列化时会出错。这时就需要自定义反序列化类型了。
就这些。
另:fastjson还可以直接写类名写到json里面,这样就方便反序列化了。不过对于无默认构造函数的还是无能为力。
其它:jackson应该是我最常用的,但是由于它本身很强大,所以并没有去仔细看过,以后有的话再补上。
fastjson参考:https://github.com/alibaba/fastjson/wiki/ObjectDeserializer_cn
带泛型的集合类型
当使用toJson(obj)时,Gson调用obj.getClass()获取字段信息以在序列化中使用。类似的,也可以将对象MyClass.class作为参数传递给fromJson(json, MyClass.class)方法,这可以在在对象不是泛型的时候使用。不过,当对象是一个泛型类型的对象,由于Java中类型擦除(Type Erasure)这一机制,泛型类型信息会丢失。带泛型的集合类型 fastjson
//错误代码 Result deserialized = JSON.parseObject(json, Result.class); //正确代码,需要使用泛型创建一个类以获取真实泛型。因为java中不能写Result<List<Product>>.class,所以需要借助另一个类 TypeReference typeReference = new TypeReference<Result<List<Product>>>() {}; Result deserialized = JSON.parseObject(json, typeReference.getType());
带泛型的集合类型 gson
Gson gson = new GsonBuilder().create(); Type typeToken = new TypeToken<Result<List<ProductListVO>>>() { }.getType(); Result deserialized = gson.fromJson(json, typeToken);
抽象类处理
product类import org.apache.commons.lang3.tuple.Pair; public class ProductListVO { private long id; private String title; private List<Pair> properties;//属性对 //...省略get set 方法 }
这个对象使用了apache Pair,这是个抽象类,有两个实现 ImmutablePair 和 MutablePair。 ImmutablePair 是没有默认构造函数的,抽象类也不能直接创建对象,所以反序列化时会出错。这时就需要自定义反序列化类型了。
fastjson 自定义反序列化类型
ParserConfig.getGlobalInstance().putDeserializer(Pair.class, new ObjectDeserializer() { @Override public < T > T deserialze(DefaultJSONParser parser, Type type, Object fieldName) { Map map = parser.parseObject(Map.class); return (T) ImmutablePair.of(map.get("key"), map.get("value")); } @Override public int getFastMatchToken() { return 0; } });
gson自定义反序列化类型
Gson gson = new GsonBuilder().registerTypeAdapter(Pair.class, new JsonDeserializer < ImmutablePair > () { @Override public ImmutablePair deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { JsonObject jsonObject = json.getAsJsonObject(); return ImmutablePair.of(jsonObject.get("key").getAsString(), jsonObject.get("value").getAsString()); } }).create();
就这些。
另:fastjson还可以直接写类名写到json里面,这样就方便反序列化了。不过对于无默认构造函数的还是无能为力。
其它:jackson应该是我最常用的,但是由于它本身很强大,所以并没有去仔细看过,以后有的话再补上。
相关文章推荐
- gson处理多层嵌套的复杂形式的json
- FastJson、Jackson、Gson进行Java对象转换Json的细节处理
- Redis下实现序列化保存和使用FastJson的处理(java)
- FastJson、Jackson、Gson进行Java对象转换Json的细节处理
- fastjson 处理json字符串嵌套结构
- fastJson序列化和反序列化 中空字符串的处理
- fastjson序列化数据为空的字段如何处理的问题
- fastjson 嵌套对象,泛型的再次处理
- java中对于JSON 的处理 fastjson gson 系统自带的JSON 的选择
- Gson&FastJson解析异常Json的处理
- FastJson、Jackson、Gson进行Java对象转换Json的细节处理
- java序列化框架(protobuf、thrift、kryo、fst、fastjson、Jackson、gson、hessian)性能对比
- fastjson、gson、jackson序列化和反序列化性能对比
- FastJson、Jackson、Gson进行Java对象转换Json的细节处理
- JSON序列化和反序列化集合、字典、数组的处理
- Android笔记:gson处理多层嵌套的复杂形式的json
- 关于数据序列化(3),JSON的方式,FastJson序列化对象和List集合示例
- C#在Json反序列化中处理键的特殊字符
- 用gson如何处理json转换Timestamp类型