ݺߣ

ݺߣShare a Scribd company logo
Json mapping in pLAY!
Fabian Gutierrez
Mandatory verbose java example
Jonathan
@JsonInclude(JsonInclude.Include.NON_NULL)
@Generated("org.jsonschema2pojo")
@JsonPropertyOrder({ "name", "age" })
public class Actor {
@JsonProperty("nombre") private String name;
@JsonProperty("age") private Integer age;

}
XKE PlayFramework's Json in scala
Seriously
 Building JsValues
 Reads
 Writes
 Paths
Plays json mapper
Building a JsValue
Types
 JsString
 JsNumber
 JsBoolean
 JsObject
 JsArray
 JsNull
Json.parse("""
|{
| "name" : "Jennifer",
| "Age": 26
|}
""".stripMargin
)
The Reader
Reading Json
case class Actor(name: String, age: Int)
object Actor {
// or default values
implicit val actorReads = Json.reads[Actor]
// functional syntax
implicit val actorReads: Reads[Actor] = (
(JsPath  "nombre").read[String] and
(JsPath  "age").read[Int]
)(Actor.apply _)
}
The (ghost) writer
Writing Json
object Actor {
//default values
implicit val actorWrites = Json.writes[Actor]
//or total control
implicit val actorWrites = new Writes[Actor] {
def writes(actor: Actor) = Json.obj(
"name" -> actor.name,
"age" -> actor.age
)
}
//or functional syntax
implicit val actorWrites: Writes[Actor] = (
(JsPath  "name").write[String] and
(JsPath  "age").write[Int]
)(unlift(Actor.unapply))
}
XKE PlayFramework's Json in scala
Paths
// direct parsing
val jsPerson: JsObject = ...
(jsPerson "name").as[String]
(jsPerson "age").as[Int]
// as optionals
val jsPersonList: JsObject = ...
(jsPersonList "name").asOpt[List[String]]
https://github.com/fagossa/xke_play_json

More Related Content

XKE PlayFramework's Json in scala

  • 1. Json mapping in pLAY! Fabian Gutierrez
  • 2. Mandatory verbose java example Jonathan @JsonInclude(JsonInclude.Include.NON_NULL) @Generated("org.jsonschema2pojo") @JsonPropertyOrder({ "name", "age" }) public class Actor { @JsonProperty("nombre") private String name; @JsonProperty("age") private Integer age; }
  • 4. Seriously Building JsValues Reads Writes Paths
  • 6. Building a JsValue Types JsString JsNumber JsBoolean JsObject JsArray JsNull Json.parse(""" |{ | "name" : "Jennifer", | "Age": 26 |} """.stripMargin )
  • 8. Reading Json case class Actor(name: String, age: Int) object Actor { // or default values implicit val actorReads = Json.reads[Actor] // functional syntax implicit val actorReads: Reads[Actor] = ( (JsPath "nombre").read[String] and (JsPath "age").read[Int] )(Actor.apply _) }
  • 10. Writing Json object Actor { //default values implicit val actorWrites = Json.writes[Actor] //or total control implicit val actorWrites = new Writes[Actor] { def writes(actor: Actor) = Json.obj( "name" -> actor.name, "age" -> actor.age ) } //or functional syntax implicit val actorWrites: Writes[Actor] = ( (JsPath "name").write[String] and (JsPath "age").write[Int] )(unlift(Actor.unapply)) }
  • 12. Paths // direct parsing val jsPerson: JsObject = ... (jsPerson "name").as[String] (jsPerson "age").as[Int] // as optionals val jsPersonList: JsObject = ... (jsPersonList "name").asOpt[List[String]]