如何使用jackson序列化瞬态字段? [英] How to serialize transient fields using jackson?

查看:363
本文介绍了如何使用jackson序列化瞬态字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们使用与Jackson的JSON序列化来公开系统的内部状态以调试属性。

We use JSON serialization with Jackson to expose internal state of the system for debugging properties.

默认情况下,jackson不会序列化瞬态字段 - 但我也希望将它们序列化。
如何序列化这些字段?

By default jackson does not serialize transient fields - but I wish to serialize them as well. How can I serialize these fields?

我知道的一种方法是为这些字段提供getter - 但我不想这样做,因为我有一些我不想被调用的getX方法(例如,有一些getter改变了对象状态)。

One way I know is to supply a getters for these fields - but I don't want to do that, as I have some getX methods that I don't want to be invoked ( for instance, there are some getters that change the objects state ).

我知道我可以创建一个注释,但我真的想避免它。

I know I could create an annotation, but I really want to avoid it.

所以我的问题是:
有没有办法设置jackson序列化全部对象字段?包括瞬态的。

So my question is: Is there a way to setup jackson to serialize all the objects fields? include transient ones.

推荐答案

我认为Jackson不支持任何类型的配置来使其能够序列化瞬态字段。添加该功能有一个悬而未决的问题,但它已经过时并且尚未得到解决(据我所知): http ://jira.codehaus.org/browse/JACKSON-623

I don't think Jackson supports any type of configuration to enable it to serialize a transient field. There's an open issue to add that feature, but it's old and hasn't been addressed (as far as I can tell): http://jira.codehaus.org/browse/JACKSON-623


所以我的问题是:有没有办法设置jackson序列化所有
对象字段?包括短暂的。

So my question is: Is there a way to setup jackson to serialize all the objects fields? include transient ones.

所以回答你的问题,没有。

So to answer your question, no.

其他一些Java JSON工具(例如GSON )支持配置选项以序列化瞬态字段。如果您可以使用其他工具,您可以查看(对于GSON,请参阅: https:// sites。 google.com/site/gson/gson-user-guide )。

Some other Java JSON tools, such as GSON do support a configuration option to serialize transient fields. If you can use another tool, you might look into that (for GSON, see: https://sites.google.com/site/gson/gson-user-guide).

要扩展一点,您可以尝试不同的方法。

To expand a little, you might try a different approach.

首先,您不应该尝试序列化瞬态字段。瞬态的定义是不要序列化这个。不过,我可以想到一些可能需要的特定情况,或者至少方便(比如使用代码时无法修改等)。尽管如此,在99%的案例中,答案是不这样做。如果需要序列化,请更改字段,使其不是瞬态的。如果您有多个使用相同字段的上下文,并且希望将它序列化为一个(例如JSON),而不是另一个(例如java.io)序列化,那么您应该为这种情况创建一个自定义序列化程序你不想要它,而不是滥用关键字。

First, You shouldn't try to serialize a transient field. After all the definition of transient is "don't serialize this." Nevertheless I can think of a few specific situations where it might be necessary, or at least convenient (like when working with code you can't modify or such). Still, in 99% of cases, the answer is don't do that. Change the field so that it's not transient if you need to serialize it. If you have multiple contexts where you use the same field, and you want it serialized in one (JSON, for example), and not serialized in another (java.io, for example) then you should create a custom serializer for the case where you don't want it, rather than abuse the keyword.

其次,关于使用getter并使用一些改变对象状态的getter,你应该尽量避免这种情况。这可能会导致各种意想不到的后果。而且,从技术上讲,这不是一个吸气剂,那是一个制定者。我的意思是,如果它改变状态,你有一个mutator(setter)而不是accessor(getter),即使你按照get约定命名并返回一些东西。

Second, as to using a getter and having "some getters that change the objects state," you should try to avoid that too. That can lead to various unintended consequences. And, technically, that's not a getter, that's a setter. What I mean is, if it mutates state, you've got a mutator (setter) rather than accessor (getter), even if you name it following the "get" convention and return some stuff.

这篇关于如何使用jackson序列化瞬态字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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