当前位置: 首页 > 面试题库 >

Gson-使用InstanceCreator即时设置对象引用

司马辉
2023-03-14
问题内容

我被困在我需要我的对象的设定参考使用反序列化过程中被解析到其子对象的问题GsonInstanceCreator

为了描述这个问题,下面是类结构的简单表示。

public class Workshift {
    private final transient Context context;
    private final Visit visit;

    public Workshift(Context context) {
        this.context = context;
        this.visit = new Visit(this);
    }
}

public class Visit {
    private final transient Workshift workshift;

    public Visit(Workshift ws) {
        this.workshift = ws;
    }
}

通过这样的结构,我能够设置ContextWorkshift通过提供InstanceCreator给我的GsonBuilder,例如:

Gson gson = new GsonBuilder()
    .registerTypeAdapter(Workshift.class, new InstanceCreator<Workshift>() {
        @Override
        public Workshift createInstance(Type type) {
            return new Workshift(context);
        }
    })
    .create();

我知道,我可以添加额外的InstanceCreator给我GsonBuilder,但我不知道如何提供我的参考Workshift对象,它是在被解析(上即时)的进程Visit对象?

任何帮助,将不胜感激!


问题答案:

您绝对应该使用GraphAdapterBuilder。

正如您在@Braj的回答下的评论中所说,

*由于某些原因将 *workshift
设置为瞬态,因此在序列化访问对象时不会将其序列化。如果未将其标记为瞬态,则序列化将陷入堆栈溢出异常-通过创建不可停止的循环

这有一个简单的解决方案。

工作班

public class Workshift {
    private final transient Context context;
    private final Visit visit;
    //for testing
    private String workshift_description;

    public Workshift(Context context,String id) {
        this.workshift_description=id;
        this.context = context;
        this.visit = new Visit(this);

    }
    public String getId() {
        return workshift_description;
    }

    public void setId(String id) {
        this.workshift_description = id;
    }
    public String toString() {
        return "[Workshift element => { WD: "+this.workshift_description+", VD : "+this.visit.getVisit_description()+"}";
    }
}

Visit.java

public class Visit {

    private final /* transient  */ Workshift workshift;

    public Visit(Workshift ws) {
        this.workshift = ws;

    }
    public String getVisit_description() {
        return "visit containing  "+ workshift.getId();
    }

}

窍门在这里:

GsonBuilder gsonBuilder = new GsonBuilder();
        new GraphAdapterBuilder()
        .addType(Visit.class)
        .addType(Workshift.class)
        .registerOn(gsonBuilder);

放在一起

