如何在Scala中使用Json4s序列化密封的抽象类?

编程入门 行业动态 更新时间:2024-10-16 20:28:15
本文介绍了如何在Scala中使用Json4s序列化密封的抽象类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

如何在Scala中使用Json4s序列化密封的抽象类?

How do i serialize a sealed abstract class with Json4s in Scala?

定义了以下类:

sealed abstract class Person extends Product with Serializable case class Spouse(name: String, age: Int) extends Person case class Customer(name: String, age: Int, spouse: Spouse) extends Person

我创建了一个Customer类型的对象:

I create an object of type Customer:

val customer: Customer = Customer("Joe", 35, Spouse("Marilyn", 33))

然后我序列化为JSON:

Then I serialize to JSON:

implicit val formats = DefaultFormats val serialized = write(customer)

那很好.但随后我尝试反序列化:

That works fine. But then I try to deserialize:

val parsedObj = Serialization.read[Person](serialized)

在这里我总是收到错误消息:

Here I keep getting error:

org.json4s.package $ MappingException:解析的JSON值与类构造函数不匹配

org.json4s.package$MappingException: Parsed JSON values do not match with class constructor

我花了很多时间试图使这项工作...

I spent a lot of time trying to make this work...

推荐答案

经过大量的搜索并仔细阅读了Json4s文档后,我发现我需要一个自定义的Serializer才能使其正常工作.

After a lot of googling around and a close read of the Json4s documentation i found out that I need a custom Serializer to make it work.

但是,我花了一段时间才弄清楚我需要设置格式实际使用自定义序列化器.

However, it took me a while to figure out that I need to set the formats to actually use the custom Serializer.

这是对我有用的代码.

简单示例:

import org.json4s._ import org.json4s.native.JsonMethods._ import org.json4s.native.Serialization.{read, write} import org.json4s.native.Serialization sealed abstract class Person extends Product with Serializable case class Spouse(name: String, age: Int) extends Person case class Customer(name: String, age: Int, spouse: Spouse) extends Person val customer: Customer = Customer("Joe", 35, Spouse("Marilyn", 33)) implicit val formats = Serialization.formats(NoTypeHints) + PersonSerializer val serialized = write(customer) val parsedObj = Serialization.read[Person](serialized)

自定义序列化器:

object PersonSerializer extends Serializer[Person] { private val PersonClass = classOf[Person] def deserialize(implicit format: Formats) : PartialFunction[(TypeInfo, JValue), Person] = { case (TypeInfo(PersonClass, _), json) => json match { case JObject(List( JField("name", JString(d)), JField("age", JInt(f)), ("spouse", JObject(List(JField("name", JString(g)), JField("age", JInt(h))))) )) => Customer(d, f.toInt, Spouse(g, h.toInt)) case JObject(List( JField("name", JString(d)), JField("age", JInt(f)) )) => Spouse(d, f.toInt) } } def serialize(implicit format: Formats): PartialFunction[Any, JValue] = { case x: Customer => JObject(List( JField("name", JString(x.name)), JField("age", JInt(x.age)), ("spouse", JObject(List(JField("name", JString(x.spouse.name)), JField("age", JInt(x.spouse.age))))) )) case x: Spouse => JObject(List( JField("name", JString(x.name)), JField("age", JInt(x.age)) )) } }

输出

scala> val serialized = write(customer)

scala> val serialized = write(customer)

序列化:字符串= {名称":"Joe",年龄":35,配偶":{名称":"Marilyn",年龄":33}}

serialized: String = {"name":"Joe","age":35,"spouse":{"name":"Marilyn","age":33}}

scala> val parsedObj = Serialization.readPerson

scala> val parsedObj = Serialization.readPerson

parsedObj:人员=客户(Joe,35,Spouse(Marilyn,33))

parsedObj: Person = Customer(Joe,35,Spouse(Marilyn,33))

更多推荐

如何在Scala中使用Json4s序列化密封的抽象类?

本文发布于:2023-07-14 14:46:02,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1105101.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:序列化   抽象类   如何在   Json4s   Scala

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!