1
我有枚舉JSON轉換的很多隱性val的在我的程序像:斯卡拉通用隱VAL
implicit val format = new Format[AuthRoleIndividual] {
def reads(json: JsValue) = JsSuccess(AuthRoleIndividual.withName(json.as[String]))
def writes(myEnum: AuthRoleIndividual) = JsString(myEnum.toString)
}
注:AuthRoleIndividual
延伸Enumeration
。我的方法是寫這樣的東西:
implicit val format[T <: Enumeration] = new Format[T] {
def reads(json: JsValue) = JsSuccess(T.withName(json.as[String]))
def writes(myEnum: T) = JsString(myEnum.toString)
}
但這是不可能的。有任何想法嗎?
順便說一句,Play JSON已經提供['Writes'](https://www.playframework.com/documentation/2.6.0/api/scala/index.html#play.api.libs.json.Writes$ @enumNameWrites [E%3C:Enumeration]:play.api.libs.json.Writes [E#Value])和['Reads'](https://www.playframework.com/documentation/2.6.0/api/ scala/index.html#[email protected] [E%3C:Enumeration](enum:E):play.api.libs.json.Reads [E#Value])枚舉 – cchantep