简体   繁体   中英

Jackson JSON: how to deserialize double to class using setter in java

I want to deserialize (and serialize) something like:

{"myVal":1.0}

To(from) MyClass (without messing with complex structure of myVal):

public class MyClass {
    private MyValue myVal;

    public Double getMyVal() {
        return myVal.getVal();
    }

    public void setMyVal(Double val) {
        myVal = new MyValue();
        myVal.setVal(val);
    } 
}

but deserialization throws exception like

com.fasterxml.jackson.databind.JsonMappingException: Can not find a (Map) Key deserializer for type "MyValue"

I tried various annotations but without success.

I think what you are looking for is a custom deserializer . Essentially, you can write a custom deserializer that will create a MyValue object instead of having to modify said object to make it into a Java bean.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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