Jackson ObjectMapper使用自定义序列化器和反序列化器

时间:2014-02-11 15:45:33

标签: java testing serialization jackson deserialization

我有一个配置Jackson ObjectMapper的类。它为我的对象类型添加了一些自定义序列化器和反序列化器,如下所示:

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

我已经在自己的测试类中测试了我的客户序列化程序,所以在我对这个JsonMapperFactory类的测试中,我试图简单地检查创建的ObjectMapper是否具有预期的序列化程序(或反序列化程序)通过内省ObjectMapper来实现,但它似乎没有任何机制来实现这一点。

有没有人知道测试它的好方法?

对于反序列化器,我有以下内容:

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 :(
}

这是非常详细的,并使用弃用的方法。

我还没有找到对序列化程序进行类似测试的方法。所以我现在使用序列化对象并检查它是否正确序列化(基本上是复制序列化程序测试)

非常欢迎任何想法。

2 个答案:

答案 0 :(得分:6)

从答案&amp;这里提供的评论,我最近重新设计了类,以便为ModuleObjectMapper使用构建器。这允许我提供模拟并检查是否将正确的(de)序列化程序添加到模块中,然后按预期将模块注册到对象映射器。

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

模块构建器:

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

最后,新的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();
    }
}

工厂方法仍在Spring配置中使用,但配置现在实例化空白ModuleObjectMapper,然后再将它们提供给工厂方法,然后再配置它们。

答案 1 :(得分:1)

如果JsonDeserializer(和DateTimeDeserializer也是一个接口),您可以轻松地“JMock”它,将模拟实例传递给JsonMapperFactory#createObjectMapper,然后期望完全调用您的自定义“序列化” “ 方法;例如

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

作为一个具体的类,你可以改为定义一个新的(测试范围的)De / Serializer来扩展你的自定义DateTime(De)序列化程序,并简单地计算对它的调用:

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

下面是一个更“以测试为导向”的工厂的快照:

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

希望有所帮助。