本文介绍了Jackson:如何在不修改 POJO 的情况下向 JSON 添加自定义属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的应用程序开发 REST 接口,使用 Jackson 将我的 POJO 域对象序列化为 JSON 表示.我想自定义某些类型的序列化,以向 POJO 中不存在的 JSON 表示添加其他属性(例如,添加一些元数据、参考数据等).我知道如何编写自己的 JsonSerializer,但在这种情况下,我需要为 each 显式调用 JsonGenerator.writeXXX(..) 方法我的对象的属性,而我所需要的只是添加一个额外的属性.换句话说,我希望能够写出类似的东西:

I am developing a REST interface for my app using Jackson to serialize my POJO domain objects to JSON representation. I want to customize the serialization for some types to add additional properties to the JSON representation that do not exist in POJOs (e.g. add some metadata, reference data, etc). I know how to write my own JsonSerializer, but in that case I would need to explicitly call JsonGenerator.writeXXX(..) methods for each property of my object while all I need is just to add an additional property. In other words I would like to be able to write something like:

@Override
public void serialize(TaxonomyNode value, JsonGenerator jgen, SerializerProvider provider) {
    jgen.writeStartObject();
    jgen.writeAllFields(value); // <-- The method I'd like to have
    jgen.writeObjectField("my_extra_field", "some data");
    jgen.writeEndObject();
}

或者(甚至更好)在 jgen.writeEndObject() 调用之前以某种方式拦截序列化,例如:

or (even better) to somehow intercept the serialization before the jgen.writeEndObject() call, e.g.:

@Override void beforeEndObject(....) {
    jgen.writeObjectField("my_extra_field", "some data");
}

我以为我可以扩展 BeanSerializer 并覆盖它的 serialize(..) 方法,但它声明为 final 而且我找不到一种创建 BeanSerializer 新实例的简单方法,而无需为其提供所有类型元数据详细信息,实际上复制了 Jackson 的大部分内容.所以我已经放弃这样做了.

I thought I could extend BeanSerializer and override its serialize(..) method but it's declared final and also I couldn't find an easy way to create a new instance of BeanSerializer without providing it with all the type metadata details practically duplicating a good portion of Jackson. So I've given up on doing that.

我的问题是 - 如何自定义 Jackson 的序列化以向特定 POJO 的 JSON 输出添加其他内容,而不会引入太多样板代码并尽可能多地重用默认的 Jackson 行为.

My question is - how to customize Jackson's serialization to add additional stuff to the JSON output for particular POJOs without introducing too much of the boilerplate code and reusing as much as possible of the default Jackson behaviour.

推荐答案

在查看更多 Jackson 源代码后,我得出结论,如果不编写自己的 BeanSerializerBeanSerializerBuilderBeanSerializerFactory 并提供一些扩展点,例如:

After looking more on the Jackson source code I concluded that it's simply impossible to achieve without writing my own BeanSerializer, BeanSerializerBuilder and BeanSerializerFactory and provide some extension points like:

/*
/**********************************************************
/* Extension points
/**********************************************************
 */

protected void beforeEndObject(T bean, JsonGenerator jgen, SerializerProvider provider) throws IOException, JSONException {
    // May be overridden
}

protected void afterStartObject(T bean, JsonGenerator jgen, SerializerProvider provider) throws IOException, JSONException {
    // May be overridden
}

不幸的是,我不得不将整个 JacksonBeanSerializer 源代码复制并粘贴到 MyCustomBeanSerializer,因为前者不是为声明所有的扩展而开发的字段和一些重要的方法(如 serialize(...))作为 final

Unfortunately I had to copy and paste entire Jackson's BeanSerializer source code to MyCustomBeanSerializer because the former is not developed for extensions declaring all the fields and some important methods (like serialize(...)) as final

这篇关于Jackson:如何在不修改 POJO 的情况下向 JSON 添加自定义属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 09:45