在JSON序列化之前更改对象 [英] Change Object just before JSON serialization

查看:98
本文介绍了在JSON序列化之前更改对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在JSON序列化之前更改对象。为此,我使用change方法创建了一个接口,任何实现此接口的类都将尝试自行更改。 (是的,可能这样做不是最优的,但例如为了清酒

I want to change an object just before JSON Serialization. To do it, I've created an interface, with change method, and any class that implements this interface will "try" to change itself. (yes, probably doing it like this is not optimal, but for example sake will do)

@JsonSerialize(using = ChangesValuesSerializer.class)
public interface ChangesValues {
    void changeValues();
}

现在, ChangesValuesSerializer class我正在实现 serialize 方法。并想知道,我怎么能说杰克逊,把它称为对象内置的序列化器。

Now, is ChangesValuesSerializer class I'm implementing serialize method. And want to know, how can I say Jackson, to call it's built in serializer on object.

class ChangesValuesSerializer extends JsonSerializer<ChangesValues> {

    @Override
    public void serialize(ChangesValues changesValues, JsonGenerator jsonGenerator,
            SerializerProvider serializerProvider) throws IOException,
            JsonProcessingException {

        changesValues.changeValues();

        // ***
        // ->> Just call Jacksons default serializer
        // ***
    }

}


推荐答案

您可以使用 BeanSerializerModifier 来获取对默认序列化程序的访问权限,并使用您自己的委托覆盖它。在委托内,您可以更改序列化对象并调用默认序列化程序。下面是一个示例:

You can use BeanSerializerModifier to get the access to the default serializer and override it with your own delegate. Inside the delegate you can change your serialised object and call the default serializer. Here is an example:

public class JacksonSerializeModifier {
    public static class Bean {
        public final String value;

        public Bean(final String value) {
            this.value = value;
        }

        public void foo() {
            System.out.println("foo() invoked");
        }
    }

    private static class MyBeanSerializerModifier extends BeanSerializerModifier {
        @Override
        public JsonSerializer<?> modifySerializer(
                final SerializationConfig serializationConfig,
                final BeanDescription beanDescription,
                final JsonSerializer<?> jsonSerializer) {
            return new ModifyingSerializer((JsonSerializer<Object>) jsonSerializer);
        }
    }

    private static class ModifyingSerializer extends JsonSerializer<Object> {
        private final JsonSerializer<Object> serializer;

        public ModifyingSerializer(final JsonSerializer<Object> jsonSerializer) {
            this.serializer  = jsonSerializer;
        }

        @Override
        public void serialize(
                final Object o,
                final JsonGenerator jsonGenerator,
                final SerializerProvider serializerProvider)
        throws IOException {
            if (o instanceof Bean) {
                ((Bean) o).foo();
            }
            serializer.serialize(o, jsonGenerator, serializerProvider);
        }
    }

    public static void main(String[] args) throws JsonProcessingException {
        SimpleModule module = new SimpleModule();
        module.setSerializerModifier(new MyBeanSerializerModifier());
        ObjectMapper mapper = new ObjectMapper();
        mapper.registerModule(module);
        System.out.println(mapper.writeValueAsString(new Bean("abc")));
    }
}

输出:

foo() invoked
{"value":"abc"}

这篇关于在JSON序列化之前更改对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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