score:3
Accepted answer
If you really want Id
to be serialized as a JSON string, here you go:
import play.api.libs.json._
import play.api.libs.functional.syntax._
case class Id(value: String)
implicit val idFormats: Format[Id] =
implicitly[Format[String]].inmap(Id, unlift(Id.unapply))
Json.toJson(Id("asd")) == JsString("asd")
Json.toJson(Id("asd")).toString == "\"asd\""
Json.parse(Json.toJson(Id("asd")).toString).as[Id] == Id("asd")
I wrote it this way to clearly illustrate that you use nothing else that a basic String
formatter, which is defined in play-json.
Source: stackoverflow.com
Related Query
- How to write a symmetric Play Json formatter for a case class with one field in scala?
- How to write a Play JSON writes converter for a case class with a single nullable member
- Play JSON formatter for case class with generic type classes
- How do I define Json format for a case class with more than one apply method?
- How to turn json to case class when case class has only one field
- Play json merge formats for case class with more than 22 fields
- Deriving type class instances for case classes with exactly one field
- How do I write a scala extractor for a case class with default parameters?
- How to send Json from client with missing fields for its corresponding Case Class after using Json.format function
- Play 2.1(scala) - How to write Format[T] for scala enumeration case class
- How to convert a json with a single value to a case class using play json
- How to parse json with arbitrary schema, update/create one field and write it back as json(Scala)
- How to clone a case class instance and change just one field in Scala?
- Scala. Can case class with one field be a value class?
- Json Serialization for Trait with Multiple Case Classes (Sum Types) in Scala's Play
- Lift-json extract json with 'type' field into a case class
- Scala spark: how to use dataset for a case class with the schema has snake_case?
- How to convert case class to JSON in Play framework 2.3.x (Scala)?
- in scala how to convert one case class to another immune to code changes field additions?
- How to fill case class from json with partial data?
- How can I write and read an empty case class with play-json?
- Convert one case class to another with the same structure but with one additional field
- In Scala, how does one write a class with a constructor, not all of whose arguments are class members?
- How to parse json to case class with map by jsonter, plokhotnyuk
- How to write case class for enum column in Apache Spark Dataset?
- Play framework: JSON Reads for a single-attribute case class
- How to read and write Anorm object with the new JSON API in Play Framework 2.1-RC2?
- Scala play framework: complex read to match multiple keys for same field in case class
- Mapping json to case class with Spark (spaces in the field name)
- In Scala, how to write an efficient json formatter for Map[IndexedSeq[String], Int]?
More Query from same tag
- How to create a graph from a list with Spark Graphx
- Best practices of handling HTTP response with scala
- Encoding all subtypes with prepopulated field with Circe
- Why doesn't dataset's foreach method require an encoder, but map does?
- Scala deep type cheking
- type parameter mismatch with WeakTypeTag reflection + quasiquoting (I think!)
- Scala instantiation error with libGdx and Gradle
- Spark groupby filter sorting with top 3 read articles each city
- Map with same key when fetched shows all related to that key
- Using classpathTransformerFactories in sbteclipse plugin to exclude Scala library?
- View bound not compatible with upper type bound?
- Scala HashMap: iterate in insertion order?
- Exception while building Scala-Maven project on IntelliJ
- Mapping cassandra row to parametrized type in Spark RDD
- Spark Scala code not working similarly then its pyspark version
- Window function to convert n number of rows in a column to one single row
- Receiving error when I add the scala nature to project in Eclipse
- How to filter data based on three columns in Scala
- How to test whether all elements in Lists in Map[String,List[Int]] hold negatives?
- How do I automatically compile Coffeescript using Typesafe?
- I have a DataFrame in two rows and multiple columns, how to transpose into two columns and multiple rows?
- Given a Future[T] can I write function with onComplete callback which returns T?
- How to explore a PSITree in intellij API?
- Match may not be exhaustive warning is incorrect
- Scala multiline string placeholder
- How do I make a list of objects that descend from traits in Scala?
- How much is there to LINQ?
- Spark Shell not working after adding support for Iceberg
- Scala/Slick plain SQL: retrieve result as a map
- sbt-android: IntelliJ marks usages of android support resources red