playframework 2.0 - Play Framework Json Transform recursivly -
since while fooling around following problem.
i try recursivly transform through tree-like json structure.
{ "id": 21, "title": "title1", "children": [ { "id": 22, "title": "title1.1", "children": [ { "id": 33, "title": "title1.1.1", "children": [ { "id": 41, "title": "title1.1.1.1", "children": [ { "id": 42, "title": "title1.1.1.1.1", } ] } ] } ] } ] }
so far have :
val idchanger:reads[jsobject] = (__ \ 'id).json.update(of[jsnumber].map{case jsnumber(nb) => jsnumber(nb * 100)}) val childpicker:reads[jsvalue] = (__ \ 'children).json.pick[jsarray].map{ case jsarray(arr) => jsarray(arr.map(o => { o.transform(idchanger) match { case jssuccess(value, jspath) => println(s"success:$value") value case jserror(errors) => println(s"error:$errors"); jsnull } })) } val jsvalue: jsvalue = ... jsvalue.transform(childpicker) ...
this works on first level , changes id '21' accordingly, not rest far.
i tried many different ways rest of children recursivly too, no success far.
any on 1 appreciated.
cheers, rob.
just add recursive call inside reads , make sure handle jsarray jsobject. here simple example fits basic idea.
val idchanger: reads[jsobject] = (__ \ 'id).json.update(of[jsnumber].map{case jsnumber(nb) => jsnumber(nb * 100)}) val picker: reads[jsvalue] = __.json.pick.map{ case jsarray(arr) => jsarray(arr.map(_.transform(picker).get)) case v: jsvalue => v.transform( idchanger andthen (__ \ "children").json.update(picker) orelse idchanger ).get }
Comments
Post a Comment