Gson - 根据字段值反序列化为特定的对象类型 [英] Gson - deserialization to specific object type based on field value
问题描述
我想根据 type
字段值将json对象反序列化为特定类型的对象(使用Gson库),例如:
[
{
type:type1,
id:131481204101,
url:http://something.com,
name:BLAH BLAH,
icon:SOME_STRING,
price:FREE,
backgroundUrl:SOME_STRING
},
{
....
}
]
因此 type
字段将具有不同的(但已知的)值。基于这个值,我需要将该json对象反序列化为适当的模型对象,例如:Type1Model,Type2Model等
我知道我可以在反序列化之前轻松地将它转换为 JSONArray
,遍历它并解析它应该被反序列化的类型。但我认为这是丑陋的方法,我正在寻找更好的方法。任何建议?
您可以实现一个 JsonDeserializer
将您的Json值解析为Java实例。我会尝试用一个代码来展示它:
1)定义您的自定义 JsonDeserializer
类,它通过传入的json值的id属性创建不同类的实例:
class MyTypeModelDeserializer实现JsonDeserializer< MyBaseTypeModel> {
@Override
public MyBaseTypeModel deserialize(final JsonElement json,final Type typeOfT,final JsonDeserializationContext context)
throws JsonParseException {
JsonObject jsonObject = json .getAsJsonObject();
JsonElement jsonType = jsonObject.get(type);
String type = jsonType.getAsString();
MyBaseTypeModel typeModel = null;
if(type1.equals(type)){
typeModel = new Type1Model();
} else if(type2.equals(type)){
typeModel = new Type2Model();
}
// TODO:设置类型模型的属性
返回typeModel;
2)为你的不同实例定义一个基类java对象:
class MyBaseTypeModel {
private String type;
// TODO:在这里添加其他共享字段
code
$ b 3)定义你的不同的java对象类的实例扩展你的基类:
class Type1Model extends MyBaseTypeModel {
// TODO :为这个类添加特定的字段
}
$ b $ class Type2Model extends MyBaseTypeModel {
// TODO:为这个类添加特定的字段
}
4)将这些类解析为一个bean时使用这些类:
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.registerTypeAdapter(MyBaseTypeModel.class,new MyTypeModelDeserializer());
Gson gson = gsonBuilder.create();
MyBaseTypeModel myTypeModel = gson.fromJson(myJsonString,MyBaseTypeModel.class);
现在我无法测试它,但我希望您能明白。 这个链接会非常有帮助。
I want to deserialize json objects to specific types of objects (using Gson library) based on type
field value, eg.:
[
{
"type": "type1",
"id": "131481204101",
"url": "http://something.com",
"name": "BLAH BLAH",
"icon": "SOME_STRING",
"price": "FREE",
"backgroundUrl": "SOME_STRING"
},
{
....
}
]
So type
field will have different (but known) values. Based on that value I need to deserialize that json object to appropriate model object, eg.: Type1Model, Type2Model etc.
I know I can easily do that before deserialization by converting it to JSONArray
, iterate through it and resolve which type it should be deserialized to. But I think it's ugly approach and I'm looking for better way. Any suggestions?
解决方案 You may implement a JsonDeserializer
and use it while parsing your Json value to a Java instance. I'll try to show it with a code which is going to give you the idea:
1) Define your custom JsonDeserializer
class which creates different instance of classes by incoming json value's id property:
class MyTypeModelDeserializer implements JsonDeserializer<MyBaseTypeModel> {
@Override
public MyBaseTypeModel deserialize(final JsonElement json, final Type typeOfT, final JsonDeserializationContext context)
throws JsonParseException {
JsonObject jsonObject = json.getAsJsonObject();
JsonElement jsonType = jsonObject.get("type");
String type = jsonType.getAsString();
MyBaseTypeModel typeModel = null;
if("type1".equals(type)) {
typeModel = new Type1Model();
} else if("type2".equals(type)) {
typeModel = new Type2Model();
}
// TODO : set properties of type model
return typeModel;
}
}
2) Define a base class for your different instance of java objects:
class MyBaseTypeModel {
private String type;
// TODO : add other shared fields here
}
3) Define your different instance of java objects' classes which extend your base class:
class Type1Model extends MyBaseTypeModel {
// TODO: add specific fields for this class
}
class Type2Model extends MyBaseTypeModel {
// TODO: add specific fields for this class
}
4) Use these classes while parsing your json value to a bean:
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.registerTypeAdapter(MyBaseTypeModel.class, new MyTypeModelDeserializer());
Gson gson = gsonBuilder.create();
MyBaseTypeModel myTypeModel = gson.fromJson(myJsonString, MyBaseTypeModel.class);
I can not test it right now but I hope you get the idea. Also this link would be very helpful.
这篇关于Gson - 根据字段值反序列化为特定的对象类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!