Skip to content

Instantly share code, notes, and snippets.

@nsphung
Last active June 28, 2017 09:21
Show Gist options
  • Save nsphung/ec9c5d870acbf145b4bfe18de7af1175 to your computer and use it in GitHub Desktop.
Save nsphung/ec9c5d870acbf145b4bfe18de7af1175 to your computer and use it in GitHub Desktop.
Decode a Json Map with Play-Json + Joda DateTime pattern fix
import net.messages.RightPrivilege
import org.joda.time.DateTime
import play.api.libs.json._
// Works with play-json 2.4.8
implicit val rightPrivilegeFormat = Json.format[RightPrivilege]
// Read/Writes for Joda DateTime avoid (error.expected.jodadate.format due to default pattern)
implicit val tsreads: Reads[DateTime] = Reads.of[String] map (new DateTime(_))
implicit val tswrites: Writes[DateTime] = Writes { (dt: DateTime) => JsString(dt.toString)}
val json = "[{\"PRIVILEGE_1\":[{\"source_id\":\"hello\",\"date_started\":\"2016-01-04T18:17:04+0100\",\"date_ended\":\"2016-01-04T19:17:04+0100\",\"status\":\"started\"},{\"source_id\":\"Foo\",\"date_started\":\"2016-01-04T19:17:04+0100\",\"date_ended\":\"2016-01-04T20:17:04+0100\",\"status\":\"ended\"}]},{\"PRIVILEGE_2\":[{\"source_id\":\"World\",\"date_started\":\"2016-01-04T18:17:04+0100\",\"date_ended\":\"2016-01-04T19:17:04+0100\",\"status\":\"pending\"}]}]"
val playjson = Json.parse(json)
val array = playjson.as[JsArray]
val rights = array.value.map{
truc => truc.as[Map[String, Seq[RightPrivilege]]]
}
rights.tail
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment