Advertisement
Guest User

Untitled

a guest
May 2nd, 2016
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.66 KB | None | 0 0
  1. val format = fields.length match {
  2. case 0 => c.abort(c.enclosingPosition, "Cannot create json formatter for case class with no fields")
  3. case 1 =>
  4. // Only one field, use the serializer for the field
  5. q"""
  6. implicit val jsonAnnotationFormat = {
  7. import play.api.libs.json._
  8. Format(
  9. __.read[${fields.head.tpt}].map(s => ${className.toTermName}(s)),
  10. new Writes[$className] { def writes(o: $className) = Json.toJson(o.${fields.head.name}) }
  11. )
  12. }
  13. """
  14. case _ =>
  15. // More than one field, use Play's Json.format[T] macro
  16. q"implicit val jsonAnnotationFormat = play.api.libs.json.Json.format[$className]"
  17. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement