Playframework - JSON parsing object with single field - definition issue - json

I cannot find a way how to make it work when deserialized object has single field - I cannot compile the code. Seems that and operator does some transformation and I cannot find a method to call to do the same.
I have following json:
{"total": 53, "max_score": 3.2948244, "hits": [
{
"_index": "h",
"_type": "B",
"_id": "3413569628",
"_score": 3.2948244,
"_source": {
"fotky": [
{
"popisek":" ",
"localFileSystemLocation":" ",
"isMain": true,
"originalLocation": ""
}
]
}
}
]
}
I try the following data model to de serialize to:
case class SearchLikeThisResult(total: Int, max_score: Double, hits: Seq[Hits])
case class Hits(_index: String, _type: String, _id: String, _score: Double, _source: Source)
case class Source(fotky: Seq[Photo])
case class Photo(isMain: Boolean, originalLocation: Option[String], localFileSystemLocation: Option[String], popisek: Option[String])
Implicit reads as follows:
object SearchLikeThisHits {
import play.api.libs.functional.syntax._
implicit val photoReads: Reads[Photo] = (
(JsPath \ "isMain").read[Boolean] and
(JsPath \ "originalLocation").readNullable[String] and
(JsPath \ "localFileSystemLocation").readNullable[String] and
(JsPath \ "popisek").readNullable[String]
)(Photo.apply _)
implicit val sourceReads: Reads[Source] = (
(JsPath \ "fotky").read[Seq[Photo]]
)(Source.apply _)
implicit val hitsReads: Reads[Hits] = (
(JsPath \ "_index").read[String] and
(JsPath \ "_type").read[String] and
(JsPath \ "_id").read[String] and
(JsPath \ "_score").read[Double] and
(JsPath \ "_source").read[Source]
)(Hits.apply _)
implicit val searchLikeThisResult: Reads[SearchLikeThisResult] = (
(JsPath \ "total").read[Int] and
(JsPath \ "max_score").read[Double] and
(JsPath \ "hits").read[Seq[Hits]]
)(SearchLikeThisResult.apply _)
}
What I am really struggling with is under the _source:
implicit val sourceReads: Reads[Source] = (
(JsPath \ "fotky").read[Seq[Photo]]
)(Source.apply _)
where read is reported as unkown symbol - in other cases and performs some transformation.
Inline definition doesn't help either.
Does anybody faced this before?

The fancy applicative builder syntax (and, etc.) is nice, but it can obscure the fact that Reads is monadic and also works perfectly well with map, flatMap, for-comprehensions, etc.
So while the applicative builder syntax doesn't work with single values, plain old map does:
implicit val sourceReads: Reads[Source] =
(JsPath \ "fotky").read[Seq[Photo]].map(Source(_))
The key here is that (JsPath \ "fotky").read[Seq[Photo]] is a Reads[Seq[Photo]], and you want a Reads[Source]. map gives you a way to get from one to the other, just as you could use it to transform an Option[Seq[Photo]] into an Option[Source, for example.

You could save yourself some trouble by making use of the Json.reads to automatically generate your Reads (provided the case class is defined exactly like the Json objects - which is your case).
implicit val photoReads = Json.reads[Photo]
implicit val sourceReads = Json.reads[Source]
implicit val hitsReads = Json.reads[Hits]
implicit val searchResultReads = Json.reads[SearchLikeThisResult]
For more information, see https://www.playframework.com/documentation/2.1.1/ScalaJsonInception

Related

Processing JSON Response in Scala play WS

I would like to process the following JSON response:
[
{
"name":"xxx1",
"format":"xxx2",
"type":"xxx3",
"url":"xxx4"
},
{
"name":"yyy1",
"format":"yyy2",
"type":"yyy3",
"url":"yyy4"
}
]
I am tying to write the related code, but I am stuck here:
case class Repository(repoName: String, repoFormat: String, repoType: String, repoURL: String)
case class RepositoryList(repositoryList: List[Repository])
implicit val repoReads = Reads[Repository] = (
(JsPath \ "name").read[String] and
(JsPath \ "format").read[String] and
(JsPath \ "type").read[String] and
(JsPath \ "url").read[String]
)(Repository.apply _)
implicit val repoListReads = Reads[RepositoryList] = (
(???).read[Seq[Repository]]
)(RepositoryList.apply _)
request.get().map(response => Ok((response.json).validate[RepositoryList]))
What comes to the "???" part? It is basically a list of JSONs, there is no attribute on that level.
Thanks in advance!
A pragmatic solution would be just to use List[Repository] and then create a RepositoryList:
request.get()
.map(response =>
Ok(
(response.json).validate[List[Repository]]
.map(RepositoryList)
)
)
Check scalafiddle
In terms of using validate[RepositoryList] and avoiding the extra map after validate, using Reads.list composition would do the trick. So the ??? could be replaced with
implicit val repoListReads: Reads[RepositoryList] = Reads.list[Repository].map(RepositoryList)
and you can call
response.json.validate[RepositoryList]

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))
}

