如何表示“未设定"? Java成员,以便可以使用Jackson将这些序列化未设置为JSON [英] How to represent "unset" Java members so that they can be serialized unset in JSON with Jackson

查看:124
本文介绍了如何表示“未设定"? Java成员,以便可以使用Jackson将这些序列化未设置为JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个系统,其中null字段与JSON中的缺少字段明显不同.用Java进行Jackson序列化的最佳方式是什么?

I have a system where explicitly null fields are different from absent fields in JSON. What would be the best way to represent this in Java for Jackson serialization?

例如,我有一个数据对象

For example, I have a data object like

class Data {
  public String string;
  public String other;
}

默认情况下,Jackson会序列化空字段,因此new ObjectMapper().writeValueAsString(new Data())会导致{"string":null,"other":null}.但是,我希望由于字段未设置"而导致{}.显然,在Java中,它们为null,但是我正在寻找一种解决方法.

By default, Jackson serializes null fields so new ObjectMapper().writeValueAsString(new Data()) results in {"string":null,"other":null}. However, I would like this to result in {} as the fields were "unset." Obviously, in java they are null, but I'm looking for a way around that.

如果我要执行new ObjectMapper().wirteValueAsString(new Data(null, null)),我希望那个产生{"string":null,"other":null}.而且只有字符串"字段可产生{"string":"value"}.

If I were to do new ObjectMapper().wirteValueAsString(new Data(null, null)), I want that to result in {"string":null,"other":null}. And only the "string" field to result in {"string":"value"}.

我现在唯一想到的方法是为每个字段使用布尔值,并使用自定义序列化程序检查布尔值,但这看起来很混乱.有没有更简单的方法可以做到这一点?

The only way I can think of right now is to have booleans for each field and use a custom serializer to check the booleans but that seems pretty messy. Is there an easier way to do this?

这与不序列化空值无关.如果该字段明确设置为null,我将想要 null值进行序列化.

This is not about not serializing null values. I want null values to be serialized if the field was explicitly set to null.

我想也许我可以使用Optionals和Include.NON_ABSENT,但是不幸的是,这也不起作用,因为Jackson将对null的Optional引用与Optional.empty()相同.

I thought maybe I could use Optionals and Include.NON_ABSENT, but unfortunately this does not work either as Jackson treats an Optional reference to null the same as Optional.empty().

推荐答案

仅通过配置杰克逊就不会实现这一目标.

You will not achieve that only by configuring jackson.

您要说的是要区分逻辑设置的null和默认的init值.这是您的代码需要处理的事情:如何区分这两种情况.如果您使用的是setter,这就像在每个字段上使用true/false设置标志一样容易(您可以使用其他一些不那么麻烦的方法,但不在此问题范围之内).

You are saying that you want to differentiate a null being set by your logic from a null being just the default init value. That's something your code will need to handle: how to differentiate these 2 scenarios. If you are using setters this can be as easy as to have flags for each field with true/false (you could have other less cumbersome approaches, but out of the scope of this question).

实现标记后,请为您的类编写自己的序列化程序,以适当地处理该标记.

Once you have your flags implemented, write your own serializer for your classes that handles this flag appropiately.

无论如何,除非您尝试对json或类似内容进行部分更新,否则我不会尝试区分这种"2种null".显式null与隐式null是IMO遇到的问题.

In any case, i would just not try to differentiate this "2 kind of nulls" unless you are trying to do partial updates on a json or things like that. Explicit nulls vs Implicit nulls is something problematic IMO.

这篇关于如何表示“未设定"? Java成员,以便可以使用Jackson将这些序列化未设置为JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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