如果缺少可选值,则显式输出JSON null [英] Explicitly output JSON null in case of missing optional value

查看:88
本文介绍了如果缺少可选值,则显式输出JSON null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Play的JSON API(play.api.libs.json)考虑以下示例:

Consider this example using Play's JSON API (play.api.libs.json):

case class FooJson(
   // lots of other fields omitted
   location: Option[LocationJson]
)

object FooJson {
  implicit val writes = Json.writes[FooJson]
}

case class LocationJson(latitude: Double, longitude: Double)

object LocationJson {
  implicit val writes = Json.writes[LocationJson]
}

如果locationNone,则生成的JSON将完全没有location字段.这是很好的并且是不可改变的.但是,如果出于某种原因(例如,使我的API更加自我记录),我如何在JSON中显式输出null ?

If location is None, the resulting JSON won't have location field at all. This is fine and understadable. But if I wanted for some reason (say, to make my API more self-documenting), how can I explicitly output null in JSON?

{      
  "location": null
}

我还尝试将字段定义为location: LocationJson并将其传递给option.orNull,但是它不起作用(在play.api.libs.json.OWrites$$anon$2.writes处为scala.MatchError: null).对于非自定义类型(例如String或Double),此方法在JSON输出中生成null.

I also tried defining the field as location: LocationJson and passing option.orNull to it, but it does not work (scala.MatchError: null at play.api.libs.json.OWrites$$anon$2.writes). For non-custom types such as String or Double, this approach would produce null in JSON output.

因此,在使用如上所示的Json.writes[FooJson] 时(或同样简单的方法,即不是不必编写自定义的Writes实现),是否存在问题? JSON中包含空值的方法?

So, while using Json.writes[FooJson] as shown above (or something equally simple, i.e. not having to write a custom Writes implementation), is there a clean way to include nulls in JSON?

我要问的是类似于Jackson库中的JsonInclude.Include.ALWAYS(也是Jackson的默认行为).同样,在Gson中
(new GsonBuilder().serializeNulls().create()).

What I'm asking is analogous to JsonInclude.Include.ALWAYS in the Jackson library (also Jackson's default behaviour). Similarly in Gson this would be trivial
(new GsonBuilder().serializeNulls().create()).

播放2.4.4

推荐答案

Greg Methvin,Play提交者,在相关的GitHub问题中向我写了这个答案:

Greg Methvin, a Play committer, wrote this answer to me in a related GitHub issue:

JSON宏仅支持一种编码可选值的方式, 这将忽略JSON中的None值.这不是错误,但 而是实施的限制.如果你想包括 null,很遗憾,您将不得不实现自己的Writes.

The JSON macros only support one way of encoding optional values, which is to omit None values from the JSON. This is not a bug but rather a limitation of the implementation. If you want to include nulls you're unfortunately going to have to implement your own Writes.

我确实认为我们应该尝试为 宏.

I do think we should try to provide more configurability for the macros though.

在这种情况下,即使值略微牺牲了API的一致性和可自我记录性,我也会让Play在值为null时排除该字段. (在这个特定的API中)仍然是一件很小的事情,以至于它不保证代码会像自定义Writes那样具有十几个值的case类那样笨拙.

In this case, I'll let Play exclude this field when the value is null, even if it slightly sacrifices API consistency and self-documentability. It's still such a minor thing (in this particular API) that it doesn't warrant uglifying the code as much as a custom Writes would take for a case class with a dozen values.

我希望他们能在以后的Play版本中对此进行更多配置.

I'm hoping they do make this more configurable in future Play versions.

这篇关于如果缺少可选值,则显式输出JSON null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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