public static void main(String[] args) {

        Workshift[] workshifts = new Workshift[10];
        for (int i = 0; i < workshifts.length; i++) {
            //Replace Context(i) for the real one
            workshifts[i] = new Workshift(new Context(i), "Workshift#"
                    + i);
        }
        System.out.println("Original Workshifts array:");
        for (int i = 0; i < workshifts.length; i++) {
            System.out.println(workshifts[i]);
        }
        System.out.println("===================================");

        GsonBuilder gsonBuilder = new GsonBuilder();
        new GraphAdapterBuilder()
        .addType(Visit.class)
        .addType(Workshift.class)
        .registerOn(gsonBuilder);

        Gson gson = gsonBuilder.setPrettyPrinting().create();
        String serialized = gson.toJson(workshifts);
        // System.out.println(serialized);
        Workshift[] w_array = gson.fromJson(serialized, Workshift[].class);
        // System.out.println(gson.toJson(w_array));

        System.out.println("Des-serialized Workshifts array:");
        for (int i = 0; i < w_array.length; i++) {
            System.out.println(w_array[i]);
        }
        System.out.println("===================================");

输出:

Original Workshifts array:
[Workshift element => { WD: Workshift#0, VD : visit containing  Workshift#0}
[Workshift element => { WD: Workshift#1, VD : visit containing  Workshift#1}
[Workshift element => { WD: Workshift#2, VD : visit containing  Workshift#2}
[Workshift element => { WD: Workshift#3, VD : visit containing  Workshift#3}
[Workshift element => { WD: Workshift#4, VD : visit containing  Workshift#4}
[Workshift element => { WD: Workshift#5, VD : visit containing  Workshift#5}
[Workshift element => { WD: Workshift#6, VD : visit containing  Workshift#6}
[Workshift element => { WD: Workshift#7, VD : visit containing  Workshift#7}
[Workshift element => { WD: Workshift#8, VD : visit containing  Workshift#8}
[Workshift element => { WD: Workshift#9, VD : visit containing  Workshift#9}
===================================
Des-serialized Workshifts array:
[Workshift element => { WD: Workshift#0, VD : visit containing  Workshift#0}
[Workshift element => { WD: Workshift#1, VD : visit containing  Workshift#1}
[Workshift element => { WD: Workshift#2, VD : visit containing  Workshift#2}
[Workshift element => { WD: Workshift#3, VD : visit containing  Workshift#3}
[Workshift element => { WD: Workshift#4, VD : visit containing  Workshift#4}
[Workshift element => { WD: Workshift#5, VD : visit containing  Workshift#5}
[Workshift element => { WD: Workshift#6, VD : visit containing  Workshift#6}
[Workshift element => { WD: Workshift#7, VD : visit containing  Workshift#7}
[Workshift element => { WD: Workshift#8, VD : visit containing  Workshift#8}
[Workshift element => { WD: Workshift#9, VD : visit containing  Workshift#9}
===================================

没有StackOverflow错误。

如果您取消注释该行

// System.out.println(serialized);

输出如下:

[
  {
    "0x1": {
      "visit": "0x2",
      "workshift_description": "Workshift#0"
    },
    "0x2": {
      "workshift": "0x1"
    }
  },
  {
    "0x1": {
      "visit": "0x2",
      "workshift_description": "Workshift#1"
    },
    "0x2": {
      "workshift": "0x1"
    }
  },
  {
    "0x1": {
      "visit": "0x2",
      "workshift_description": "Workshift#2"
    },
    "0x2": {
      "workshift": "0x1"
    }
  },
  {
    "0x1": {
      "visit": "0x2",
      "workshift_description": "Workshift#3"
    },
    "0x2": {
      "workshift": "0x1"
    }
  },
  {
    "0x1": {
      "visit": "0x2",
      "workshift_description": "Workshift#4"
    },
    "0x2": {
      "workshift": "0x1"
    }
  },
  {
    "0x1": {
      "visit": "0x2",
      "workshift_description": "Workshift#5"
    },
    "0x2": {
      "workshift": "0x1"
    }
  },
  {
    "0x1": {
      "visit": "0x2",
      "workshift_description": "Workshift#6"
    },
    "0x2": {
      "workshift": "0x1"
    }
  },
  {
    "0x1": {
      "visit": "0x2",
      "workshift_description": "Workshift#7"
    },
    "0x2": {
      "workshift": "0x1"
    }
  },
  {
    "0x1": {
      "visit": "0x2",
      "workshift_description": "Workshift#8"
    },
    "0x2": {
      "workshift": "0x1"
    }
  },
  {
    "0x1": {
      "visit": "0x2",
      "workshift_description": "Workshift#9"
    },
    "0x2": {
      "workshift": "0x1"
    }
  }

]

这是因为Gson正在替换您的引用,以避免出现堆栈溢出异常。就像 模拟指针

希望能帮助到你。

注意: 请记住复制文件GraphAdapterBuilder.java并更改该行

private final ConstructorConstructor constructorConstructor = new ConstructorConstructor();

private final ConstructorConstructor constructorConstructor = new ConstructorConstructor(instanceCreators);

否则将无法编译。也许现在是固定的。



 类似资料:
  • 我陷入了一个问题,需要在反序列化过程中使用和将解析对象的引用设置为其子对象。 为了描述这个问题,下面是类结构的简单表示。 通过这种结构,我可以通过向我的 提供 来在 中设置,例如: 我知道,我可以添加额外的到我的,但我不知道如何提供我的对象的引用,该对象正在被解析(即时)到对象? 任何帮助将不胜感激!

  • 问题内容: 我有一个名为的类,该类具有一个作为参数的构造函数: 具有以下属性: 并且是我的应用程序的其他类,并具有以下构造函数: ,并且是的子类。 我想用Gson 反序列化数组,所以我写道: 以定义为: 调试时,实例具有正确的“ MainActivity”作为上下文,而其成员变量的上下文为null。 Gson 使用正确的构造函数创建了对象,但使用默认的无参数构造函数创建了实例。我怎样才能解决这个问

  • 我有一个名为PageItem的类,它有一个构造函数,该构造函数将上下文作为参数: 具有以下属性: 新闻提供者(Newsprovider)和主题(Topic)是我的应用程序的其他类,具有以下构造函数:

  • 我有以下POJO: 我可以让GSON将ShapeHolder的实例序列化到JSON。但是当我试图将JSON的字符串反序列化回实例时,我得到错误: 抛出: 所以我看了这里,开始实现我自己的: 但现在我被困住了:我只得到了一个,但我真的需要一个这样我就可以编写如下代码: 我能做什么? 根据@raffian的建议(他/她发布的链接),我实现了一个,与链接中的完全相同(我没有做任何更改)。现在我得到了以下

  • 我可以使用代码1将正确地保存到使用Gson的json字符串中,但是当我尝试用代码2从json字符串中还原对象时,我遇到了错误。我已经搜索了一些资源,似乎我需要注册一个InstanceCreator。 我如何用Kotlin编写一个register an代码?谢谢! 错误 添加 在我使用之后,我使用时可以得到正确的结果,为什么?

  • 我需要将一些html转换为PDF。我遇到了IText7,它看起来是一个很好的解决方案。 我正在. net core 3.1中开发 但是,即使使用它的基本实现,我也会遇到一个错误: 每个人都喜欢这个错误: 对象引用未设置为对象的实例。 内部异常为null,唯一的线索是SerializationStackTraceString是: 在iText. IO. FontCache. cctor() 那么,它