简体   繁体   English

Jackson枚举反序列化

[英]Jackson enum deserialization

I'm trying to deserialize json object using jackson and getting exception 我正在尝试使用杰克逊反序列化json对象并获取异常

`com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException:
Unrecognized field "mobile" (class mypack.JacksonPhoneBuilder), not
marked as ignorable (2 known properties: , "phoneType", "value"])  at
[Source: %filelocation%; line: 8, column: 24] (through reference
chain:
mypack.JacksonAddressListBuilder["addresses"]->mypack.JacksonAddressBuilder["phones"]->mypack.JacksonPhoneBuilder["mobile"])`

This is the object: 这是对象:

{
    "addresses": [
    {
        ...
        "phones": {
            "mobile": "+01234567890"
        }
    },
    ...
    ]
}

Phone.java: Phone.java:

@JsonDeserialize(builder = JacksonBuilder.class)
public class Phone {    
    protected String value;

    protected Type type;

    // setters and getters
}

i've read about jackson enum deserializtion, but there was plain enum and there was used Map. 我读过有关杰克逊枚举反序列化的信息,但是有一个普通的枚举,并且使用过Map。 Obviously, field "mobile" is not represented in model, but it's a enum value, so how can i deserialize it? 显然,“移动”字段未在模型中表示,但这是一个枚举值,那么我如何反序列化它?

Your JacksonPhoneBuilder works the same way as Jackson default deserialization. JacksonPhoneBuilder的工作方式与Jackson默认的反序列化相同。 The problem is that it's able to read phones in following form: 问题在于它能够以以下形式读取电话:

{
    "type": "mobile",
    "value": "+01234130000"
}

However in your json object phones are represented as a subobject which can be seen in Java as a Map<PhoneType, String> . 但是,在您的json对象中,电话被表示为子对象,在Java中可以将其视为Map<PhoneType, String> One of possible solutions is to use a Converter from Map to List (I assume there may be many phones in one address). 一种可能的解决方案是使用“从映射到列表”的转换器(我假设一个地址中可能有很多电话)。

import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.type.TypeFactory;
import com.fasterxml.jackson.databind.util.Converter;

public class PhoneConverter implements Converter<Map<PhoneType, String>, List<Phone>>{

    public List<Phone> convert(Map<PhoneType, String> phonesMap) {
        List<Phone> phones = new ArrayList<Phone>();
        for (PhoneType phoneType : phonesMap.keySet()) {
            phones.add(new Phone(phoneType, phonesMap.get(phoneType)));
        }
        return phones;
    }

    public JavaType getInputType(TypeFactory typeFactory) {
        return typeFactory.constructMapLikeType(Map.class, PhoneType.class, String.class);
    }

    public JavaType getOutputType(TypeFactory typeFactory) {
        return typeFactory.constructCollectionLikeType(List.class, Phone.class);
    }
}

Then in your Address class: 然后在您的Address类中:

public class Address {

    @JsonDeserialize(converter = PhoneConverter.class)
    protected List<Phone> phones;

}

Note that it won't play with your Builders but if you don't do any other custom deserialization then you don't need them - you can rely on Jackson's default behavior. 请注意,它不会与您的构建器一起使用,但是如果您不执行任何其他自定义反序列化,则不需要它们-您可以依赖Jackson的默认行为。

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

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