play-json

Scala - convert String to Json using Play json

爷,独闯天下 提交于 2019-12-08 03:59:53
问题 I need help to parse a json string into scala class using playJson I wrote a formatter but I don't know how to handle the nested arrays. Where Document Case class is case class Document(content: String, score: Double, size: Int, path:String) and formatter implicit val similarHashFormatter: Format[SimilarHash] = ( ((__ \ "hits" \ "hits" \\ "fields")(0) \ "content_hash")(0).format[String] and (__ \ "hits" \ "hits" \\ "_score").format[Double] and ((__ \ "hits" \ "hits" \\ "fields")(0) \ "ast

No Json formatter for Option[String]?

孤街醉人 提交于 2019-11-30 09:57:38
I am trying to marshall and un-marshall an Option[String] field to and from JSON. For my use-case, a None value should be marshaled as "null". Here is the code I have: import org.scalatest.{FlatSpec, Matchers} import play.api.libs.json._ import play.api.libs.json.Reads._ import play.api.libs.functional.syntax._ case class Person( id: Int, firstName: Option[String], lastName: Option[String] ) object Person { implicit lazy val personFormat = ( (__ \ "id").format[Int] and (__ \ "first_name").format[Option[String]] and (__ \ "last_name").format[Option[String]] )(Person.apply, unlift(Person.unapply