Dynamic Json values in Play Framework Json

I am currently using the Play framework json parser in order to parse a json string in my scala code.
I have the following class:
case class Address(address: String,
gps: GPS,
country: String) {}
object Address {
implicit val reads: Reads[Address] = (
(JsPath \ "address").read[String] and
(JsPath \ "gps").read[GPS] and
(JsPath \ "country").read[String]
) (Address.apply _)
implicit val writes: Writes[Address] = (
(JsPath \ "address").write[String] and
(JsPath \ "gps").write[GPS] and
(JsPath \ "country").write[String]
) (unlift(Address.unapply))
}
Which works fine with the following json:
{
"address": "123 Fake Street",
"country": "USA",
"gps": { ... }
}
The problem is that in some situations the json may instead have the gps field be a string which doesnt parse, i.e.
{
"address": "123 Fake Street",
"country": "USA",
"gps": "123abc"
}
Now I know that I cant have the gps member be both a string or a GPS object, but is there any way to have it be say an Option[GPS] and only have a value if the json contained a gps object?
Only very little is needed to be changed in your impl.
You need to read the field "gps" as something that is 'safe' like JsValue and then try to map it into your GPS case class if it can be done, if not, return None.
case class GPS(a:String, b:String)
object GPS {
val travelInfoReads = Json.reads[GPS]
val travelInfoWrites = Json.writes[GPS]
implicit val travelInfoFormat: Format[GPS] = Format(travelInfoReads, travelInfoWrites)
}
case class Address(address: String,
gps: Option[GPS],
country: String) {}
object Address {
implicit val reads: Reads[Address] = (
(JsPath \ "address").read[String] and
(JsPath \ "gps").read[JsValue].map(js => js.asOpt[GPS]) and
(JsPath \ "country").read[String]
) (Address.apply _)
implicit val writes: Writes[Address] = (
(JsPath \ "address").write[String] and
(JsPath \ "gps").writeNullable[GPS] and
(JsPath \ "country").write[String]
) (unlift(Address.unapply))
}
I also tested it:
val json = Json.toJson(Address("1",Some(GPS("a","b")),"2"))
println(json)
println(json.as[Address])
val newObj: JsObject = (json.as[JsObject] - "gps") + ("gps" -> JsNumber(1))
println(newObj)
val a = newObj.as[Address]
println(a)
a must beEqualTo(Address("1",None,"2"))
Output was like
{"address":"1","gps":{"a":"a","b":"b"},"country":"2"}
Address(1,Some(GPS(a,b)),2)
{"address":"1","country":"2","gps":1}
Address(1,None,2)

playframework - Best way to trim a json values

