Play Json - Complex object creation - json

I'm trying to create Json readers in my Play framework application (Scala). The problem is, part of my Json is a little funky, and requires further processing to retrieve the values. For example:
{
"field1":"value1",
"field2":"value/1",
"num2":2
}
with case classes:
case class Field1(text: String, fields: Field2)
case class Field2(text: String, num: Int, num2: Int)
Basically the text and num fields for Field2 are derived from the value value/1, by splitting the text. Here's the splitter function:
def splitter(path: String, num2: Int): Field2 = {
val split = path.split("\\")
Field2(split(0), split(1).toInt, num2)
}
This is fairly straightforward, the actual splitter function is far more complex. Basically the only way to construct this object Field2 is to pass a single string to a function that spits out the required object.
How do I go about creating a reader for Field2 (and by extension for Field1)?
Here's what I have so far:
object Field1 {
implicit val reader = (
(__ \ "field1").read[String] and
(__).read[Field2]
) (Field1.apply _)
}
object Field2 {
implicit val reader = (
splitter((__ \ "field2").read[String], (__ \ "num2"))
) // Obviously incorrect syntax + type mismatch, but this is roughly what I'm trying to accomplish.
}

If you use the non-functional combinator syntax it becomes a little simpler I think:
object Field2 {
implicit val reader = Reads[Field2] { json =>
for {
path <- (json \ "field2").validate[String]
num2 <- (json \ "num2").validate[Int]
} yield splitter(path, num2)
}
}
Additionally if you want splitter to further validate the input have it return a JsResult[Field2] like this:
def splitter(path: String, num2: Int): JsResult[Field2] = {
val split = path.split("\\")
if (split.size != 2) {
JsError(s"$path must be of the form: {field}\\{num}")
} else {
Try(Field2(split(0), split(1).toInt, num2)).map(JsSuccess(_)).getOrElse {
JsError(s"${split(1)} is not a valid Int")
}
}
}
And then modify the reader:
object Field2 {
implicit val reader = Reads[Field2] { json =>
for {
path <- (json \ "field2").validate[String]
num2 <- (json \ "num2").validate[Int]
field2 <- splitter(path, num2)
} yield field2
}
}
If you still prefer using the functional syntax and you don't mind the lack of validation that splitter does try this:
def splitter(path: String, num2: Int): Field2 = {
val split = path.split("\\")
Field2(split(0), split(1).toInt, num2)
}
implicit val reader = (
(__ \ "field2").read[String] and
(__ \ "num2").read[Int]
)(splitter _)
I'd recommend against this, it's unsafe (split(1) and toInt both might throw exceptions) and the functional syntax can have performance issues. See https://www.lucidchart.com/techblog/2016/08/29/speeding-up-restful-services-in-play-framework/.

I don't know why do you need case classes, but you can also transform json for your needs with
(__ \ "field2" \ "num2").json.copyFrom((__ \ "num2").json.pick) and
(__ \ "field2").json.update(
of[String].map { o =>
val split = o.split("/")
Json.obj(
"text" -> split(0),
"num" -> split(1).toInt
)
}
)
).reduce andThen (__ \ "num2").json.prune
scala> j: play.api.libs.json.JsResult[play.api.libs.json.JsObject] = JsSuccess({"field2":{"num2":2,"text":"value","num":1},"field1":"value1"},/num2)
and then you can use your Reads[Field1]

Related

Overloaded method value [read] cannot be applied to (String => SearchController.this.TrackSearch)

I am querying the spotify api for a list of tracks for a given query using ws, when I come to transform the JSON data into a case class I'm getting an error that I've yet to figure out...
class SearchController #Inject() (
val ws: WSClient
) extends Controller {
case class TrackSearch(href: String)
implicit val trackResultsReads: Reads[TrackSearch] = (
(__ \ "tracks" \ "href").read[String]
)(TrackSearch.apply _)
def index = Action.async { implicit request =>
search("track", param(request, "q")).map { r =>
val ts = r.json.as[TrackSearch]
println(ts)
Ok
}
}
private def search(category: String, query: String): Future[Try[WSResponse]] = {
ws.url("https://api.spotify.com/v1/search")
.withQueryString("q" -> query, "type" -> category)
.get()
.map(Success(_))
.recover { case x => Failure(x) }
}
private def param(request: Request[AnyContent], name: String): String = {
request.queryString.get(name).flatMap(_.headOption).getOrElse("")
}
}
The error I am getting is:
Overloaded method value [read] cannot be applied to (String => SearchController.this.TrackSearch)
implicit val trackResultsReads: Reads[TrackSearch]
> (__ \ "tracks" \ "href").read[String]
)(TrackSearch.apply _)
If I query the JSPath in my action, I can get the "href" string back fine, so it is not that:
println(r._2.json \ "tracks" \ "href")
The issue is that there is just a single field. If you added a second field it would compile. I don't fully understand why it shouldn't compile with a single field. So in the single field case, try the following:
implicit val trackResultsReads: Reads[TrackSearch] = {
((__ \ "tracks" \ "href").read[String])
.map(TrackSearch(_))
}
Here is quite an old link where I found the above. See also this link for a similar SO question with a different approach.

