2
votes

I have a generic interface with several implementation classes, which I need to serialise and deserialise via Json. I'm trying to get started with Jackson, using full data-binding, without much luck.

The sample code illustrates the problem:

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

The call to writeValueAsString to causes the following exception:

org.codehaus.jackson.map.JsonMappingException: No serializer found for class Test$Success and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS) )

Why is Jackson expecting me to register a serializer - I though the point of full data-binding was that I wouldn't need to do this?

Is the above approach correct?

1

1 Answers

2
votes

First of all, you need to register the specialized type to use it with Jackson using the factory method TypeFactory.constructSpecializedType. Then, the specialized type should be a bean (it should have a default constructor, getters and setters) to deserialize it.

Take a look at these tests clarifiers.

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