Scala play! 2 create Json formatter for class in external library

I am using https://github.com/DanielaSfregola/twitter4s and I need to read and parse the custom class provided by twitter4s in Json.

So, I tried to create the following serializer:

object User {
    implicit val twitterUser: OFormat[User] = Json.format[User]
}

      

but this return No unapply or unapplySeq function found

How do I create the correct serializer for this outer class?

+3


source to share


1 answer


Your problem is that it User

has a lot of fields (namely more than 22). In scala 2.10 there was a limit on the number of fields for the case class. This disappeared in 2.11, but not in the Json.format

Play! Macro as they continued to support 2.10.

However, you can use Json Extra , which has a different implementation for macros and allows you to go beyond 22 fields.



If you don't like this (the macro has some limitations, for example, it doesn't output OFormat

), you can also split the class User

into two (or more) classes with less than 22 fields and create the original one, with a simpler (and more secure) implementation for Format

what you would need to do if you did it all manually.

You can also read this question for further explanations.

+2


source







All Articles