How to turn json to case class when case class has only one field

前端 未结 4 1540
时光取名叫无心
时光取名叫无心 2020-11-28 23:59

In play 2.1 reads are used to marshall Json to objects. But how can I do this when the case class has only one field. The ideom that works for more fields does not work, as

相关标签:
4条回答
  • 2020-11-29 00:18

    Json combinators doesn't work for single field case class.

    Pascal (writer of this API) has explained this situation here https://groups.google.com/forum/?fromgroups=#!starred/play-framework/hGrveOkbJ6U

    There are some workarounds which works, like this one:

    case class A(value: List[Int])
    val areads = (__ \ 'value).read[List[Int]].map{ l => A(l) } // covariant map
    
    0 讨论(0)
  • 2020-11-29 00:19

    Based on @yokomizor's answer, I found the simplest solution to create a Formatter to be

    case class Person(name: String)
    val personFormatter: Format[Person] =
      (__ \ "full_name").format[String].inmap(Person.apply, unlift(Person.unapply))
    
    0 讨论(0)
  • 2020-11-29 00:20

    Even simpler solution than the accepted one:

    case class A(value: String)
    val reads = (__ \ "key").read[String].map(A.apply)
    
    0 讨论(0)
  • 2020-11-29 00:42

    As Julien answered, you can read single field case classes using this:

    case class Person(name: String)
    
    val personReads: Reads[Person] = 
      (__ \ "name").read[String].map { name => Person(name) }
    

    Just a complement, if you want to write:

    val personWrites: Writes[Person] = 
      (__ \ "name").write[String].contramap { (person: Person) => person.name }
    

    Or format (read and write):

    val personFormat: Format[Person] = 
      (__ \ "name").format[String].inmap(name => Person(name), (person: Person) => person.name)
    

    For write and format you have to import this:

    import play.api.libs.functional.syntax._
    
    0 讨论(0)
提交回复
热议问题