Jackson JSON 反序列化 - 合成列表 getter

发布于 2024-11-19 03:44:59 字数 408 浏览 1 评论 0原文

我正在尝试使用 Jackson 反序列化一些最初使用 Jackson 创建的 JSON。该模型有一个合成列表 getter:

public List<Team> getTeams() {
   // create the teams list
}

其中列表不是私有成员,而是动态生成的。现在序列化很好,但在反序列化中使用 getTeams,大概是因为 Jackson 看到一个带有可变列表的 getter 并认为它可以将其用作 setter。 getTeams 的内部依赖于 Jackson 尚未填充的其他字段。其结果是 NPE,即我认为顺序是这里的问题之一,但不是我想解决的问题。

因此,我想做的是注释 getTeams,以便它永远不会用作 setter,而是用作 getter。这可能吗?有什么建议吗?

I'm trying to use Jackson to deserialize some JSON originally created using Jackson. The model has a synthetic list getter:

public List<Team> getTeams() {
   // create the teams list
}

where the list is not a private member, but generated on the fly. Now this serialises fine, but in deserialization uses getTeams, presumably because Jackson sees a getter with a mutable list and thinks it can use it as a setter. The internals of getTeams rely on other fields which Jackson hasn't yet populated. The result of which is a NPE i.e. I think order is one of the problems here but not one I want to solve.

So, what I'd like to do is annotate getTeams so that it's never used as a setter but is used as a getter. Is this possible? Any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

沙沙粒小 2024-11-26 03:44:59

禁用 DeserializationConfig.Feature .USE_GETTERS_AS_SETTERS

mapper.configure(DeserializationConfig.Feature.USE_GETTERS_AS_SETTERS, false);

使用静态导入可以使这一行更短。

或者,如果您希望注释仅配置此属性的内容,而不是如上所述指定全局设置,则将某些内容标记为“团队”的设置器。

public class Foo
{
  @JsonSetter("teams")
  public void asdf(List<Team> teams)
  {
    System.out.println("hurray!");
  }

  public List<Team> getTeams()
  {
    // generate unmodifiable list, to fail if change attempted
    return Arrays.asList(new Team());
  }

  public static void main(String[] args) throws Exception
  {
    ObjectMapper mapper = new ObjectMapper();
    String fooJson = mapper.writeValueAsString(new Foo());
    System.out.println(fooJson);
    // output: {"teams":[{"name":"A"}]}

    // throws exception, without @JsonSetter("teams") annotation
    Foo fooCopy = mapper.readValue(fooJson, Foo.class);
    // output: hurray!
  }
}

class Team
{
  public String name = "A";
}

Disable DeserializationConfig.Feature.USE_GETTERS_AS_SETTERS.

mapper.configure(DeserializationConfig.Feature.USE_GETTERS_AS_SETTERS, false);

Use static import to make this line shorter.

Or, if you want an annotation to just configure things for this one property, and not specify the global setting as above, then mark something as the setter for "teams".

public class Foo
{
  @JsonSetter("teams")
  public void asdf(List<Team> teams)
  {
    System.out.println("hurray!");
  }

  public List<Team> getTeams()
  {
    // generate unmodifiable list, to fail if change attempted
    return Arrays.asList(new Team());
  }

  public static void main(String[] args) throws Exception
  {
    ObjectMapper mapper = new ObjectMapper();
    String fooJson = mapper.writeValueAsString(new Foo());
    System.out.println(fooJson);
    // output: {"teams":[{"name":"A"}]}

    // throws exception, without @JsonSetter("teams") annotation
    Foo fooCopy = mapper.readValue(fooJson, Foo.class);
    // output: hurray!
  }
}

class Team
{
  public String name = "A";
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文