如何触发对Jackson中实现JsonSerializable的类的.serializeWithType()的调用? [英] How to trigger calls to .serializeWithType() of a class implementing JsonSerializable in Jackson?

查看:778
本文介绍了如何触发对Jackson中实现JsonSerializable的类的.serializeWithType()的调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是Jackson 2.2.x.

This is Jackson 2.2.x.

我有一个实现 JsonSerializable ;有两种方法可以实现此接口, serialize() serializeWithType()

我想测试这个类的{de,}序列化,我可以轻松地触发对 serialize()的调用;但是, serializeWithType()

I want to test {de,}serialization of this class, and I can trigger calls to serialize() easily; not, however, serializeWithType().

后一种方法的javadoc 表示此方法被称为


[...] 当序列化中包含其他类型信息时,可以使用反序列化。

[...] when additional type information is expected to be included in serialization, for deserialization to use.

我只是不明白这意味着什么...

I just don't understand what this means...

如何设置测试环境以便调用此方法?请注意,要序列化的JSON可以是除object之外的任何类型(即boolean,number,string,array都是有效类型)。

How do I set up a test environment so that this method be called? Note that the JSON to be serialized can be of any type except object (ie, boolean, number, string, array are all valid types).

推荐答案

当你想使用多态时使用这个方法

This method is used when you want to use polymorphism

public class A {
    ...
}

public class B extends A {
    ...
}

public class C extends A {
    ...
}

如果您序列化C的实例然后尝试反序列化结果json但只知道它是A的子类型:

If you serialize an instance of C and then try to deserialize the resulting json but only knowing that its a sub-type of A :

final ObjectMapper objectMapper = new ObjectMapper();
final String json = objectMapper.writeValueAsString(new C());
final A deserialized = objectMapper.readValue(json, A.class);

您需要在生成的JSON中存储一些内容以保留序列化对象的真实类型。

You need something to be stored within the resulting JSON to keep the real type of the serialized object.

可以在类上使用 @JsonTypeInfo 启用此功能,也可以通过调用 enableDefaultTyping < ObjectMapper 上的/ code>。

This can be enabled either using @JsonTypeInfo on your class, or by calling enableDefaultTyping on your ObjectMapper.

这是一个使用JUnit&的示例测试用例。 Mockito

This is a sample test case using JUnit & Mockito

import com.fasterxml.jackson.databind.JsonSerializable;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Test;

import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

public class SerializeWithTypeTest {

    private JsonSerializable serializable = mock(JsonSerializable.class);

    @Test
    public void shouldCallSerializeWithType() throws Exception {
        final ObjectMapper objectMapper = new ObjectMapper().enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);

        objectMapper.writeValueAsString(serializable);

        // make sure serializeWithType is called once
        verify(serializable, times(1)).serializeWithType(any(), any(), any());

    }

}

这篇关于如何触发对Jackson中实现JsonSerializable的类的.serializeWithType()的调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