简体   繁体   English

Jackson 使用 @JsonRootName 反序列化具有额外属性的 JSON

[英]Jackson deserialize JSON with extra properties using @JsonRootName

I want to deserialize an object which is annotated with @JsonRootName .我想反序列化一个用@JsonRootName注释的对象。 However the JSON in which the object is transported contains another extra property.然而,传输对象的 JSON 包含另一个额外的属性。 As a result Jackson is complaining with:结果杰克逊抱怨:

com.fasterxml.jackson.databind.exc.MismatchedInputException: Unexpected token (FIELD_NAME), expected END_OBJECT: Current token not END_OBJECT (to match wrapper object with root name 'account'), but FIELD_NAME at [Source: (ByteArrayInputStream); line: 1, column: 26] com.fasterxml.jackson.databind.exc.MismatchedInputException: Unexpected token (FIELD_NAME), expected END_OBJECT: Current token not END_OBJECT (to match wrapper object with root name 'account'), but FIELD_NAME at [Source: (ByteArrayInputStream); line: 1, column: 26] . com.fasterxml.jackson.databind.exc.MismatchedInputException: Unexpected token (FIELD_NAME), expected END_OBJECT: Current token not END_OBJECT (to match wrapper object with root name 'account'), but FIELD_NAME at [Source: (ByteArrayInputStream); line: 1, column: 26]

Apparently deserialization of @JsonRootName annotated objects works ONLY if that object is the sole property in JSON file - since it's not expecting the "lastTransactionID" to be there.显然, @JsonRootName注释对象的反序列化仅在该对象是 JSON 文件中的唯一属性"lastTransactionID" - 因为它不希望"lastTransactionID"在那里。

Here's my Account class:这是我的Account类:

@JsonRootName("account")
public class Account {
    private String id;
}

This is the JSON I need to deserialise:这是我需要反序列化的 JSON:

{
  "account": {
    "id": "1234"
  },
  "lastTransactionID": "1"
}

Since I'm using spring I have this setup also spring.jackson.deserialization.unwrap_root_value=true .因为我使用的是 spring,所以我也有这个设置spring.jackson.deserialization.unwrap_root_value=true

Is there any way to solve this without:有没有办法解决这个问题:

  • having to write a custom deserializer?必须编写自定义解串器?

OR或者

  • intercepting the response and stripping it of the extra property before deserialization takes place?在反序列化发生之前拦截响应并剥离它的额外属性?

It looks like simplest way to solve this issue is create wrapper for Account class and deserialise json as usual with disabled DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES feature.看起来解决这个问题的最简单方法是为Account类创建包装器,并像往常一样使用禁用的DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES功能反序列化 json。

public static class Wrapper {
    public Account account;
}

In this case other fields will be ignored.在这种情况下,其他字段将被忽略。

It's not a nice solution, I know, but it solve a problem.我知道这不是一个好的解决方案,但它解决了一个问题。

We can use ObjectMapper to Map json to java Objects.我们可以使用 ObjectMapper 将 json 映射到 java 对象。

public Account jsonToObject(Map<String, Object> map){
    ObjectMapper objectMapper = new ObjectMapper();
    Account account = objectMapper.convertvalue(map.get("account"),Account.class);
    return account;
}

You can use JsonIgnoreProperties(ignoreUnknown=true) annotation on your Account class.您可以在 Account 类上使用JsonIgnoreProperties(ignoreUnknown=true)注释。 Please refer below link for more details.请参阅以下链接了解更多详情。
https://www.thetechnojournals.com/2019/10/entity-object-conversion-to-dto-object.html https://www.thetechnojournals.com/2019/10/entity-object-conversion-to-dto-object.html

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

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