[英]How to prevent json4s from rendering null values?
How do I prevent json4s rendering null values when converting an object/JObject into a json string? 将对象/ JObject转换为json字符串时,如何防止json4s呈现空值?
In Jackson you can do this by doing this: 在Jackson中,您可以这样做:
mapper.setSerializationInclusion(Include.NON_NULL)
How can I do the same thing in json4s? 如何在json4s中做同样的事情?
Example 例
import org.json4s.jackson.JsonMethods._
import org.json4s.jackson.Serialization
import org.json4s.{Extraction, NoTypeHints}
case class Book(title: String, author: String)
implicit val formats = Serialization.formats(NoTypeHints)
val bookJValue = Extraction.decompose(Book(null, "Arthur C. Clark"))
# JObject(List((title,JNull), (author,JString(Arthur C. Clark))))
val compacted = compact(render(bookJValue))
# {"title":null,"author":"Arthur C. Clark"}
I'd like the compacted json be this: 我想要压缩的json是这样的:
{"author":"Arthur C. Clark"}
case class Book(title: Option[String], author: String)
val b = Book(None, "Arthur C. Clark")
println(write(b))
res1:> {"author":"Arthur C. Clark"}
You can use Option
to define variable. 您可以使用
Option
定义变量。 if it is none, it will not serialize this variable. 如果为none,则不会序列化此变量。
There is another way to do this by using removeField
after you decompose
your object
, like: decompose
object
,还有另一种方法可以通过使用removeField
来实现,例如:
bookJValue.removeFile {
case (_, JNull) => true
case _ => false
}
You can easily create your own custom serializer. 您可以轻松创建自己的自定义序列化程序。 Follow me.
跟着我。
First of all make small change in formats
: 首先,在
formats
进行一些小的更改:
implicit val formats = DefaultFormats + new BookSerializer
After that build your own serializer/deserializer: 之后,构建自己的序列化器/解串器:
class BookSerializer extends CustomSerializer[Book](format => (
{
case JObject(JField("title", JString(t)) :: JField("author", JString(a)) ::Nil) =>
new Book(t, a)
},
{
case x @ Book(t: String, a: String) =>
JObject(JField("title", JString(t)) ::
JField("author", JString(a)) :: Nil)
case Book(null, a: String) =>
JObject(JField("author", JString(a)) :: Nil) // `title` == null
}
))
The first part is deserializer (convert data from json to case class) and the second is serializer (conversion from case class to json). 第一部分是解串器(将数据从json转换为case类),第二部分是序列化器(从case类别转换为json)。 I've added case of
title == null
. 我添加了
title == null
。 You can easily add cases as many as you need. 您可以根据需要轻松添加案例。
The whole listing: 整个清单:
import org.json4s.jackson.JsonMethods._
import org.json4s.{DefaultFormats, Extraction}
import org.json4s._
case class Book(title: String, author: String)
implicit val formats = DefaultFormats + new BookSerializer
class BookSerializer extends CustomSerializer[Book](format => (
{
case JObject(JField("title", JString(t)) :: JField("author", JString(a)) ::Nil) =>
new Book(t, a)
},
{
case x @ Book(t: String, a: String) =>
JObject(JField("title", JString(t)) ::
JField("author", JString(a)) :: Nil)
case Book(null, a: String) =>
JObject(JField("author", JString(a)) :: Nil) // `title` == null
}
))
val bookJValue = Extraction.decompose(Book(null, "Arthur C. Clark"))
val compacted = compact(render(bookJValue))
Output: 输出:
compacted: String = {"author":"Arthur C. Clark"}
You can find additional information on the page of json4s project . 您可以在json4s项目的页面上找到更多信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.