gpt4 book ai didi

json - Play Framework : How to serialize/deserialize an enumeration to/from JSON

转载 作者:IT老高 更新时间:2023-10-28 12:52:50 27 4
gpt4 key购买 nike

鉴于以下枚举...

object MyEnum extends Enumeration {

type MyEnum = Value

val Val1 = Value("val1")
val Val2 = Value("val2")
val ValN = Value("valN")

implicit val myEnumFormat = new Format[MyEnum] {
def reads(json: JsValue) = MyEnum.withName(json.as[String].value) // doesn't compile
def writes(myEnum: MyEnum) = JsString(myEnum.toString)
}
}

...我需要将其序列化/反序列化到 JSON 或从 JSON 序列化/反序列化。 myEnumFormat 无法编译,我总是收到以下错误消息:

type mismatch;
[error] found : models.MyEnum.Value
[error] required: play.api.libs.json.JsResult[models.MyEnumValue]
[error] Note: implicit value myEnumFormat is not applicable here because it comes after the application point and it lacks an explicit result type
[error] def reads(json: JsValue) = MyEnum.withName(json.as[JsString].value)

我错过了什么吗?

最佳答案

implicit val genderReads = Reads.enumNameReads(Gender) 对我来说工作正常。玩 Scala 2.4.2

关于json - Play Framework : How to serialize/deserialize an enumeration to/from JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23816753/

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