GSON Interface InstanceCreator<T>
来源:互联网 发布:elmo控制器 编程 编辑:程序博客网 时间:2024/06/05 13:21
Interface InstanceCreator<T>
- Type Parameters:
T
- the type of object that willbe created by this implementation.
public interface InstanceCreator<T>
Thisinterface is implemented to create instances of a class that doesnot define a no-args constructor. If you can modify the class, youshould instead add a private, or public no-args constructor.However, that is not possible for library classes, such as JDKclasses, or a third-party library that you do not have source-codeof. In such cases, you should define an instance creator for theclass. Implementations of this interface should be registeredwith GsonBuilder.registerTypeAdapter(Type,Object)
Letus look at an example where defining an InstanceCreator might beuseful. The Id
public class Id<T> { private final Class<T> clazz; private final long value; public Id(Class<T> clazz, long value) { this.clazz = clazz; this.value = value; } }
IfGson encounters an object oftype Id
private Id() { this(Object.class, 0L); }
However, let us assume that the developer does not have access tothe source-code ofthe Id
InstanceCreator
Id
:
class IdInstanceCreator implements InstanceCreator<Id> { public Id createInstance(Type type) { return new Id(Object.class, 0L); } }
Notethat it does not matter what the fields of the created instancecontain since Gson will overwrite them with the deserialized valuesspecified in Json. You should also ensure thata IdInstanceCreator
Gson gson = new GsonBuilder().registerTypeAdapter(Id.class, new IdInstanceCreator()).create();
- GSON Interface InstanceCreator<T>
- Register an InstanceCreator with Gson for this type to fix this problem.
- Wireshark OS X can't detect interface
- Gson解析json字符串,并封装成List<T>
- 使用Gson将List<T>()转化为json字符串
- GSON解析传入泛型T,出现类型转换异常
- Gson
- Gson
- Gson
- Gson
- GSON
- Gson
- Gson
- Gson
- Gson
- gson
- GSON
- Gson
- Python基础语法
- gson的@Expose注解和@SerializedName注解
- Gson通过借助TypeToken获取泛型参数的类型的方法
- [入门]MySQL学习笔记二数据的增删改查
- 数据分类
- GSON Interface InstanceCreator<T>
- Android measure
- 过了这么久你变了没有
- Nightmare
- Tabhost中子Activity显示异常
- TabHost中,同一个页面向下级页面跳转时的解决方案
- LeetCode OJ - Count and Say
- 构建一个工作类
- Play mp4 from url in android