1つのオプションは、あなたがそれらを必要とする場合は、後から値を抽出することができ、そこからMap[String,JsValue]
で「不明」のフィールドを、キャプチャすることです。
// And a writes...
implicit val writes: Writes[MyClass] = (
(__ \ "a").write[String] and
(__ \ "b").write[String] and
(__ \ "c").write[String] and
__.write[Map[String, JsValue]]
)(unlift(MyClass.unapply _))
// Or combine the two...
implicit val format: Format[MyClass] = (
(__ \ "a").format[String] and
(__ \ "b").format[String] and
(__ \ "c").format[String] and
__.format[Map[String, JsValue]](Reads
.map[JsValue].map(_.filterKeys(k => !Seq("a", "b", "c").contains(k))))
)(MyClass.apply, unlift(MyClass.unapply))
注:
case class MyClass(a: String, b: String, c: String, extra: Map[String, JsValue])
implicit val reads: Reads[MyClass] = (
(__ \ "a").read[String] and
(__ \ "b").read[String] and
(__ \ "c").read[String] and
__.read[Map[String, JsValue]]
.map(_.filterKeys(k => !Seq("a", "b", "c").contains(k)))
)(MyClass.apply _)
// Result:
// MyClass(aa,bb,cc,Map(e -> {"aa":"aa"}, d -> "dd"))
同様に、あなたはそうのようなWrites
またはFormat
を行うことができますが、引数(Reads.map
)としてMap[String,JsValue]
明示Reads
ためformat
を与えるので、それは少し混乱に見えます次に、既にキャプチャ値を削除するために変換します(.map
メソッド)。
と書き込みはどのように見えるのですか? – arseny