Creating Writes[T] for Custom Object - json

Play for Scala shows how to convert JSON to a Scala object.
case class Product(ean: Long, name: String, description: String)
import play.api.libs.json._
import play.api.libs.functional.syntax._
implicit val productWrites: Writes[Product] = (
(JsPath \ "ean").write[Long] and
(JsPath \ "name").write[String] and
(JsPath \ "description").write[String]
)(unlift(Product.unapply))
And then using in REPL:
scala> val p = Product(100, "tilley hat", "Nice hat")
p: Product = Product(100,tilley hat,Nice hat)
scala> Json.toJson(p)
res1: play.api.libs.json.JsValue = {"ean":100,"name":"tilley hat",
"description":"Nice hat"}
What's going on with the last line: (unlift(Product.unapply)) of the Writes[Product]?

Product.unapply _ is a function Product => Option[(Long, String, String)].
Result type of this expression:
(
(JsPath \ "ean").write[Long] and
(JsPath \ "name").write[String] and
(JsPath \ "description").write[String]
)
is FunctionalBuilder[OWrites]#CanBuild3[Long,String,String]. It accepts T => (Long, String, String) as parameter of method apply.
So you have to convert Product => Option[(Long, String, String)] to Product => (Long, String, String).
Method unlift accepts T => Option[R] and returns T => R. Unlifted function throws MatchError instead of None. It produces something like this:
val unlifted = (Product.unapply _) andThen { case Some(r) => r }
Default unapply method for case class should never return None, so for case class unlift is safe.

Related

JSON / BigInt Json deserializer in play / scala

I am trying to read some JSON String from request and convert them in case class in Play/Scala based REST application.
My code is something like ...
implicit val memberRead: Reads[MemberInfo] = (
(JsPath \ "memberId").readNullable[BigInt] and
(JsPath \ "firstName").read[String] and
(JsPath \ "lastName").read[String] and
(JsPath \ "janrainUUID").readNullable[String] and
(JsPath \ "phones").read[Seq[MemberPhone]] and
(JsPath \ "address").read[Seq[MemberAddress]]
)(MemberInfo.apply _)
implicit val addressRead: Reads[MemberAddress] = (
(JsPath \ "addressId").readNullable[BigInt] and
(JsPath \ "addressType").read[String] and
(JsPath \ "address").read[String] and
(JsPath \ "memberId").read[BigInt]
)(MemberAddress.apply _)
implicit val phoneRead: Reads[MemberPhone] = (
(JsPath \ "phoneId").readNullable[BigInt] and
(JsPath \ "phoneNumber").read[String] and
(JsPath \ "phoneType").read[String] and
(JsPath \ "primaryInd").read[String] and
(JsPath \ "memberId").read[BigInt]
)(MemberPhone.apply _)
But I am getting some compilation error(For all three readNullable[BigInt], memberid in memberRead, addressId in addressRead and phoneId in phoneRead ). Error is ...
No Json deserializer found for type BigInt. Try to implement an implicit Reads or Format for this type.
My Case class are some like this ...
case class MemberInfo(memberId : Option[BigInt],firstName : String, lastName : String,janrainUUID :Option[String] , phones : Seq[MemberPhone],address : Seq[MemberAddress])
case class MemberAddress(addressId:Option[BigInt],addressType:String,address:String,memberId:BigInt)
case class MemberPhone(phoneId : Option[BigInt], phoneNumber:String,phoneType:String,primaryInd:String,memberId:BigInt)
for janrainUUID :Option[String] I am not getting any compilation error , but for BigInt I am getting "No Json deserializer found for type BigInt"
Any one can explain why I am getting this error for BigInt and How can I resolve those? Actually those are PK value when I will do the DB operation for those, so they never may come with request. Is there any way to express that in play/scala like #ignore annotation in jersey.
Any help will be appreciated , thanks a lot...
You need to define serializers for BigInt in the following way:
implicit val BigIntWrite: Writes[BigInt] = new Writes[BigInt] {
override def writes(bigInt: BigInt): JsValue = JsString(bigInt.toString())
}
implicit val BigIntRead: Reads[BigInt] = Reads {
case JsString(value) => JsSuccess(scala.math.BigInt(value))
case JsNumber(value) => JsSuccess(value.toBigInt())
case unknown => JsError(s"Invalid BigInt")
}
Just add this before memberRead serializer and you are good to go and also add error handling for invalid BigInt.
play-json doesn't provide a Reads[BigInt]. It only provides a Reads[BigDecimal].
You can either write your own Reads[BigInt]:
implicit val bigIntReads: Reads[BigInt] = implicitly[Reads[BigDecimal]].map(_.toBigInt())
or use play's Reads[BigDecimal] and transform the result:
implicit val memberRead: Reads[MemberInfo] = (
(JsPath \ "memberId").readNullable[BigDecimal].map(_.toBigInt()) and
...
Edit: Both above solutions have the advantage of not re-inventing the wheel, they build on some well-tested infrastructure provided by play-json. As such they provide benefits that other solutions proposed for this question do not, mainly the correct handling of json string as well as numbers.
You can implement Format like this and use it in your companion object as implicit val:
import play.api.libs.json._
import play.api.libs.functional.syntax._
import scala.util.Try
object BigIntFormat extends Format[BigInt] {
override def reads(json: JsValue): JsResult[BigInt] = json match {
case JsNumber(n) => Try(JsSuccess(n.toBigInt)).getOrElse {
JsError(JsPath() -> JsonValidationError(s"error.expected.numeric(as BigInt), but got '$json'"))
}
case JsString(s) => Try(JsSuccess(BigInt(s))).getOrElse {
JsError(JsPath() -> JsonValidationError(s"error.expected.string(as BigInt), but got '$json'"))
}
case _ => JsError(JsPath() -> JsonValidationError("error.expected.string"))
}
override def writes(o: BigInt): JsValue = JsString(o.toString)
}
case class SomethingWithBigInt(id: BigInt, str: String)
object SomethingWithBigInt {
implicit val bigIntFormatter = BigIntFormat
implicit lazy val format: Format[SomethingWithBigInt] = ({
(JsPath \ "id").format[BigInt] and
(JsPath \ "str").format[String]
})(SomethingWithBigInt.apply, unlift(SomethingWithBigInt.unapply))
}

Playframework 2.4.x Json write List[(String, String)]

I am trying to update some code. I am having problem with this case class trying to write the json implicit writer
case class TemplateEmailMessage(recipients: List[EmailRecipient], globalEmailVars: List[(String, String)])
It was like this
implicit val templateEmailMessageWrites = new Writes[TemplateEmailMessage] {
def writes(m: TemplateEmailMessage): JsValue = {
val globalVars: List[JsValue] = m.globalEmailVars.map(g => Json.obj("name" -> g._1, "content" ->g._2))
Json.obj(
"to" -> m.recipients,
"global_merge_vars" -> JsArray(globalVars)
)
}
}
Now is like this. Obviously is not working because the type of the second field List[(String, String)]
object TemplateEmailMessage {
implicit val templateEmailMessageWrites: Writes[TemplateEmailMessage] = (
(JsPath \ "to").write[List[EmailRecipient]] and
(JsPath \ "global_merge_vars").write[List[(String, String)]]
)(unlift(TemplateEmailMessage.unapply))
}
I am not sure how to translate the JsArray, because before it was use to manipulate the values in the writer. I should leave like the old way or there is another way?
Thank you
You can do it like this:
object TemplateEmailMessage{
implicit val templateEmailMessageWrites: Writes[TemplateEmailMessage] = (
(JsPath \ "to").write[List[EmailRecipient]] and
(JsPath \ "global_merge_vars").write[JsArray]
.contramap[List[(String, String)]](list => JsArray(list.map(g => Json.obj("name" -> g._1, "content" ->g._2))))
)(unlift(TemplateEmailMessage.unapply))
}
Other option, is to convert the (String, String) tuple to a case class and create a writer for it.
case class Variable(name: String, content: String)
object Variable{
implicit val variableWrites: Writes[Variable] = (
(JsPath \ "name").write[String] and
(JsPath \ "content").write[String]
)(unlift(Variable.unapply))
}
And then:
implicit val templateEmailMessageWrites: Writes[TemplateEmailMessage] = (
(JsPath \ "to").write[List[EmailRecipient]] and
(JsPath \ "global_merge_vars").write[List[Variable]]
)(unlift(TemplateEmailMessage.unapply))

Play framework - Writes partial object

I have the following object:
case class A(id: Long, name:String, lastName:Option[String], age:Int)
I want to use Writes convertor to write only part of this object.
So I tried this code (trying to write the object without the age):
implicit val aWrites: Writes[A] = (
(JsPath \ "it").write[Long] and
(JsPath \ "name").write[String] and
(JsPath \ "lastName"). writeNullable[String]
)(unlift(A.unapply))
But this obviously doesn't compile.
Is there a way to make this work?
You can do this, but you can't reference A.unapply. Part of the conciseness of JSON combinators comes from the apply and unapply methods that are automatically generated by the compiler for case classes. But these methods use all of the parameters in the class.
A.unapply has the signature A => Option[(Long, String, Option[String], Int)]. This is incompatible with combinators that only cover three fields (instead of all four). You have two options.
1) Write another unapply-like method that has the correct signature:
def unapplyShort(a: A): Option[(Long, String, Option[String], Int)] =
Some((a.id, a.name, a.lastName))
implicit val aWrites: Writes[A] = (
(JsPath \ "id").write[Long] and
(JsPath \ "name").write[String] and
(JsPath \ "lastName").writeNullable[String]
)(unlift(unapplyShort))
2) Manually create the Writes as an anonymous class:
implicit val aWrites: Writes[A] = new Writes[A] {
def writes(a: A): JsValue = Json.obj(
"id" -> a.id,
"name" -> a.name,
"lastName" -> a.lastName
)
}
Option 1) from #m-z can be put more succinctly as:
implicit val aWrites: Writes[A] = (
(JsPath \ "id").write[Long] and
(JsPath \ "name").write[String] and
(JsPath \ "lastName").writeNullable[String]
)((a) => (a.id, a.name, a.lastName))
FYI, you can achieve that like the following. (This is just a suggestion.)
object noWrites extends OWrites[Any] {
override def writes(o: Any): JsObject = JsObject(Seq())
}
implicit val aWrites: Writes[A] = (
(JsPath \ "id").write[Long] and
(JsPath \ "name").write[String] and
(JsPath \ "lastName").writeNullable[String] and noWrites // <---
)(unlift(A.unapply))