How do you add validation to a Reads in Play Json?

Let's say I've got a reads that creates an object from JSON with two optional fields:
implicit val rd: Reads[MyObject] = (
(__ \ "field1").readNullable[String] and
(__ \ "field2").readNullable[String]
)(MyObject.apply _)
I want to check to make sure that the value of field1 is one of the values in the list:
List("foo", "bar")
I can do that after the fact, by creating a new MyObject and mapping the values through a function to transform them, but I feel like there should be a way to do this more elegantly using JSON transformers or something.
Ideally, I want the Reads to read the nullable value of field1 and transform it if it is defined, without the need to post-process it. Is there some way of sneaking a transform in there?
You can use this approach:
case class MyObject(a: Option[String], b: Option[String])
val allowedValues = Seq("foo", "bar")
implicit val reads: Reads[MyObject] = new Reads[MyObject] {
override def reads(json: JsValue): JsResult[MyObject] = {
val a = (json \ "a").asOpt[String].filter(allowedValues.contains)
val b = (json \ "b").asOpt[String]
JsSuccess(MyObject(a, b))
}
}
Usage examples:
scala> Json.parse(""" { "a": "bar", "b": "whatever"} """).validate[MyObject]
res2: play.api.libs.json.JsResult[MyObject] = JsSuccess(MyObject(Some(bar),Some(whatever)),)
scala> Json.parse(""" { "a": "other", "b": "whatever"} """).validate[MyObject]
res3: play.api.libs.json.JsResult[MyObject] = JsSuccess(MyObject(None,Some(whatever)),)
scala> Json.parse(""" {} """).validate[MyObject]
res4: play.api.libs.json.JsResult[MyObject] = JsSuccess(MyObject(None,None),)
Okay, after doing some more research, I came up with the following:
In play.api.libs.json.ConstraintReads there is a function called verifying(cond: A => Boolean) that returns a Reads[A]. This can be passed as a parameter to JsPath.readNullable[A] like so:
implicit val rd: Reads[MyObject] = (
(__ \ "field1").readNullable[String](verifying(allowedValues.contains)) and
(__ \ "field2").readNullable[String]
)(MyObject.apply _)
This will return a JsResponse, either a JsSuccess if "field1" validates, or a JsError if it doesn't validate. It actually fails on an invalid input, rather than just ignoring the input. That's more like the behaviour I wanted.
There are a number of other constraint functions that perform similar tests on the read value, as well.

PlayFramework in Scala, Reads - error in case of unknown key? [duplicate]

Play's JSON serialization is by default permissive when serializing from JSON into a case class. For example.
case class Stuff(name: String, value: Option[Boolean])
implicit val stuffReads: Reads[Stuff] = (
( __ \ 'name).read[String] and
( __ \ 'value).readNullable[Boolean]
)(Stuff.apply _)
If the following JSON was received:
{name: "My Stuff", value: true, extraField: "this shouldn't be here"}
It will succeed with a 'JsSuccess' and discard the 'extraField'.
Is there a way to construct the Json Reads function to have it return a JsError if there are 'unhandled' fields?
You can verify that the object doesn't contain extra keys before performing your own decoding:
import play.api.data.validation.ValidationError
def onlyFields(allowed: String*): Reads[JsObject] = Reads.filter(
ValidationError("One or more extra fields!")
)(_.keys.forall(allowed.contains))
Or if you don't care about error messages (and that one's not very helpful, anyway):
def onlyFields(allowed: String*): Reads[JsObject] =
Reads.verifying(_.keys.forall(allowed.contains))
And then:
implicit val stuffReads: Reads[Stuff] = onlyFields("name", "value") andThen (
(__ \ 'name).read[String] and
(__ \ 'value).readNullable[Boolean]
)(Stuff)
The repetition isn't very nice, but it works.
Inspired from Travis' comment to use LabelledGeneric I was able achieve compile time safe solution.
object toStringName extends Poly1 {
implicit def keyToStrName[A] = at[Symbol with A](_.name)
}
case class Foo(bar: String, boo: Boolean)
val labl = LabelledGeneric[Foo]
val keys = Keys[labl.Repr].apply
now keys.map (toStringName).toList will give you
res0: List[String] = List(bar, boo)

Parsing a List of Models in Play! 2.1.x

Given the JSON...
[ {"ID": "foo"}, {"ID": "bar"} ]
Represented with case classes...
case class Example(models: List[Model])
case class Model(id: String)
I attempt the following which fails with overloaded method value read with alternatives.
trait JsonReader {
implicit val modelReads: Reads[Model] = (__ \ "name").read[String](Model)
implicit val exampleReads: Reads[Example] = JsPath.read[List[Model]](Example)
def get (response: Response) = response.json.as[Example]
}
What is the correct way to parse this?
For a strange reason I did not find an elegant solution to read a json model with only one value. For 2 and more values you may write:
implicit val reader = (
(__ \ 'id).read[Long] and
(__ \ 'field1).read[String] and
(__ \ 'field2).read[String])(YourModel.apply _)
For a json with 1 field try using something like that:
implicit val reader = new Reads[Model] {
def reads(js: JsValue): JsResult[Model] = {
JsSuccess(Model((js \ "name").as[String]))
}
}
This should work but doesn't look nice :(

Play2.1 JSON Format with Empty String Validation

I am attempting to validate JSON when unmarshalling to an object in Play2.1. The Format object I have defined only validates when a field is absent in the JSON, but I want to validate that fields are nonEmpty strings. Is this possible? I've tried specifying the minLength() constraint (as seen here) in the reads() call, but I get a compiler error saying minLength can't be found. Is that only for the tuple approach?
See the following Specs2 Junit test which fails now, but should pass when the constraint is defined properly:
import org.specs2.mutable._
import play.api.libs.json._
class SimpleValidation extends SpecificationWithJUnit{
private val badPayload: JsValue = Json.obj(
"simpleValue1" -> "mySimpleValue", // Comment this line out to pass test
"simpleValue2" -> ""
)
"An IssueFormat" should {
"validate when unmarshalling" in {
badPayload.validate[SimpleObj].fold(
valid = (res => {
// Fail if valid
failure("Payload should have been invalid")
}),
invalid = (e => {
// Should be one error
e.length mustBeEqualTo(1)
}))
}
}
}
import play.api.libs.functional.syntax._
case class SimpleObj(simpleValue1: String, simpleValue2: String)
object SimpleObj {
val simpleReads = (
(__ \ "simpleValue1").read[String] and
(__ \ "simpleValue2").read[String])(SimpleObj.apply _) // read[String](minLength(0)) yields compiler error
val simpleWrites = (
(__ \ "simpleValue1").write[String] and
(__ \ "simpleValue2").write[String])(unlift(SimpleObj.unapply))
implicit val simpleFormat: Format[SimpleObj] = Format(simpleReads, simpleWrites)
}
You can use the minLength in Reads:
import play.api.libs.json.Reads._
Then minLength should be available, however, try this format instead:
implicit val simpleReads = (
(__ \ "simpleValue1").read(minLength[String](1)) and
(__ \ "simpleValue2").read(minLength[String](1))(SimpleObj.apply _)
After looking through the Play2.1 documentation some more, I was able to add a custome read validator. If you replace the SimpleObj from the original question, with the following, the test case will pass. Not sure if there is a simpler way to do this, but this definitely works:
object SimpleObj {
// defines a custom reads to be reused
// a reads that verifies your value is not equal to a give value
def notEqual[T](v: T)(implicit r: Reads[T]): Reads[T] = Reads.filterNot(ValidationError("validate.error.unexpected.value", v))(_ == v)
implicit val simpleReads = (
(__ \ "simpleValue1").read[String](notEqual("")) and
(__ \ "simpleValue2").read[String](notEqual("")))(SimpleObj.apply _)
val simpleWrites = (
(__ \ "simpleValue1").write[String] and
(__ \ "simpleValue2").write[String])(unlift(SimpleObj.unapply))
implicit val simpleFormat: Format[SimpleObj] = Format(simpleReads, simpleWrites)
}