Odd type casting error with com.fasterxml.jackson(.databind) v3 when deserializing a class deriving from `java.util.Properties`

1 day ago 1
ARTICLE AD BOX

Dificult to know with so small info

I know you mentioned this isn't a polymorphism issue but I think it almost is (Properties inherits from Hashtable and the Jackson deserializers implementations.

From what I've heard, Jackson 3 is more strict and explicit with deserialization and no longer does automatic conversions between parent and child classes Properties - NodeProperties.

I think a friend has a similar problem. try this:

@JsonDeserialize(as = NodeProperties.class) public class NodeProperties extends Properties { public NodeProperties() { // Not pretty sure it is needed super(); } // Your fantastic methods :) }

If not, you can write your own serializer extending JsonDeserializer or ValueDeserializer:

public class NodePropertiesDeserializer extends ValueDeserializer<NodeProperties> { @Override public NodeProperties deserialize(JsonParser p, DeserializationContext ctxt) throws IOException { Properties props = p.readValueAs(Properties.class); NodeProperties nodeProps = new NodeProperties(); nodeProps.putAll(props); return nodeProps; } }

And add this to your NodeProperties class: @JsonDeserialize(using = NodePropertiesDeserializer.class). Also you can add it manualy as a deserializer if you do not like the anotation.

As another option, I never tested this, but as you are using the serialization of your own data structures I think you can use @JsonTypeInfo annotation that might be cleaner and is one line but it makes the jsons more dirty and attached to the framework.

import tools.jackson.databind.annotation.JsonTypeInfo; @JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, include = JsonTypeInfo.As.PROPERTY, property = "@class") public class NodeProperties extends Properties { // Completely useful stuff here }

This should include inside the json of the class a property on the serialization process that tells Jackson what class should be mapped on the deserialization. Something like

{ "@class": "my.application.utils.NodeProperties", "timeout": 5000, "host": "localhost" }
Read Entire Article