gpt4 book ai didi

json4s 无法使用 mixin 特征序列化案例类

转载 作者:行者123 更新时间:2023-12-02 09:36:02 25 4
gpt4 key购买 nike

为什么这不起作用?

object JsonExample extends App {
import org.json4s._
import org.json4s.native.Serialization
import org.json4s.native.Serialization.{read, write}
implicit val formats = Serialization.formats(NoTypeHints)

case class Winner(id: Long, numbers: List[Int])

trait Greet { val greeting = "hi"}
val obj = new Winner(1, List(1,2)) with Greet
println(write(obj))
}

这会打印一个空的 JSON 对象

{}

如果我删除“with Greet”,我会得到(正确的)结果:

{"id":1,"numbers":[1,2]}

最佳答案

看起来如果您对格式更具体,您就可以获得您想要的结果:

import org.json4s.{FieldSerializer, DefaultFormats}
import org.json4s.native.Serialization.write

case class Winner(id: Long, numbers: List[Int])
trait Greet { val greeting = "hi"}

implicit val formats = DefaultFormats + FieldSerializer[Winner with Greet]()

val obj = new Winner(1, List(1,2)) with Greet

//returns {"greeting":"hi","id":1,"numbers":[1,2]}
write(obj)

关于json4s 无法使用 mixin 特征序列化案例类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26179087/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com