我想为我的对象编写一个自定义的反序列化程序,但为了实现这一点,我需要访问反序列化对象的另一部分。想象一下这样的JSON对象:
{
"id": 2,
"name": "circle",
"dependencies": {
"donut": "e8ed7bbd8715de2f5a85930912fcf238"
},
"someReference": {
"type": "Model",
"refersTo": "donut"
}
}我知道如何编写适配器/反序列化程序。我还知道如何通过前者将"e8ed7bbd8715de2f5a85930912fcf238“转换为Java对象。我不知道也想知道的是如何将我的“甜甜圈”转换成"e8ed7bbd8715de2f5a85930912fcf238“,或者在创建过程中成为最终的Java对象。该信息位于父对象的字段中,因为"someReference“对应于另一个Java对象。因此,顶级类是一个包含以下字段的Owner:
public class Owner {
public int id;
public String name;
public List<String, Resource> dependencies;
public MyReference someReference;
}到目前为止,id,name,dependencies都可以工作。
那么:如何在反序列化过程中引用父对象?这有可能吗?对于我正在尝试做的事情,有什么变通办法?
发布于 2014-05-19 04:23:02
我写了一个反序列化程序,它可以做你想要的事情(当然,如果我理解它的话)。对于您所问的,唯一的区别是我不知道您所说的Reference是什么意思,所以我使用了String。但我认为你可以很容易地修改我的代码。
我的所有者定义:
package stackoverflow.questions.q23718183;
import java.util.*;
public class Owner {
public int id;
public String name;
public Map<String, String> dependencies;
public MyReference someReference;
@Override
public String toString() {
return "Owner [id=" + id + ", name=" + name + ", dependencies=" + dependencies + ", someReference=" + someReference + "]";
}
public static class MyReference {
String type;
String refersTo;
@Override
public String toString() {
return "MyReference [type=" + type + ", refersTo=" + refersTo + "]";
}
}
}我的反序列化程序:
package stackoverflow.questions.q23718183;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.*;
import com.google.gson.*;
import com.google.gson.stream.*;
import com.google.gson.reflect.TypeToken;
public class OwnerDeserializer implements JsonDeserializer<Owner> {
@Override
public Owner deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
if (json == null)
return null;
else {
Owner owner = new Owner();
JsonObject jo = json.getAsJsonObject();
owner.id = jo.get("id").getAsInt();
owner.name = jo.get("name").getAsString();
TypeToken<Map<String, String>> mapType = new TypeToken<Map<String, String>>() {};
owner.dependencies = context.deserialize(jo.get("dependencies"), mapType.getType());
Owner.MyReference reference = new Owner.MyReference();
JsonObject jo2 = jo.get("someReference").getAsJsonObject();
reference.type = jo2.get("type").getAsString();
reference.refersTo = owner.dependencies.get(jo2.get("refersTo").getAsString());
owner.someReference = reference;
return owner;
}
}
}我的测试代码:
package stackoverflow.questions.q23718183;
import com.google.gson.*;
public class Q23718183 {
public static void main(String[] args) {
String s = " { "+
" \"id\": 2, "+
" \"name\": \"circle\", "+
" \"dependencies\": { "+
" \"donut\": \"e8ed7bbd8715de2f5a85930912fcf238\" "+
" }, "+
" \"someReference\": { "+
" \"type\": \"Model\", "+
" \"refersTo\": \"donut\" "+
" } "+
"}";
GsonBuilder b = new GsonBuilder();
b.registerTypeAdapter(Owner.class, new OwnerDeserializer());
Gson g = b.create();
Owner o = g.fromJson(s, Owner.class);
System.out.println(o);
}
}这就是结果:
Owner [id=2, name=circle, dependencies={donut=e8ed7bbd8715de2f5a85930912fcf238}, someReference=MyReference [type=Model, refersTo=e8ed7bbd8715de2f5a85930912fcf238]]如您所见,dependencies节点中的值替换为donut。
https://stackoverflow.com/questions/23718183
复制相似问题