I am trying to figure out the best and elegant way to tim values on an in coming json.
So for example I have the following json:
{
"firstName": " foo",
"lastName": "bar "
}
With the following definitions:
case class Someone(firstName:String, lastName: String)
object Someone{
implicit val someoneReads: Reads[Someone] = (
(JsPath \ "firstName").read[String] and
(JsPath \ "lastName").read[String]
)(Someone.apply _)
}
Is there a way to trim the json while reading it? or I need to write a transformer for that? and if I do, how to write it so it will be generic to trip every json I will provide?
Thanks!
Use map(_.trim) for read[String] for trim string (universal solution)
implicit val someoneReads: Reads[Someone] = (
(JsPath \ "firstName").read[String].map(_.trim) and
(JsPath \ "lastName").read[String].map(_.trim)
)(Someone.apply _)
You can implement own Reads[String] with trimmed string also
def trimmedString(path: JsPath): Reads[String] = Reads.at[String](path).map(_.trim)
implicit val someoneReads: Reads[Someone] = (
trimmedString(JsPath \ "firstName") and trimmedString(JsPath \ "lastName")
)(Someone.apply _)
For a more familiar view of code you may implement implicit conversion
import scala.language.implicitConversions
class JsPathHelper(val path: JsPath) {
def trimmedString: Reads[String] = Reads.at[String](path).map(_.trim)
}
implicit def toJsPathHelper(path: JsPath): JsPathHelper = new JsPathHelper(path)
implicit val someoneReads: Reads[Someone] = (
(JsPath \ "firstName").trimmedString and
(JsPath \ "lastName").trimmedString
)(Someone.apply _)
You can specify your own reads[String] based on the default one, and then use macros:
object Someone {
implicit val trimStringReads: Reads[String] = Reads.StringReads.map(_.trim)
implicit val someoneReads: Reads[Someone] = Json.reads[Someone]
}

Play - Custom Json with Generic Crud

I have a Crud Controller in my application which works perfectly with JsonInception, but fail with a custom json converter.
Follow the given case class:
case class Validity(id: Option[UUID], objectType: String, since: DateTime, until: DateTime, objectId: UUID, validityTargetId: UUID, validityTargetType: String)
I have an object companion as follow:
object Validity extends CrudObject[Validity] {
implicit val reads = Json.reads[Validity]
implicit val writes = Json.writes[Validity]
}
Where my CrudObject is a trait with the given code:
trait CrudObject[T] {
val reads: Reads[T]
val writes: Writes[T]
}
This is needed since I'm working in a Generic Crud. Without this, Play is unable to find any implicit converter.
So my Generic Crud Controller, is something like this:
trait CrudController[T] extends Controller {
def service: ServiceModule[T]
def companion: CrudObject[T]
def search...
def insert...
implicit def reads: Reads[T] = companion.reads
implicit def writes: Writes[T] = companion.writes
and for each controller, I have the follow:
object ValidityController extends CrudController[Validity] {
override def service: GenericServiceModule[Validity] = ServiceModule
override def companion: CrudObject[Validity] = Validity
}
Ok, with this design in mind, as I said, works perfectly, I need to design a custom json converter. Yes, I know, it's pretty simple, but something is happening and I don't know how to get rid of that.
Now I'm trying to do the following:
implicit val reads: Reads[Validity] = (
(JsPath \ "id").read[String] and
(JsPath \ "objectType").read[String] and
(JsPath \ "since").read[Long] and
(JsPath \ "until").read[Long] and
(JsPath \ "objectId").read[String] and
(JsPath \ "validityTargetId").read[String] and
(JsPath \ "validityTargetType").read[String]
)(unlift(Validity.apply _))
and it gives me:
Type mismatch, expected: (NotInferedA) => Option[NotInferedB], actual: (CrudObject[Nothing]) => CrudObject[Nothing]
I believe this is happen cuz CrudObject is a trait and does not have apply and unapply.
Anyway, removing CrudObject gives me a similar error:
Type mismatch, expected: (NotInferedA) => Option[NotInferedB], actual: (Option[UUID], String, DateTime, DateTime, UUID, UUID, String) => Validity
but even if I can solve this, I can't imagine living without CrudObject due my GenericCrud.
Any Thoughts?
PS: Thanks to #m-z who has been giving me some assist through stackoverflow =)
UPDATE
I'm almost there with this approach:
implicit object validityFormat extends Format[Validity] {
override def writes(o: Validity): JsValue = {
Json.obj(
"id" -> JsString(o.id.getOrElse(null).toString),
"objectType" -> JsString(o.objectType),
"since" -> JsString(o.since.toString),
"until" -> JsString(o.since.toString),
"objectId" -> JsString(o.objectId.toString),
"validityTargetId" -> JsString(o.validityTargetId.toString),
"validityTargetType" -> JsString(o.validityTargetType))
}
override def reads(json: JsValue): JsResult[Validity] = {
JsSuccess(Validity(
(json \ "id").as[Option[UUID]],
(json \ "objectType").as[String],
(json \ "since").as[DateTime],
(json \ "until").as[DateTime],
(json \ "objectId").as[UUID],
(json \ "validityTargetId").as[UUID],
(json \ "validityTargetType").as[String])
)
}
}
Using this way, which is different from documentation Scala Combinators, I don't get the previous error, which is good, no type mismatch =)
Now I'm working out on to figure out how to convert to UUID and DateTime.
UPDATE
I did an example that works, but I accepted the #m-z answer because there are less boilerplate than mine, but both worked fine. The big difference is, in my approach I needed to provide some custom converters for DateTime and for UUID, whereas #m-z approach you don't!
implicit object UUIDFormatter extends Format[UUID] {
override def reads(json: JsValue): JsResult[UUID] = {
val uuid = json.validate[String]
JsSuccess(UUID.fromString(uuid.get))
}
override def writes(o: UUID): JsValue = {
JsString(o.toString)
}
}
implicit object DateTimeFormatter extends Format[DateTime] {
override def reads(json: JsValue): JsResult[DateTime] = {
val datetime = json.validate[Long]
JsSuccess(new DateTime(datetime.get))
}
override def writes(o: DateTime): JsValue = {
JsNumber(o.getMillis)
}
}
implicit object validityFormat extends Format[Validity] {
override def writes(o: Validity): JsValue = {
Json.obj(
"id" -> JsString(o.id.getOrElse(null).toString),
"objectType" -> JsString(o.objectType),
"since" -> JsNumber(o.since.getMillis),
"until" -> JsNumber(o.since.getMillis),
"objectId" -> JsString(o.objectId.toString),
"validityTargetId" -> JsString(o.validityTargetId.toString),
"validityTargetType" -> JsString(o.validityTargetType))
}
override def reads(json: JsValue): JsResult[Validity] = {
JsSuccess(Validity(
(json \ "id").as[Option[UUID]],
(json \ "objectType").as[String],
(json \ "since").as[DateTime],
(json \ "until").as[DateTime],
(json \ "objectId").as[UUID],
(json \ "validityTargetId").as[UUID],
(json \ "validityTargetType").as[String])
)
}
There are a couple problems here, but neither of them are relevant to generics, because you're dealing with the concrete type Validity.
First, the last argument using Reads combinators should be (Validity.apply _). You would only use unlift with Writes.
Second, the types in the combinators must map to the types in your Validity class.
implicit val reads: Reads[Validity] = (
(JsPath \ "id").readNullable[UUID] and // readNullable reads to Option
(JsPath \ "objectType").read[String] and
(JsPath \ "since").read[DateTime] and
(JsPath \ "until").read[DateTime] and
(JsPath \ "objectId").read[UUID] and
(JsPath \ "validityTargetId").read[UUID] and
(JsPath \ "validityTargetType").read[String]
)(Validity.apply _)
Reads already exist for UUID and DateTime, so this should work okay.
Similarly, Writes[Validity] would look like this:
implicit val writes: Writes[Validity] = (
(JsPath \ "id").writeNullable[UUID] and
(JsPath \ "objectType").write[String] and
(JsPath \ "since").write[DateTime] and
(JsPath \ "until").write[DateTime] and
(JsPath \ "objectId").write[UUID] and
(JsPath \ "validityTargetId").write[UUID] and
(JsPath \ "validityTargetType").write[String]
)(unlift(Validity.unapply))
Hi I Think that your case class has an option parameter your Reads must have readnullable in the optional parameters must be like this , I'm not sure with UIID and String
case class Validity(id: Option[UUID], objectType: String, since: DateTime, until: DateTime, objectId: UUID, validityTargetId: UUID, validityTargetType: String)
implicit val reads: Reads[Validity] = (
(JsPath \ "id").readNullable[UIID] and
(JsPath \ "objectType").read[String] and
(JsPath \ "since").read[Long] and
(JsPath \ "until").read[Long] and
(JsPath \ "objectId").read[String] and
(JsPath \ "validityTargetId").read[String] and
(JsPath \ "validityTargetType").read[String]
)(unlift(Validity.apply _))