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 Contextas parameter:
我有一个名为 的类PageItem,它有一个将 aContext作为参数的构造函数:
PageItem(Context context)
{
super(context);
this.context = context;
}
PageItemhas these properties:
PageItem有这些属性:
private int id;
private String Title;
private String Description;
public Newsprovider newsprovider;
public Topic topic;
Newsproviderand Topicare 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, Newsproviderand Topicare subclasses of SQLiteOpenHelper.
PageItem,Newsprovider并且Topic是的子类SQLiteOpenHelper。
I want to deserialize PageItemarray 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 PageItemInstanceCreatordefined 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 PageIteminstance has correctly "MainActivity" as context while
but its newsprovidermember variable has context = null.
调试时,PageItem实例正确地将“MainActivity”作为上下文,而其newsprovider成员变量的上下文为 null。
Gson created PageItemobject using the right constructor but it created Newsproviderinstance using the default parameterless constructor. How can I fix this?
GsonPageItem使用正确的构造函数创建了对象,但它Newsprovider使用默认的无参数构造函数创建了实例。我怎样才能解决这个问题?
采纳答案by giampaolo
Just add a new InstanceCreatorderived class for NewsProviderlike 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 GsonBuilderlike 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 Topicclass.
在Topic课堂上也重复一遍。

