Jackson Serialization of Class with Generic Property losing type information of the generic property

sweetfa :

I am having difficulty getting the type information for a generic property to be maintained when I use Jackson objectMapper to serialise the payload to a string value.

The result I would like to achieve is below.

{"@type":"BlockChainWrapper","@id":1,"payload":{"@type":"TestClassA","@id":2,"helloWorld":"Hello World"},"nonce":255,"signature":"SGVsbG8gV29ybGQ="}

The result I actually get is as below (noting the missing @type info for the TestClassA.

{"@type":"BlockChainWrapper","@id":1,"payload":{"@id":2,"helloWorld":"Hello World"},"nonce":255,"signature":"SGVsbG8gV29ybGQ="}

The test I am using is:

class BlockChainWrapperTest extends Specification {

    def objectMapper = JacksonConfiguration.createObjectMapper()

    def "test Json marshalling"() {
        given: "A test payload"
        def payload = new BlockChainWrapper<TestClassA>(
                payload: new TestClassA(),
                nonce: 255,
                signature: "Hello World".getBytes(Charset.defaultCharset())
        )

        when:
        //ObjectWriter w = objectMapper.writerFor(new TypeReference<BlockChainWrapper<TestClassA>>() { });
        //def result = w.writeValueAsString(payload)
        def result = objectMapper.writeValueAsString(payload)

        then:
        assert result == "{\"@type\":\"BlockChainWrapper\",\"@id\":1,\"payload\":{\"@type\":\"TestClassA\",\"@id\":2,\"helloWorld\":\"Hello World\"},\"nonce\":255,\"signature\":\"SGVsbG8gV29ybGQ=\"}"
    }

    def "test TestClassA generates correct JSON"() {
        given:
        def payload = new TestClassA()

        when:
        def result = objectMapper.writeValueAsString(payload)

        then:
        assert result == "{\"@type\":\"TestClassA\",\"@id\":1,\"helloWorld\":\"Hello World\"}"
    }
}

There are two versions in the "test JSON Marshalling" test. Both the commented out version, and the uncommented version produce exactly the same results.

The second test validates that TestClassA generates the correct type information in its own right.

The issue only becomes an issue when the parent class with the Generic type is incorporated, subsequently losing the type information, and causing deserialisation to return a LinkedHashMap for the contents of TestClassA instead.

TestClassA looks like:

@JsonRootName("TestClassA")
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "@type")
@JsonIdentityInfo(generator = ObjectIdGenerators.IntSequenceGenerator.class)
public class TestClassA implements Serializable {

    private String helloWorld = "Hello World";
}

The generic class looks like:

@JsonRootName("BlockChainWrapper")
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include= JsonTypeInfo.As.PROPERTY, property = "@type")
@JsonIdentityInfo(generator = ObjectIdGenerators.IntSequenceGenerator.class)
public class BlockChainWrapper<T> implements Serializable {

    private T payload;

For completeness the Jackson ObjectMapper configuration is

var mapper = new Jackson2ObjectMapperBuilder()
    .createXmlMapper(false)
    .modules(new JavaTimeModule(), new Jdk8Module())
    .serializationInclusion(JsonInclude.Include.NON_NULL)
    .build();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
mapper.configure(JsonGenerator.Feature.WRITE_BIGDECIMAL_AS_PLAIN, true);
mapper.configure(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS, true);
mapper.configure(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS, false);
mapper.configure(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE, false);
mapper.configure(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS, false);
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
Gaetano :

I think I've fixed your problem. I've created a github project with the solution. https://github.com/GaetanoPiazzolla/stackoverflow-question-61025761

In short, you should put the @JsonTypeInfo annotation also on your Payload field:

@JsonTypeInfo(use=JsonTypeInfo.Id.NAME, include=JsonTypeInfo.As.PROPERTY, property="@type")
private T payload;

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=407491&siteId=1