Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jackson ObjectMapper using custom Serializers and Deserializers

I've got a class that configures a Jackson ObjectMapper. It adds in some custom serializers and deserializers for my object types as follows:

public class JsonMapperFactory {
    public static ObjectMapper createObjectMapper() {
        final SimpleModule module = new SimpleModule("customerSerializationModule", new Version(1, 0, 0, "static version"));
        addCustomDeserializersTo(module);
        addCustomSerializersTo(module);

        final ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.registerModule(module);
        return objectMapper;
    }
    private static void addCustomSerializersTo(final SimpleModule module) {
        module.addSerializer(DateTime.class, new DateTimeSerializer());
    }
    private static void addCustomDeserializersTo(final SimpleModule objectMapper) {
        objectMapper.addDeserializer(DateTime.class, new DateTimeDeserializer());
    }
}

I've tested my customer serializers within their own test classes, so in my test of this JsonMapperFactory class, I'm trying to simply check that the ObjectMapper created has the expected serializers (or deserializers) This could be achieve by introspecting the ObjectMapper, but it doesn't seem to have any mechanisms to do this.

Does anyone know of a nice way to test that?

For deserializers, I have the following:

private void assertThatObjectMapperUsesCorrectDeserializer(final Class<?> typeClazz, final Class<?> deserializerClazz) throws JsonMappingException {
    final  DeserializationConfig deserializationConfig = this.objectMapper.getDeserializationConfig();
    final JsonDeserializer<Object> deserializer = this.objectMapper.getDeserializerProvider().findTypedValueDeserializer(deserializationConfig, javaTypeFor(typeClazz), null);
    assertThat(deserializer, is(instanceOf(deserializerClazz)));
}
private JavaType javaTypeFor(final Class<?> clazz) {
    return TypeFactory.type(clazz); //deprecated method :(
}

Which is quite verbose and uses deprecated methods.

I'm yet to find a way to do a similar test for the serializers. So I've currently resorted to serializing an object and check it serializes correctly (essentially duplicating the serializer test)

Any ideas are very welcome.

like image 307
Dan Temple Avatar asked Feb 11 '14 15:02

Dan Temple


2 Answers

From the answers & comments provided here, I recently redesigned the class to use builders for both the Module and the ObjectMapper. This allowed me to provide mocks and check that the correct (de)serializers were added to the module and then the module is registered to the object mapper as expected.

Object Mapper Builder:

public class ObjectMapperBuilder {
    ObjectMapper mapper;

    public ObjectMapperBuilder configure(final ObjectMapper mapper) {
        this.mapper = mapper;
        return this;
    }

    public ObjectMapperBuilder withModule(final Module module) {
        this.mapper.registerModule(module);
        return this;
    }

    public ObjectMapper build() {
        return this.mapper;
    }
}

Module Builder:

public class SimpleModuleBuilder {
    SimpleModule module;

    public SimpleModuleBuilder configure(final SimpleModule module) {
        this.module = module;
        return this;
    }

    public <X> SimpleModuleBuilder withSerializer(final Class<X> clazz, final JsonSerializer<X> serializer) {
        this.module.addSerializer(clazz, serializer);
        return this;
    }

    public <X> SimpleModuleBuilder withDeserializer(final Class<X> clazz, final JsonDeserializer<X> deserializer) {
        this.module.addDeserializer(clazz, deserializer);
        return this;
    }

    public SimpleModule build() {
        return this.module;
    }
}

And finally, the new JsonMapperFactory:

public class JsonMapperFactory {

    public static ObjectMapper configureObjectMapper(final ObjectMapper mapper, final SimpleModule module) {
        final SimpleModuleBuilder modulebuilder = new SimpleModuleBuilder();

        final SimpleModule configuredModule = modulebuilder.configure(module)
            .withSerializer(DateTime.class, new DateTimeSerializer())
            .withDeserializer(DateTime.class, new DateTimeDeserializer())
            .build();

        final ObjectMapperBuilder objectMapperBuilder = new ObjectMapperBuilder();
        return objectMapperBuilder.configure(mapper).withModule(configuredModule).build();
    }
}

The factory method is still used within Spring configuration, but the configuration now instantiates the blank Module and ObjectMapper before providing them to the factory methods that then configure them.

like image 140
Dan Temple Avatar answered Sep 30 '22 16:09

Dan Temple


If JsonDeserializer (and DateTimeDeserializer too) was an interface, you could easily "JMock" it, pass mocked instance to JsonMapperFactory#createObjectMapper and then expect exactly 1 invocation of your custom "serialize" method; e.g.

DateTimeSerializer serializer = context.mock(DateTimeSerializer.class);
DateTimeDeserializer serializer = context.mock(DateTimeDeserializer.class);
ObjectMapper mapper = JacksonMapperFactory.createObjectMapper(deserializer, serializer);

exactly(1).of(jsonDeserializer).serialize(myDateTime,
  with(any(JsonGenerator.class),
  with(any(SerializerProvider.class)))

Being a concrete class, you can instead define a new (test-scoped) De/Serializer that extends your custom DateTime(De)serializer, and simply count invocation on that:

private static class DateTimeDeserializerWithCounter extends DateTimeDeserializer {
    public int counter = 0;

    @Override
    public DateTime deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
        counter++;
        return super.deserialize(jsonParser, deserializationContext);
    }
}

@Test
public void usageTest(){
    //init mapper with the above DateTimeDeserializerWithCounter - see below
    mapper.readValue("...", DateTime.class);
    Assert.assertEquals(1, deserializer.counter);
}

Below a snapshot of a more "test-oriented" Factory:

//package visibility, to allow passing different De/Serializers while testing
static ObjectMapper createObjectMapper(JsonDeserializer deserializer, JsonSerializer serializer) {
    final SimpleModule module = new SimpleModule("customerSerializationModule", new Version(1, 0, 0, "static version"));
    module.addDeserializer(DateTime.class, deserializer);
    module.addSerializer(DateTime.class, serializer);

    final ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.registerModule(module);
    return objectMapper;
}

//production method: no-args, as in the original version
public static ObjectMapper createObjectMapper() {
    return createObjectMapper(new DateTimeDeserializer(), new DateTimeSerializer());
}

Hope that helps.

like image 33
PaoloC Avatar answered Sep 30 '22 17:09

PaoloC