简体   繁体   English

使用杰克逊反序列化包含2个具有相同子对象的对象的数组?

[英]Deserialize array that contains 2 objects with the same child using Jackson?

My objective is to avoid the creation of the same object twice. 我的目标是避免两次创建同一对象。 I want to deserialize an array of two objects that contain the same child object, this is the JSON object that I want to deserialize 我想反序列化包含相同子对象的两个对象的数组,这是我想反序列化的JSON对象

  [
     {
        @id: 98,
        relatedPackage: {@id:99, receivedOn:1374012807237, packingTypeFk:1,…}
     },
     {
        @id: 101,
        relatedPackage: {@id:99, receivedOn:1374012807237, packingTypeFk:1,…}
     }
  ]

and this is the POJO 这是POJO

@JsonIdentityInfo(generator=ObjectIdGenerators.IntSequenceGenerator.class, property="@id")
public class Package extends StampedModel {
...
}

In the last code you can see that I put the annotation @JsonIdentityInfo to achieve my objective but it doesn't do the job. 在最后的代码中,您可以看到我使用了@JsonIdentityInfo注释来实现我的目标,但是它没有完成任务。 Always that I send this array to the server, it creates two diferent packages. 总是将这个数组发送到服务器,它会创建两个不同的程序包。

What am I doing Wrongo? 我在做什么Wrongo?

To solve the problem I need to send the first related package only one time, and the next times send only the ID number. 为了解决该问题,我只需要发送一次第一个相关的程序包,而下一次仅发送ID号。 ie. 即。

[
   {
      @id: 98,
      relatedPackage: {@id:99, receivedOn:1374012807237, packingTypeFk:1,…}
   },
 {
      @id: 101,
      relatedPackage: 99
   }
]

As far as I know, the "identity" information does not enforce instance identity. 据我所知,“身份”信息不强制执行实例身份。 It only ensures that two object conforming to that identity are regarded as equal. 它仅确保符合该身份的两个对象被视为相等。

You can implement custom deserializers with Jackson . 您可以使用Jackson实现自定义反序列化器 Then you would also implement class instance control (either through an instance manager, or within the value classes themselves). 然后,您还将实现类实例控制 (通过实例管理器或在值类本身中)。 HTH HTH

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM