【发布时间】:2014-12-29 10:54:26
【问题描述】:
我有 2 位 scala 代码使用 json4s dsl 来生成 json 内容
val feastWithMenus = "Feast" ->
("target" -> "me") ~
("menus" -> List(
("first" -> "pate") ~
("mains" -> "beef") ~
("dessert" -> "ice-cream")
,
("first" -> "soup") ~
("mains" -> "omlette") ~
("dessert" -> "ice-cream")
))
val feastNoMenu = "Feast" ->
("target" -> "me") ~
("menus" -> List.empty)
首先,我得到了我的期望:
{"Feast":{"target":"me","menus":[{"first":"pate","mains":"beef","dessert":"ice-cream"}, {"first":"soup","mains":"omlette","dessert":"ice-cream"}]}}
JObject(List((Feast,JObject(List((target,JString(me)), (menus,JArray(List(JObject(List((first,JString(pate)), (mains,JString(beef)), (dessert,JString(ice-cream)))), JObject(List((first,JString(soup)), (mains,JString(omlette)), (dessert,JString(ice-cream))))))))))))
但是对于空列表,我没有得到数组。相反,我得到了
{"Feast":{"target":"me","menus":{}}}
JObject(List((Feast,JObject(List((target,JString(me)), (menus,JObject(List())))))))
注意第一种情况下的菜单是JArray(List(Jobject,第二种情况下是JObject(List()
这是设计使然吗?这目前用于测试,但我需要了解发生了什么,因为我的真实世界代码可能在许多地方都有空列表,并且接收者期待一个数组。
【问题讨论】: