score:27
writes
isn't a covariant functor, so you can't use map
, but you can use contramap
:
import play.api.libs.json._
import play.api.libs.functional.syntax._
implicit val someclasswrites: writes[someclass] =
(__ \ 'foo).writenullable[double].contramap(_.foo)
if you have more than one member, you can use play's functionalbuilder
syntax:
case class anotherclass(foo: option[double], bar: option[string])
implicit val anotherclasswrites: writes[anotherclass] = (
(__ \ 'foo).writenullable[double] and
(__ \ 'bar).writenullable[string]
)(ac => (ac.foo, ac.bar))
in the first case the argument to contramap
is just a function from the type you want a writes
for to the type in the writes
you're calling contramap
on. in the second case, the function at the end is from the target (anotherclass
here) to a tuple of the writes
instances you've built up with and
(in this case option[double]
and option[string]
).
score:2
the easy way is:
import play.api.libs.json.json
implicit val fmt = json.format[someclass]
which uses a macro to auto-generate the json format for you. beats implementing writes directly.
Source: stackoverflow.com
Related Query
- How to write a Play JSON writes converter for a case class with a single nullable member
- How to write a symmetric Play Json formatter for a case class with one field in scala?
- How to convert a json with a single value to a case class using play json
- Play json merge formats for case class with more than 22 fields
- 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
- Play how to implement an implicit Writes or Format for a case class including an Enumeration
- How to write implicit Writes for case class having more than 22 fields
- Play JSON formatter for case class with generic type classes
- How to swap JSON Writes Converter for Play controller Action
- Akka-HTTP how map JSON entity with two case class in single REST call
- Transform JSON array and assign to single value case class with Play JSON
- How do I define Json format for a case class with more than one apply method?
- Play Json case class mapper with custom converter function
- Json Serialization for Trait with Multiple Case Classes (Sum Types) in Scala's Play
- Custom Json Writes with combinators - not all the fields of the case class are needed
- 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)?
- How to fill case class from json with partial data?
- How can I write and read an empty case class with play-json?
- 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?
- How to write test case for NoHostAvailableException in lagom with Scala?
- How to read json file and convert to case class with Spark and Spray Json
- Argonaut.io: how to rename json property for Right/Left in case class containing Either
- How to "reads" into a Scala Case Class given a Json object with key names that start with a capital letter
- How to create Dataset with case class Type Parameter ? (Unable to find encoder for type T)
More Query from same tag
- zipWithIndex not working as expected in Play! Framework template
- How to declare generic type function in scala?
- Not able to write to CSV with header using Spark Scala
- Can I create a Vector with max size more then 999 elements? (get elem by index more than 999)
- sbt multi-module project global version setting
- Apply a list of attributes to xml element dynamically
- Approx quantile on a array of doubles - Spark dataframe
- Generic REST client in Scala
- Load Packages into Renjin in Scala or Java
- Spark RDD: multiple reducebykey or just once
- Using match .. case in scala
- Better platform to turn software into VHDL/Verilog for an FPGA
- Scala create a function to run on existing type
- Using SBT from Scala IDE
- Can HTTPS listening be enabled in Play Framework during tests?
- why sparkcontext stopped?
- How to access scala annotation in scala code
- cats' NonEmptyList vs scala stdlib ::
- Overcoming type erasure in Scala when pattern matching on Objects which may be different Sets or any type of Object
- scala user define function not working in sparksql
- Running Fat Jar with Spark 2.0 on cluster with only Spark 1.6 support
- Convert JValue to JSON string
- Error while parsing Date in Spark scala program
- Scala Spark Dataframe - Sum for each row the content of Array[Double]
- akka-http how to accept invalid URI [will not solve]
- How to use a redirect when using Scala Process Methods
- How does Array.toList work in Scala?
- Scala Reflection of Nested List
- How to decode an ADT with circe without disambiguating objects
- how to do chain functions in Spark dataframe?