1

I have JSON structure:

  {
  "date": "2015-01-01",
  "id": 100,
  "movies":[
    {
      "id": 1,
      "length": 131,
      "timestamp": 1447837200
    },
    {
      "id": 2,
      "length": 131,
      "timestamp": 1447840800
    }
]
}

I need some validation: - "date" is a valid date; - "id" - is a Number in present List; - "movies" - is a List of Objects;

And transformation: - "timestamp" - Parse unixtimestamp to Date string ( HH:mm )

I need put "date" and "id" from root to each array element

So, result object:

[
    {
      "id": 1,
      "length": 131,
      "timestamp": "09:00",
      "parent_id": 100,
      "date": "2015-01-01"
    },
    {
      "id": 2,
      "length": 131,
      "timestamp": "10:00",
      "parent_id": 100,
      "date": "2015-01-01"
    }
]

I wrote some code, witch transform timestamp, but it looks terrible..

  val inputJson = play.api.libs.json.Json.parse(
    """{
  "date": "2015-01-01",
  "id": 100,
  "movies":[
    {
      "id": 1,
      "length": 131,
      "timestamp": 1447837200
    },
    {
      "id": 2,
      "length": 131,
      "timestamp": 1447840800
    }
]
}"""
  )

  val timestampTransform = (
    ( __ \ "timestamp").json.update(
      __.read[JsNumber].map(
        timestamp => {
          JsString(new DateTime(timestamp.asOpt[Int].getOrElse(0)).toString("HH:mm"))
        } )
    ))

  val reads = ( __ \ 'movies).json.update(
    __.read[JsArray].map(
      movies => {
        val result = movies.as[List[JsObject]].map(
          element => {
            element.transform(timestampTransform).get
          })
        Json.toJson(result)
      }
    )
  )

  println(inputJson.transform(reads))

1 Answer 1

1

Here is another terrible way to transform.

For <= play 2.3.x

import play.api.libs.json._
import org.joda.time.DateTime

case class Movie(id: Int, date: String, parent_id: Int, length: Int, timestamp: String)
implicit val mWritess = Json.writes[Movie]
implicit val nreads = new Reads[JsValue] {
  def reads(json: JsValue): JsResult[JsValue] = json match {
    case JsObject(Seq(("date", JsString(date)), ("id", pid: JsNumber), ("movies", JsArray(movies)))) => {
      movies match {
        case s: scala.collection.mutable.ListBuffer[_] => {
          val collectedId: Seq[Movie] = s.collect {
            case JsObject(Seq(("id", mid: JsNumber), ("length", length: JsNumber), ("timestamp", timestamp: JsNumber))) => {
              Movie(mid.asOpt[Int].getOrElse(0), date, pid.asOpt[Int].getOrElse(0), length.asOpt[Int].getOrElse(0), 
              new DateTime(timestamp.asOpt[Int].getOrElse(0)).toString("HH:mm"))
            }
          }
          JsSuccess(Json.toJson(collectedId))
        }
        case _ => JsError(s"Error '$date', $pid, $movies")
      }
    }
    case _ => JsError("Invalid format")
  }
}

println(inputJson.transform(nreads))

For play 2.4.x

case class Movie(id: Int, date: String, parent_id: Int, length: Int, timestamp: String)
implicit val mWritess = Json.writes[Movie]
implicit val nreads = new Reads[JsValue] {
  def reads(json: JsValue): JsResult[JsValue] = json match {
    case values: JsObject => {
      values.fields match {
        case Seq(("date", JsString(date)), ("id", pid: JsNumber), ("movies", JsArray(movies))) =>
          movies match {
            case s: scala.collection.mutable.ListBuffer[_] => {
              val collectedId: Seq[Movie] = s.collect {
                case movies: JsObject => {
                  movies.fields match {
                    case Seq(("id", mid: JsNumber), ("length", length: JsNumber), ("timestamp", timestamp: JsNumber)) =>
                      Movie(mid.asOpt[Int].getOrElse(0), date, pid.asOpt[Int].getOrElse(0), length.asOpt[Int].getOrElse(0), 
                      new DateTime(timestamp.asOpt[Int].getOrElse(0)).toString("HH:mm"))
                  }
                }
              }
              JsSuccess(Json.toJson(collectedId))
            }
            case _ => JsError(s"Error '$date', $pid, $movies")
          }
      }
    }
    case _ => JsError("Invalid format")
  }
}

println(inputJson.transform(nreads))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.