So I have two classes in my project
case class Item(id: Int, name: String)
and
case class Order(id: Int, items: List[Item])
I'm trying to make reads and writes properties for Order but I get a compiler error saying:
"No unapply or unapplySeq function found"
In my controller I have the following:
implicit val itemReads = Json.reads[Item]
implicit val itemWrites = Json.writes[Item]
implicit val listItemReads = Json.reads[List[Item]]
implicit val listItemWrites = Json.writes[List[Item]]
The code works for itemReads
and itemWrites
but not for the bottom two. Can anyone tell me where I'm going wrong, I'm new to Play framework.
Thank you for your time.
This works:
This also allows you to customize the naming for your JSON object. Below is an example of the serialization in action.
I'd also recommend using
format
instead ofread
andwrite
if you are doing symmetrical serialization / deserialization.The "
No unapply or unapplySeq function found
" error is caused by these two:Just throw them away. As Ende said, Play knows how to deal with lists.
But you need
Reads
andWrites
forOrder
too! And since you do both reading and writing, it's simplest to define aFormat
, a mix of theReads
andWrites
traits. This should work:Above, the ordering is significant;
Item
and the companion object must come beforeOrder
.So, once you have all the implicit converters needed, the key is to make them properly visible in the controllers. The above is one solution, but there are other ways, as I learned after trying to do something similar.
You don't actually need to define those two implicits, play already knows how to deal with a list:
The error you see probably happens because play uses the unapply method to construct the macro expansion for your read/write and
List
is an abstract class, play-json needs concrete type to make the macro work.