杰克逊:引用相同的对象 [英] Jackson: referencing the same object

查看:110
本文介绍了杰克逊:引用相同的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在某些情况下,在正文中序列化或反序列化的数据(例如,JSON正文)包含对同一对象的引用。例如,一个带有玩家列表的JSON主体,以及由这些玩家组成的团队列表:

There are some cases where the data serialized or unserialized in a body, for example, a JSON body, contains references to the same object. For example, a JSON body with a list of players, and a list of teams composed of these players:

{
  "players": [
    { "name": "Player 1" },
    { "name": "Player 2" },
    { "name": "Player 3" },
    { "name": "Player 4" },
    { "name": "Player 5" },
    { "name": "Player 6" },
    { "name": "Player 7" },
    { "name": "Player 8" }
  ],
  "teams": [
    {
      "name": "Team 1",
      "players": [
        { "name": "Player 1"},
        { "name": "Player 2"}
      ]
    },
    {
      "name": "Team 2",
      "players": [
        { "name": "Player 3"},
        { "name": "Player 4"}
      ]
    },
    {
      "name": "Team 3",
      "players": [
        { "name": "Player 5"},
        { "name": "Player 6"}
      ]
    },
    {
      "name": "Team 4",
      "players": [
        { "name": "Player 7"},
        { "name": "Player 8"}
      ]
    }
  ]
}

您可以想象 Player X 指的是同一个对象,但我们可能最终会遇到不需要的情况其中 Player X 由不同的对象表示。

As you can imagine Player X refers to the same object, but potentially we could end up with an unwanted scenario where Player X is represented by different objects.

我想知道这些场景最好和最常用的方法是什么。我可以想到几种方法:

I would like to know what is the best and most common approach to these scenarios. I can think of several ways of doing this:


  1. 播放器添加ID属性上课。我的设计不包含ID,因为它不需要。识别对象的方式是通过引用,如果它们包含在集合中,则通过它们在其中的位置(如果集合具有位置)。这可能被认为是一种不好的做法,但我故意这样做,除非必要,否则我不打算更改它。

  2. 还有一个玩家ID,但不是序列化/反序列化团队作为玩家列表,它只是一个ID列表。 JSON正文中包含的信息会丢失,但我们会有更紧凑的数据。

  3. 坚持我的无ID设计,与上一点类似,我可以有一份职位列表(实际上它实际上是一个玩家ID,因为它们充当识别手段),因此数字 0 将指向玩家列表中第一个位置的玩家。

  4. 更改合约并让玩家拥有唯一名称。有了这个,我们现在可以拥有一个ID而无需向该类添加新属性。但是我认为这是一个坏主意,因为不一定所有玩家都应该有不同的名字。

  1. Add an ID attribute to the Player class. My design does not include an ID becase it is not needed. The ways of identifying an object are by its reference and if they're contained in a collection, by their position in it (if the collection has positions). This might be considered a bad practice, but I did it intentionally and I don't plan on changing it unless necessary.
  2. Also having a player ID, but instead of serializing/deserializing the teams as a list of players, it would just be a list of IDs. Information contained in the JSON body would be lost, but we would have more compact data.
  3. Sticking to my ID-less design, similarly to the previous point, I could have a list of positions (which in practice would actually be a player ID because they serve as identification means), so the number 0 would refer to the player in the first position in the players list.
  4. Changing the contract and having players with unique name. With this we could now have an ID without having to add a new attribute to the class. However I think it is a bad idea because not necessarily all players should have different names.

最好的方法是什么?通常做什么?你有不同的建议吗?

What would be the best approach? What is usually done? Do you have different suggestions?

推荐答案

我选择了定位参考。显然它不一定被认为是一种不好的方法。

I went for positioning references. Apparently it is not considered a bad approach necessarily.

所以JSON主体就是这样的:

So the JSON body would be like this:

{
  "players": [
    { "name": "Player 1" },
    { "name": "Player 2" },
    { "name": "Player 3" },
    { "name": "Player 4" }
  ],
  "teams": [
    [ 3, 1 ],
    [ 0, 2 ]
  ]
}

这篇关于杰克逊:引用相同的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