Json4s ignoring None fields during seriallization (instead of using 'null')

眉间皱痕 提交于 2019-11-29 11:39:01
edi

I assume that you would like to have Nones serialized to null within your JSON. In this case it is sufficient to use DefaultFormats.preservingEmptyValues:

import java.util.Date
import org.json4s._
import org.json4s.jackson.Serialization

object test extends App {

  implicit val f = DefaultFormats.preservingEmptyValues // requires version>=3.2.11

  case class JsonTest(x: String, y: Option[String], z: Option[Int], a: Option[Double], b: Option[Boolean], c:Option[Date], d: Option[Any])

  val v = JsonTest("test", None, None, None, None, None, None);

  // prints {"x":"test","y":null,"z":null,"a":null,"b":null,"c":null,"d":null}
  println(Serialization.write(v))
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!