我们的状态定义为:
sealed trait Status
case object Status {
case object StatusA extends Status
case object StatusB extends Status
case object StatusC extends Status
}
我们的状态如下:
val status = Status.StatusA
有没有办法(de)使用预定义的json4s
fomratters序列化这种结构?
我们尝试使用defult格式化程序:
implicit val formats = new org.json4s.DefaultFormats
它不起作用。接下来,我们尝试使用ext
提供的json4s
和Enum支持:
implicit val formats = org.json4s.DefaultFormats + new org.json4s.ext.EnumSerializer(Status)
它再次无效。我们必须完全将sealed trait
的结构更改为实际的Enumeration
。有没有办法用case class
es?
答案 0 :(得分:0)
这是一个完整的工作示例,我更改了一些类以使示例更简单,这样您就可以使用" name"以不同的方式,您实际上可以省略" def name",但在这种情况下,您需要稍微更改序列化程序。第二个Serializer就在下面。
sealed trait Status {
def name: String
}
case object Status {
def apply(name: String): Status = name match {
case StatusA.name => StatusA
case StatusB.name => StatusB
case StatusC.name => StatusC
case _ => throw new UnsupportedOperationException("Unknown value")
}
}
case object StatusA extends Status {
override val name = "StatusA"
}
case object StatusB extends Status {
override val name = "StatusB"
}
case object StatusC extends Status {
override val name = "StatusC"
}
class StatusSerializer extends CustomSerializer[Status](formats =>
( {
case JString(s) => Status(s)
case JNull => throw new UnsupportedOperationException("No status specified")
}, {
case status: Status => JString(status.name)
})
)
case class SimpleRichObject(someString: String, someInt: Int, statuses: List[Status])
object Test extends App {
implicit val formats = DefaultFormats + new StatusSerializer
val obj = SimpleRichObject("Answer to life the universe and everything", 42, List(StatusA, StatusB, StatusC))
def toCompactJsonString(any: Any) = {
JsonMethods.compact(JsonMethods.render(Extraction.decompose(any)))
}
def toPrettyJsonString(any: Any) = {
JsonMethods.pretty(JsonMethods.render(Extraction.decompose(any)))
}
/** To Json */
println(s"Compact json:\n${toCompactJsonString(obj)}")
println(s"Pretty json:\n${toPrettyJsonString(obj)}")
/** From Json */
val json =
"""{
| "someString":"Here is a another String",
| "someInt":1234,
| "statuses":["StatusA","StatusB"]
|}""".stripMargin
val richObj = JsonMethods.parse(json).extract[SimpleRichObject]
println(s"Rich object toString: $richObj")
}
这是第二个Serializer,使用第二个,你不需要在你的" Enums"
中定义额外的代码
class SecondStatusSerializer extends CustomSerializer[Status](formats =>
( {
case JString(s) => s match {
case "StatusA" => StatusA
case "StatusB" => StatusB
case "StatusC" => StatusC
}
case JNull => throw new UnsupportedOperationException("No status specified")
}, {
case status: Status => status match {
case StatusA => JString("StatusA")
case StatusB => JString("StatusB")
case StatusC => JString("StatusC")
}
})
)
这就是这个人跑步时的样子,紧凑的json:
{"someString":"Answer to life the universe and everything","someInt":42,"statuses":["StatusA","StatusB","StatusC"]}
漂亮的json:
{
"someString":"Answer to life the universe and everything",
"someInt":42,
"statuses":["StatusA","StatusB","StatusC"]
}
Rich object toString:SimpleRichObject(这是另一个String,1234,List(StatusA,StatusB))