Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- val json: JsValue = Json.parse("""
- {
- "item": {
- "id" : "11111111",
- "name" : "box",
- "categories" : [{
- "name" : "blue box",
- "id" : "12345",
- },
- {
- "name" : "fancy box",
- "id" : "98765",
- }]
- }
- }
- """)
- //I define my class and reader for one category
- case class ItemCategory(id: Option[String], name: String)
- implicit val categoryRead: Reads[ItemCategory] = (
- (JsPath "item" "categories" "id").readNullable[String] and
- (JsPath "item" "categories" "name").read[String]
- )(ItemCategory.apply _)
- //I define my class and reader for one item
- case class MyItem(categories : Option[List[ItemCategory]], id : Option[String], name : Option[String])
- implicit val myItemRead: Reads[MyItem] = (
- (JsPath "item" "categories").readNullable[List[ItemCategory]] and
- (JsPath "item" "id").readNullable[String] and
- (JsPath "item" "name").readNullable[String]
- )(MyItem.apply _)
- //I then try to read :
- var item: JsResult[MyItem] = json.validate[MyItem](myItemRead)
- println(item)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement