Search code examples
serializationjacksondeserializationjackson-databind

Jackson Serialization Problems


I am having some trouble serializing/deserializing my classes below.

My Data class holds a list of other classes.

When I call the serialize/deserialize methods in the Data class, I get the following error:

Caused by: com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of com.amazon.rancor.storage.types.ChildData: no suitable constructor found, can not deserialize from Object value (missing default constructor or creator, or perhaps need to add/enable type information?)

The error comes from the deserialize method. But I also believe the serialization is not working properly. This is what the serialized Data object looks like:

{childData:[{zipCode:{present:true},countryCode:"US"}]

The Optional field is not being serialized properly even though I have set the objectMapper.registerModule(new Jdk8Module()); field


I can't seem to figure out what I am doing wrong. Maybe I need to change something in ChildData and ChildDataV2 class. But I am not sure what.

Any pointers would be appreciated!


public class Data {
    private List<ChildData> childData;
    private List<ChildDataV2> childDataV2;

    private static ObjectMapper objectMapper;
    static {
        objectMapper = new ObjectMapper();
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        objectMapper.registerModule(new Jdk8Module());
    }

    public Data() { }

    @JsonCreator
    public Data(@JsonProperty("childData") final List<ChildData> childData,
                          @JsonProperty("childDataV2") final List<ChildDataV2> childDataV2) {
        this.childData = childData;
        this.childDataV2 = childDataV2;
    }

    public List<ChildData> getChildData() {
        return childData;
    }
    
    public void setChildData(final List<ChildData> childData) {
        this.childData = childData;
    }

    public List<ChildDataV2> getChildDataV2() {
        return childDataV2;
    }

    public void setChildDataV2(final List<ChildDataV2> childDataV2) {
        this.childDataV2 = childDataV2;
    }

    public String serialize() {
        try {
            return objectMapper.writeValueAsString(this);
        } catch (JsonProcessingException e) {
            throw new RuntimeException("Failed to serialize. Data: " + this, e);
        }
    }

    public Data deSerialize(final String data) {
        try {
            return objectMapper.readValue(data, Data.class);
        } catch (IOException e) {
            throw new RuntimeException("Failed to deserialize. Data" + data, e);
        }
    }
}
public class ChildData {
    private final String countryCode;
    private final Optional<String> zipCode;

    public ChildData(final String countryCode, final Optional<String> zipCode) {
        this.countryCode = countryCode;
        this.zipCode = zipCode;
    }

    public Optional<String> getZipCode() {
        return zipCode;
    }

    public String getCountryCode() {
        return countryCode;
    }
}
public class ChildDataV2 extends ChildData {
    private final Object struct;

    public ChildDataV2(final String cc, final Optional<String> postalCode,
        final Object struct) {
        super(cc, postalcode);
        this.struct = struct;
    }
}

Solution

  • The exception is quite clear right? You need to add a default constructor for ChildData or annotate the existing constructor like this:

    @JsonCreator
    public ChildData(@JsonProperty("countryCode") String countryCode, @JsonProperty("zipCode") Optional<String> zipCode) {
        this.countryCode = countryCode;
        this.zipCode = zipCode;
    }