简体   繁体   English

我如何教杰克逊从两个分离的 JSON 对象中获取相同的字段?

[英]How do I teach Jackson to source the same field from two disjunctive JSON objects?

I have the case where I have some existing data which is present in multiple forms, but needs to be read into the same class.我有一些现有数据以多种形式存在但需要读入同一个类的情况。

For example, given the class:例如,给定类:

@Getter
@NoArgsConstructor
@AllArgsConstructor
public class Item {

  private UUID itemId;
}

I might have some JSON that is well-formed and is parseable out-of-the-box (eg List<Item> ):我可能有一些格式正确且开箱即用可解析的 JSON(例如List<Item> ):

{
  "items": [
    {
      "itemId": "<uuid>"
    }
  ]
}

but also some which is not:但也有一些不是:

{
  "items": [
    {
      "someItemId": "<uuid>"
    }
  ]
}

I do not have source JSON which contains both of these fields at the same time.我没有同时包含这两个字段的源 JSON。

I tried to do this using a custom deserialization handler as described in this question, but my use case is a bit different since I will be essentially doing something like:我尝试使用this question中描述的自定义反序列化处理程序来做到这一点,但我的用例有点不同,因为我基本上会做类似的事情:

try {
  Item item = defaultDeserializer.deserialize(...);
} catch (UnrecognizedPropertyException e) {
  // try to rebuild object manually by traversing the tree
}

which would be rather difficult to get right since I can't let Jackson do the heavy lifting anymore.这将是相当困难的,因为我不能让杰克逊再做繁重的工作了。 Are there alternative ways?有替代方法吗? Is there perhaps an annotation-based approach that would allow something like "source this field from either one of these JSON fields, but not both"?是否可能有一种基于注释的方法可以允许“从这些 JSON 字段中的任何一个字段中获取此字段,但不能同时从两者中获取该字段”之类的东西?

You can use @JsonAlias like this:您可以像这样使用@JsonAlias

@Getter
@NoArgsConstructor
@AllArgsConstructor
public class Item {

  @JsonAlias("someItemId")
  private UUID itemId;
}

暂无
暂无

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

相关问题 使用jackson从改造中反序列化json,其中相同的变量名称可以表示两个不同的对象 - Deserializing json from retrofit using jackson where same variable name can represent two different objects 如何从同一文本字段中获得两个值? - How do I get two values from same text field? 使用Jackson JSON Generator,如何将多个对象写入一个字段? - Using Jackson JSON Generator, how can I write multiple objects to one field? 使用Jackson在JSON响应中反嵌序列化嵌套对象中的字段 - Deserializing field from nested objects within JSON response with Jackson 如何使用Android上的Jackson将JSON数组解析为不同的对象? - How do I parse a JSON array into different objects using Jackson on Android? 如何使用 jackson 反序列化包含对象数组的 JSON 表示? - How do I deserialize a JSON representation containing array of objects using jackson? 如何用Jackson为两个对象创建JSON合并补丁文档 - How to create a JSON merge patch document for two objects with Jackson 如何使用Jackson来反序列化此JSON? - How do I deserialize this JSON using Jackson? 我怎样才能在 JAVA 中的 map 与 JACKSON 来自 Z0ECD11C1D7A287401D148A23 的数组包含不同类型的对象? - How can I map in JAVA with JACKSON an array from a JSON which can contain differently typed objects as items? 如何使用Jackson JSON批注抑制空对象? - How can I use Jackson JSON annotations to suppress empty objects?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM