使用递归依赖项对复杂对象进行反序列化

时间:2022-04-21 16:55:45

I have several classes that contain a recursive dependency on each other and I serialize them to JSON format with Gson GraphAdapterBuilder, and it works perfectly. Now I want to deserialize them into the same structure but can't find out how.

我有几个类,其中包含对彼此的递归依赖,我用Gson GraphAdapterBuilder将它们序列化为JSON格式,而且它工作得很好。现在我想把它们反序列化成相同的结构,但是我不知道怎么做。

I've made an example:

我做了一个例子:

class ClassA{
    public int field;
    public ClassB parent;
    public ClassA(int f, ClassB p){
        field = f;
        parent = p;
    }
}

class ClassB{
    public Vector<ClassA> vector = new Vector<ClassA>();
}

...

ClassB b = new ClassB();        

ClassA a1 = new ClassA(1,b);
ClassA a2 = new ClassA(2,b);
ClassA a3 = new ClassA(3,b);

b.vector.add(a1);
b.vector.add(a2);
b.vector.add(a3);

//Serializing object b

GsonBuilder gsonBuilder = new GsonBuilder();

new GraphAdapterBuilder()
    .addType(ClassA.class)
    .addType(ClassB.class)
    .registerOn(gsonBuilder);

Gson gson = gsonBuilder.create();

String json = gson.toJson(b);

Output is giving me what I want:

输出是我想要的:

{"0x1":{"vector":["0x2","0x3","0x4"]},"0x2":{"field":1,"parent":"0x1"},"0x3":{"field":2,"parent":"0x1"},"0x4":{"field":3,"parent":"0x1"}}

Is there a way to deserialize that json string back into object of ClassB?

有办法将json字符串反序列化为ClassB的对象吗?

2 个解决方案

#1


8  

Okay, I found solution. It was very simple. Just had to use the function fromJson instead of toJson with the same GraphAdapterBuilder structure.

好的,我找到了解决方案。这是非常简单的。只需使用json而不是toJson的函数,使用相同的GraphAdapterBuilder结构。

    ...
    GsonBuilder gsonBuilder = new GsonBuilder();
    new GraphAdapterBuilder()
            .addType(ClassA.class)
            .addType(ClassB.class)
            .registerOn(gsonBuilder);
    gson = gsonBuilder.create();
    СlassB B = gson.fromJson(json,ClassB.class);

    System.out.println("B " + B.vector);
    for(ClassA classA:B.vector){
        System.out.println(classA.field + "  " + classA.parent);
    }

Output is:

输出是:

    B [ClassA@10178f2b, ClassA@7ab8584d, ClassA@5cad662c]
    1  ClassB@7c0f023c
    2  ClassB@7c0f023c
    3  ClassB@7c0f023c

#2


-3  

class ClassA{
    public int field;
    public String parent;
    public ClassA(int f, String parent){
        this.field = f;
        this.parent = parent;
    }
}

class ClassB{
    public List<String> vector;
}

String str1 = "{\"0x1\":{\"vector\":[\"0x2\",\"0x3\",\"0x4\"]},\"0x2\":{\"field\":1,\"parent\":\"0x1\"},\"0x3\":{\"field\":2,\"parent\":\"0x1\"},\"0x4\":{\"field\":3,\"parent\":\"0x1\"}}";

JsonParser parser = new JsonParser();
JsonObject element = (JsonObject)parser.parse(str1);
JsonElement responseWrapper = element.get("0x1");
Gson gson = new Gson();
ClassB  classB =gson.fromJson(responseWrapper,ClassB.class);
System.out.println(classB.vector);
for (String key:classB.vector) {
      responseWrapper = element.get( key);
      ClassA  classA =gson.fromJson(responseWrapper,ClassA.class);
      System.out.println(classA.field);
      System.out.println(classA.parent);

}

#1


8  

Okay, I found solution. It was very simple. Just had to use the function fromJson instead of toJson with the same GraphAdapterBuilder structure.

好的,我找到了解决方案。这是非常简单的。只需使用json而不是toJson的函数,使用相同的GraphAdapterBuilder结构。

    ...
    GsonBuilder gsonBuilder = new GsonBuilder();
    new GraphAdapterBuilder()
            .addType(ClassA.class)
            .addType(ClassB.class)
            .registerOn(gsonBuilder);
    gson = gsonBuilder.create();
    СlassB B = gson.fromJson(json,ClassB.class);

    System.out.println("B " + B.vector);
    for(ClassA classA:B.vector){
        System.out.println(classA.field + "  " + classA.parent);
    }

Output is:

输出是:

    B [ClassA@10178f2b, ClassA@7ab8584d, ClassA@5cad662c]
    1  ClassB@7c0f023c
    2  ClassB@7c0f023c
    3  ClassB@7c0f023c

#2


-3  

class ClassA{
    public int field;
    public String parent;
    public ClassA(int f, String parent){
        this.field = f;
        this.parent = parent;
    }
}

class ClassB{
    public List<String> vector;
}

String str1 = "{\"0x1\":{\"vector\":[\"0x2\",\"0x3\",\"0x4\"]},\"0x2\":{\"field\":1,\"parent\":\"0x1\"},\"0x3\":{\"field\":2,\"parent\":\"0x1\"},\"0x4\":{\"field\":3,\"parent\":\"0x1\"}}";

JsonParser parser = new JsonParser();
JsonObject element = (JsonObject)parser.parse(str1);
JsonElement responseWrapper = element.get("0x1");
Gson gson = new Gson();
ClassB  classB =gson.fromJson(responseWrapper,ClassB.class);
System.out.println(classB.vector);
for (String key:classB.vector) {
      responseWrapper = element.get( key);
      ClassA  classA =gson.fromJson(responseWrapper,ClassA.class);
      System.out.println(classA.field);
      System.out.println(classA.parent);

}