杰克逊:如何在不修改POJO的情况下向JSON添加自定义属性 [英] Jackson: How to add custom property to the JSON without modifying the POJO

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

问题描述

我正在为我的应用程序开发一个REST接口,使用Jackson将我的POJO域对象序列化为JSON表示。我想为某些类型自定义序列化,以向POJO中不存在的JSON表示添加其他属性(例如,添加一些元数据,参考数据等)。我知道如何编写自己的 JsonSerializer ,但在这种情况下我需要显式调用 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();
}

或(甚至更好)以某种方式拦截<$ c $之前的序列化c> 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 并覆盖其序列化(..)方法,但它被声明为 final 而且我找不到创建 BeanSerializer 的新实例的简单方法,不提供所有类型元数据细节,实际上复制了杰克逊的一部分。所以我放弃了这样做。

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的序列化以向JSON输出中添加其他内容特别是POJO,没有引入太多的样板代码,并尽可能多地重用默认的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 源代码中,我得出结论,如果不编写自己的 BeanSerializer BeanSerializerBuilder BeanSerializerFactory 并提供一些扩展点,例如:

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
}

不幸的是我不得不复制并粘贴整个 Jackson BeanSerializer 源代码到 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

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

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