Serialize only specific attributes using Writes trait with unapply

Lets imagine I have a case class like this:
case class Product(ean: Long, name: String, description: String)
and I want so serialize objects of this class to Json, I can implement the Writes trait like this:
implicit val productWrites: Writes[Product] = (
(JsPath \ "ean").write[Long] and
(JsPath \ "name").write[String] and
(JsPath \ "description").write[String]
)(unlift(Product.unapply))
This works fine if I want to serialize all the attributes of the object. Now lets say I don't want to serialize the ean. I tried something like this:
implicit val productWrites: Writes[Product] = (
(JsPath \ "name").write[String] and
(JsPath \ "description").write[String]
)(unlift(Product.unapply))
This doesn't seem to work since one needs to use all the fields/attributes that the unapply method returns.
Is there a way to make the second serialization method work with only the attributes that I want to serialize or do I have to use something like this:
implicit object ProductWrites extends Writes[Product] {
def writes(p: Product) = Json.obj(
"name" -> Json.toJson(p.name),
"description" -> Json.toJson(p.description)
)
}
Is this the only way?
unlift(Product.unapply) has a type Product => (Long, String, String).
In this case, the argument should have a type Product => (String, String). You can write a function literal like following.
implicit val productWrites: Writes[Product] = (
(JsPath \ "name").write[String] and
(JsPath \ "description").write[String]
)(p => (p.name, p.description))
I think your last example is the way to go. Here's another way of doing the same thing using an implicit val instead of an implicit object:
implicit val productWrites: Writes[Product] = Writes { p =>
Json.obj(
"name" -> Json.toJson(p.name),
"description" -> Json.toJson(p.description)
)
}

Play Json add field which is not present in class

Let's say I have a class like this:
abstract class SomeSuperClass(name: String)
case class SomeClass(someString: String, opt: Option[String]) extends SomeSuperClass("someName")
I want to serialize this class and be able to add the name field, this was my first approach:
implicit def serialize: Writes[SomeClass] = new Writes[SomeClass] {
override def writes(o: SomeClass): JsValue = Json.obj(
"someString" -> o.someString,
"opt" -> o.opt,
"name" -> o.name
)
}
This returns null if there's a None, so I changed my implementation following the documentation to this:
implicit def serialize: Writes[SomeClass] = (
(JsPath \ "someString").write[String] and
(JsPath \ "opt").writeNullable[String] and
(JsPath \ "name").write[String]
)(unlift(SomeClass.unapply))
This doesn't compile, it works only if I remove the name field:
[error] [B](f: B => (String, Option[String], String))(implicit fu: play.api.libs.functional.ContravariantFunctor[play.api.libs.json.OWrites])play.api.libs.json.OWrites[B] <and>
[error] [B](f: (String, Option[String], String) => B)(implicit fu: play.api.libs.functional.Functor[play.api.libs.json.OWrites])play.api.libs.json.OWrites[B]
[error] cannot be applied to (api.babylon.bridge.messaging.Command.SomeClass => (String, Option[String]))
[error] (JsPath \ "opt").writeNullable[String] and
How can I add a field which is not strictly present in a case class and has an optional field?
I'm using play-json 2.3.0.
Instead of using the default (compiler generated) unapply method, which knows nothing about your inherited values, you can write your own extractor for the JSON writes which takes a SomeClass instance and returns a (String, Option[String], String) tuple, i.e:
implicit def serialize: Writes[SomeClass] = (
(JsPath \ "someString").write[String] and
(JsPath \ "opt").writeNullable[String] and
(JsPath \ "name").write[String]
)(s => (s.someString, s.opt, s.name))
This gives you:
Json.toJson(SomeClass("foo", None))
// {"someString":"foo","name":"someName"}
Jspm.toJson(SomeClass("foo", Some("bar")))
// {"someString":"foo","opt":"bar","name":"someName"}