使用Jackson序列化通用接口子类

时间:2013-02-07 21:58:26

标签: java json generics interface jackson

我有一个带有几个实现类的通用接口,我需要通过Json进行序列化和反序列化。我正试图开始使用Jackson,使用完整的数据绑定,没有太多运气。

示例代码说明了问题:

import org.codehaus.jackson.map.*;
import org.codehaus.jackson.map.type.TypeFactory;
import org.codehaus.jackson.type.JavaType;

public class Test {

    interface Result<T> {}

    static class Success<T> implements Result<T> {
        T value;
        T getValue() {return value;}
        Success(T value) {this.value = value;}
    }

    public static void main(String[] args) {
        Result<String> result = new Success<String>("test");
        JavaType type = TypeFactory.defaultInstance().constructParametricType(Result.class, String.class);
        ObjectMapper mapper = new ObjectMapper().enableDefaultTyping();
        ObjectWriter writer = mapper.writerWithType(type);
        ObjectReader reader = mapper.reader(type);

        try {
            String json = writer.writeValueAsString(result);
            Result<String> result2 = reader.readValue(json);
            Success<String> success = (Success<String>)result2;
        } catch (Throwable ex) {
            System.out.print(ex);
        }
    }
}

writeValueAsString的调用会导致以下异常:

org.codehaus.jackson.map.JsonMappingException:没有找到类Test $ Success的序列化器,也没有发现创建BeanSerializer的属性(为了避免异常,请禁用SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS))

杰克逊为什么期望我注册一个序列化程序 - 我认为完全数据绑定的重点是我不需要这样做?

上述方法是否正确?

1 个答案:

答案 0 :(得分:2)

首先,您需要使用工厂方法TypeFactory.constructSpecializedType注册专用类型以与Jackson一起使用。然后,专用类型应该是一个bean(它应该有一个默认的构造函数,getters和setter)来反序列化它。

看看这些测试澄清剂。

@Test
public void canSerializeParametricInterface() throws IOException {
    final ObjectMapper mapper = new ObjectMapper().enableDefaultTyping();
    final JavaType baseInterface = TypeFactory.defaultInstance().constructParametricType(Result.class, String.class);
    final JavaType subType = TypeFactory.defaultInstance().constructSpecializedType(baseInterface, Success.class);
    final ObjectWriter writer = mapper.writerWithType(subType);
    final String json = writer.writeValueAsString(Success.create("test"));
    Assert.assertEquals("{\"value\":\"test\"}", json);
}

@Test
public void canDeserializeParametricInterface() throws IOException {
    final ObjectMapper mapper = new ObjectMapper().enableDefaultTyping();
    final JavaType baseInterface = TypeFactory.defaultInstance().constructParametricType(Result.class, String.class);
    final JavaType subType = TypeFactory.defaultInstance().constructSpecializedType(baseInterface, Success.class);
    final ObjectReader reader = mapper.reader(subType);
    final Success<String> success = reader.readValue("{\"value\":\"test\"}");
    Assert.assertEquals("test", success.getValue());
}

public static interface Result<T> {
}

public static class Success<T> implements Result<T> {

    private T value;

    public static <T> Success<T> create(T value) {
        final Success<T> success = new Success<T>();
        success.value = value;
        return success;
    }

    public T getValue() {
        return value;
    }

    public void setValue(T value) {
        this.value = value;
    }
}