[play2.1][scala][JSON] : writing a Reads/Writes for a single field case class

瀏覽次數:2,515 次
跳到第一則未讀訊息

Pascal Voitot Dev

未讀,
2013年1月11日 上午8:45:422013/1/11
收件者:play-fr...@googlegroups.com
Hello again,

A few people have encountered the case of Json macro failing for single field case class.
This is not a bug but a limitation of current Macro implementation.

But there is a workaround to write it relatively simply using Reads.map/Writes.contramap/Format.invmap function which can be really useful if you don't know them.

Everything is explained in this ticket:
https://play.lighthouseapp.com/projects/82401-play-20/tickets/918-play-21-json-issues#ticket-918-4

regards
Pascal

Rogério Yokomizo

未讀,
2014年6月25日 晚上8:05:552014/6/25
收件者:play-fr...@googlegroups.com
Hello

The original ticket was deprecated. Is there a new link to it?

regards

Rogério

Pascal Voitot Dev

未讀,
2014年6月26日 凌晨2:30:512014/6/26
收件者:play-fr...@googlegroups.com
I can't find it!
If you search in google group for "reads case class single field" you should find a few hints.

but this is very simple
with :
case class Foo(foo: String)

you must use :
(__ \ "foo").read[String].map(Foo(_))

(__ \ "foo").write[String].contramap((f: Foo) => f.foo)

For Format, it's invmap mixing both...

regards
Pascal


--
You received this message because you are subscribed to the Google Groups "play-framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to play-framewor...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Henry Katz

未讀,
2019年1月14日 下午1:40:342019/1/14
收件者:Play Framework [deprecated]
Expanding on this, if the single field case class were a higher kind such as

case class Foo(foo: List[String])

everything below still works just fine:

(__ \ "foo").read[List[String]].map(Foo(_))

but say you were trying to validate that field with a uniqueitems validation function such as:

  def uniqueItems[String](isUnique: Boolean)(implicit tag: TypeTag[String], r: Reads[List[String]]): Reads[List[String]] = {
    Reads.of[List[String]].filter(ValidationError(uniqueItemsJsonValidationError))({ items =>
      if (isUnique) items.size == items.distinct.size
      else true
    })
  }

which would change the read to

(__ \ "foo").read[List[String]](uniqueItems(true)).map(Foo(_))

giving a compile error as there is no implicit for a List[String] though we know play can deserialize a String:

No Json deserializer found for type List[String]. Try to implement an implicit Reads or Format for this type.
[error]   implicit val dinosaurReader: Reads[Dinosaur] = ((JsPath \ "friends").read[List[String]](uniqueItems(true))).map(Dinosaur)
[error]                                                                                                                                               ^

Tim Moore

未讀,
2019年1月14日 下午4:55:502019/1/14
收件者:play-fr...@googlegroups.com
The problem could be that you've defined a type variable named "String", which would shadow the built in String type and is probably not what you are intending:

def uniqueItems[String](isUnique: Boolean)(implicit tag: TypeTag[String], r: Reads[List[String]])

It's probably this "String" that the error refers to.

Best,
Tim

--
Please join our new forum at https://discuss.playframework.com!
The play-framework Google Group will soon be put into read-only mode.
For details, see https://blog.playframework.com/introducing-discuss-playframework-com/.
---
You received this message because you are subscribed to the Google Groups "Play Framework [deprecated]" group.

To unsubscribe from this group and stop receiving emails from it, send an email to play-framewor...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.


--
Tim Moore
Sr. Manager Engineering and Product Management, Lightbend, Inc.
回覆所有人
回覆作者
轉寄
0 則新訊息