【问题标题】:Jackson Serialization of Class with Generic Property losing type information of the generic property具有通用属性的类的杰克逊序列化丢失了通用属性的类型信息
【发布时间】:2020-04-04 08:50:29
【问题描述】:

当我使用 Jackson objectMapper 将有效负载序列化为字符串值时,我很难获取要维护的通用属性的类型信息。

我想要达到的结果如下。

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

我实际得到的结果如下(注意 TestClassA 缺少 @type 信息。

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

我正在使用的测试是:

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\"}"
    }
}

“test JSON Marshalling”测试有两个版本。注释掉的版本和未注释的版本产生完全相同的结果。

第二个测试验证 TestClassA 本身生成了正确的类型信息。

只有在合并了具有 Generic 类型的父类时才会出现问题,随后会丢失类型信息,并导致反序列化为 TestClassA 的内容返回 LinkedHashMap。

TestClassA 看起来像:

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

    private String helloWorld = "Hello World";
}

泛型类看起来像:

@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;

为了完整起见,Jackson ObjectMapper 配置为

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);

【问题讨论】:

    标签: java jackson jackson-databind


    【解决方案1】:

    我想我已经解决了你的问题。 我已经使用该解决方案创建了一个 github 项目。 https://github.com/GaetanoPiazzolla/stackoverflow-question-61025761

    简而言之,您应该将 @JsonTypeInfo 注释也放在您的 Payload 字段中:

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

    【讨论】:

      猜你喜欢
      • 2018-07-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-11
      • 1970-01-01
      • 1970-01-01
      • 2016-10-15
      相关资源
      最近更新 更多