Akka:如何创建一个没有空值的紧凑型 json?
Akka: how to create a compact json without empty values?
是否可以使用 Akka(也许是一些 Spray "utils"?)从 case class
开始构建紧凑的 json 提要,如下所示:
case class Stuff (val1: String, val2: String, val3: String)
以这种方式构建:
Stuff("one value", "", "another value")
并以紧凑的形式获得 json 将跳过 "empty value" 并将 return:
{"val1" : "one value", "val3" : "another value"}
?
您可以定义在 json 序列化过程中可能发生的事情,如果您还有其他一些 "stuff" 您可以定义这个隐含的特征以供重用
import org.json4s.jackson.Serialization._
import org.json4s.{FieldSerializer, NoTypeHints}
import org.json4s.jackson.Serialization
trait EmptySpaceIgnoredJsonable {
def toJsonWithEmptyThingies : String = {
implicit val formats = Serialization.formats( NoTypeHints ) +
FieldSerializer[ this.type ]( doStuffWhileSerializing() )
write( this )
}
def doStuffWhileSerializing() : PartialFunction[ (String, Any), Option[ (String, Any) ] ] = {
case (x, y : String) if !y.isEmpty => Some( x , y )
case _ => None
}
}
// then use it, when ever you require empty "stuff"
case class Stuff (val1: String, val2: String, val3: String) extends EmptySpaceIgnoredJsonable
val stuff = Stuff("one value", "", "another value")
println(stuff.toJsonWithEmptyThingies)
我有一个更简单的,但需要你用 Option
构建你的 case class
。
import spray.json._
case class Something(name: String, mid: Option[String], surname: String)
object MyJsonProtocol extends DefaultJsonProtocol {
implicit val sthFormat = jsonFormat3(Something)
}
object Main {
def main(args: Array[String]): Unit = {
val sth = Something("john", None, "johnson").toJson
println(sth) // yields {"name":"john","surname":"johnson"}
}
}
kali 对自定义编写器的回答可能会更好,具体取决于您的需要。
是否可以使用 Akka(也许是一些 Spray "utils"?)从 case class
开始构建紧凑的 json 提要,如下所示:
case class Stuff (val1: String, val2: String, val3: String)
以这种方式构建:
Stuff("one value", "", "another value")
并以紧凑的形式获得 json 将跳过 "empty value" 并将 return:
{"val1" : "one value", "val3" : "another value"}
?
您可以定义在 json 序列化过程中可能发生的事情,如果您还有其他一些 "stuff" 您可以定义这个隐含的特征以供重用
import org.json4s.jackson.Serialization._
import org.json4s.{FieldSerializer, NoTypeHints}
import org.json4s.jackson.Serialization
trait EmptySpaceIgnoredJsonable {
def toJsonWithEmptyThingies : String = {
implicit val formats = Serialization.formats( NoTypeHints ) +
FieldSerializer[ this.type ]( doStuffWhileSerializing() )
write( this )
}
def doStuffWhileSerializing() : PartialFunction[ (String, Any), Option[ (String, Any) ] ] = {
case (x, y : String) if !y.isEmpty => Some( x , y )
case _ => None
}
}
// then use it, when ever you require empty "stuff"
case class Stuff (val1: String, val2: String, val3: String) extends EmptySpaceIgnoredJsonable
val stuff = Stuff("one value", "", "another value")
println(stuff.toJsonWithEmptyThingies)
我有一个更简单的,但需要你用 Option
构建你的 case class
。
import spray.json._
case class Something(name: String, mid: Option[String], surname: String)
object MyJsonProtocol extends DefaultJsonProtocol {
implicit val sthFormat = jsonFormat3(Something)
}
object Main {
def main(args: Array[String]): Unit = {
val sth = Something("john", None, "johnson").toJson
println(sth) // yields {"name":"john","surname":"johnson"}
}
}
kali 对自定义编写器的回答可能会更好,具体取决于您的需要。