Java Gson 使用 InstanceCreator 反序列化嵌套对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18567719/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Gson deserializing nested objects with InstanceCreator
提问by user2568379
I have a class named PageItem
, which has a constructor that takes a Context
as parameter:
我有一个名为 的类PageItem
,它有一个将 aContext
作为参数的构造函数:
PageItem(Context context)
{
super(context);
this.context = context;
}
PageItem
has these properties:
PageItem
有这些属性:
private int id;
private String Title;
private String Description;
public Newsprovider newsprovider;
public Topic topic;
Newsprovider
and Topic
are other classes of my application and have the following constructors:
Newsprovider
并且Topic
是我的应用程序的其他类,并具有以下构造函数:
Newsprovider (Context context)
{
super(context);
this.context = context;
}
Topic (Context context)
{
super(context);
this.context = context;
}
PageItem
, Newsprovider
and Topic
are subclasses of SQLiteOpenHelper
.
PageItem
,Newsprovider
并且Topic
是的子类SQLiteOpenHelper
。
I want to deserialize PageItem
array with Gson, so I wrote:
我想PageItem
用 Gson反序列化数组,所以我写道:
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.registerTypeAdapter(PageItem.class, new PageItemInstanceCreator(context));
Gson gson = gsonBuilder.create();
Pageitem pis[] = gson.fromJson(s, PageItem[].class);
with PageItemInstanceCreator
defined as:
以PageItemInstanceCreator
定义为:
public class PageItemInstanceCreator implements InstanceCreator<PageItem>
{
private Context context;
public PageItemInstanceCreator(Context context)
{
this.context = context;
}
@Override
public PageItem createInstance(Type type)
{
PageItem pi = new PageItem(context);
return pi;
}
}
When debugging, a PageItem
instance has correctly "MainActivity" as context while
but its newsprovider
member variable has context = null.
调试时,PageItem
实例正确地将“MainActivity”作为上下文,而其newsprovider
成员变量的上下文为 null。
Gson created PageItem
object using the right constructor but it created Newsprovider
instance using the default parameterless constructor. How can I fix this?
GsonPageItem
使用正确的构造函数创建了对象,但它Newsprovider
使用默认的无参数构造函数创建了实例。我怎样才能解决这个问题?
采纳答案by giampaolo
Just add a new InstanceCreator
derived class for NewsProvider
like this:
只需添加一个新的InstanceCreator
派生类,NewsProvider
如下所示:
public class NewsProviderInstanceCreator implements InstanceCreator<NewsProvider>
{
private int context;
public NewsProviderInstanceCreator(int context)
{
this.context = context;
}
@Override
public NewsProvider createInstance(Type type)
{
NewsProvider np = new NewsProvider(context);
return np;
}
}
and register it into the GsonBuilder
like you have already done, like this:
并将其注册到GsonBuilder
您已经完成的操作中,如下所示:
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.registerTypeAdapter(PageItem.class, new PageItemInstanceCreator(context));
gsonBuilder.registerTypeAdapter(NewsProvider.class, new NewsProviderInstanceCreator(context));
Gson gson = gsonBuilder.create();
PageItem pis[] = gson.fromJson(s, PageItem[].class);
repeat it also for Topic
class.
在Topic
课堂上也重复一遍。