[英]How can i convert None field from case class to JSON correctly
I have this case class我有这个案例课
case class Example(
exId: String,
exDes: Option[String] = None)
and I try to convert我尝试转换
Example(exId = 1)
to JSON (example.asJson from io.circe) I actually got到 JSON(来自 io.circe 的 example.asJson)我实际上得到了
{
"exId" : "1"
"exDes": null
}
But i expected但我预计
{
"exId" : "1"
}
is there anyway to convert like i expected with io.circe ?有没有像我预期的那样使用 io.circe 进行转换?
Just use dropNullValues
, as it Scala Docs states:只需使用
dropNullValues
,因为它 Scala Docs 状态:
Drop the entries with a null value if this is an object.
如果这是一个对象,则删除具有空值的条目。
Your example looks then:你的例子看起来像:
Example(exId = "1").asJson.dropNullValues
And the result is as you wanted:结果如您所愿:
{
"exId" : "1"
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.