json - 使用 Playframework json 格式化密封类型参数化特征
问题描述
我有类型参数化的密封特征
sealed trait PropertyValue[T <: Property]
format
我需要为 Json 序列化/反序列化编写。我试试
implicit def format[P <: Property : Format]: Format[PropertyValue[P]] = {
Format(
Reads(j =>
Json.fromJson[StringPropertyValue](j)
.orElse(Json.fromJson[NumberPropertyValue](j))
.orElse(JsError("Undefined type"))
),
Writes(o => Json.toJson(o))
)
}
但是我有
type mismatch;
[error] found : play.api.libs.json.JsResult[Product with Serializable with PropertyValue[_ >: StringProperty with NumberProperty <: Product with Serializable with Property]]
[error] required: play.api.libs.json.JsResult[PropertyValue[P]]
有没有办法实现Reads[trait[T]]
类型参数化特征?
解决方案
推荐阅读
- sql - HAVING 给了我“列...不存在”,但我看到了该列
- apache-spark - Spark 的 DAG 调度程序与 Catalyst
- docker - 连接到远程 ArangoDB dockerized 服务器
- javascript - 在反应中使用开放时间的对象映射数组
- r - 内部 R6 类定义:“找不到对象”(或:如何在 R6 类中定义“本地”对象)
- reactjs - 以 React 形式计算
- postgresql - 是否可以在 JSONB 密钥值上使用 pgp_sym_encrypt ?
- r - 如何在变量、类中转换负值
, 转换成数字/整数而不接收 NA? - flutter - Flutter - 如何获取快照的索引值?
- php - mariadb 读取通信包时出错