2017-03-29 17 views
0

我有以下對象:播放JSON序列化/反序列化案例類的一個參數就跳過它

case class Foo(value: UUID) 
case class Bar(foo: Foo) 

我想有他們在JSON序列化這樣的:

{foo: "e88ad5ea-13cf-4666-addb-c231c12cd3a3"} 

意義我想省略自動格式化器添加的「值」參數,如果我只是寫這個:

implicit val fooFormat = Json.format[Foo] 
implicit val barFormat = Json.format[Bar] 

我t寫了我的自定義作家/讀者,但我真的不知道如何跳過JPath。

回答

0

什麼:

implicit val format: Format[Bar] = new Format[Bar] { 
    override def writes(o: Bar): JsValue = Json.obj("foo" -> o.foo.value) 

override def reads(json: JsValue): JsResult[Bar] = 
    (json \ "foo").validate[String].map(s => Bar(Foo(UUID.fromString(s)))) 
} 
+0

是的,我想這會工作。但在我的具體情況中,Bar實際上有很多其他參數,我寧願不必寫所有的寫入/讀取。 – CanardMoussant