[2.1-scala] JSON Transformers and arrays

1,398 views
Skip to first unread message

John Kodumal

unread,
Apr 1, 2013, 8:23:54 PM4/1/13
to play-fr...@googlegroups.com
Hey all,

I've been playing around a bit with JSON transformers. Most things work as expected, but I'm having issues around JsPaths containing array indices. For example:

val json = Json.parse("""{"foo": [1,2]}""")
val transform = (__ \ 'foo)(0).json.prune
json.validate(transform) // what should this do?

The semantics of prune aren't really described clearly, so I'm not sure-- I guess I'd expect this to return {"foo" : [2]}, but instead I get: validate.error.expected.jsobject. One blog post states that prune "removes given JsPath from input JSON" [1], and under that interpretation I suppose prune can't ever succeed on a JsArray.

If that's the answer, how is one supposed to perform transformations like adding an element to an array, modifying or deleting an element? My attempts using JsPaths containing array indices and transformers like update all seem to fail. There's a stackoverflow post [2] which provides the solution of selecting the entire array and updating it, which is kind of heavy-handed.

In case anyone's interested, I'm attempting to build an open-source Play! module for the application/json+patch format [3]. The Play! apis make this fairly elegant, except for the case I've described here.

cheers,
John 



Pascal Voitot Dev

unread,
Apr 2, 2013, 3:52:40 AM4/2/13
to play-fr...@googlegroups.com
In current version, there are a few limitations about JsArray such as the one you discovered.
Manipulating arrays is a bit more tricky (what happens when you merge 2 arrays, insert an element at a non-existing index etc...?).
For now, access the JsArray sequence value directly and manipulate it from there...

regards
Pascal




--
You received this message because you are subscribed to the Google Groups "play-framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to play-framewor...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

John Kodumal

unread,
Apr 2, 2013, 11:04:34 AM4/2/13
to play-fr...@googlegroups.com
Hey Pascal,

Thanks for the response!

So the limitations I mentioned are all bugs? Are they filed in GH? I get that merging arrays might be tricky, but update and prune seem pretty straightforward. Updating / navigating to a non-existent index could return a JsError, right?

Accessing the JsArray sequence value directly isn't really a great solution in my case, as I'm trying to write generic transformers based on a JSON patch document. The patch document contains an embedded path string that may include array indices, which means that the transformation may involve navigating deep within a JSON document. For example, given the following JSON document:

{"foo": [{"bar": true}]}

One can submit a JSON patch document like:

[ {"op": "replace", "path" : "/foo/0/bar", "value" : false} ]

to yield the following transformed JSON:

{"foo" : [{"bar": false} ] }


Writing these generically using JSON transformers is basically a one-liner per operation:


Except that if the path contains an array, even if it's just selecting an element and navigating further into the document, the transformers break.

John

Pascal Voitot Dev

unread,
Apr 2, 2013, 11:19:41 AM4/2/13
to play-fr...@googlegroups.com
On Tue, Apr 2, 2013 at 5:04 PM, John Kodumal <jkod...@gmail.com> wrote:
Hey Pascal,

Thanks for the response!

So the limitations I mentioned are all bugs? Are they filed in GH? I get that merging arrays might be tricky, but update and prune seem pretty straightforward. Updating / navigating to a non-existent index could return a JsError, right?


These are not really bugs but limitations, isn't it? :D

But no these cases aren't filed yet because we were also waiting for people to discover the API and use it. We already know a few axis of improvement but we wanted to know how the API was used before choosing what to improve at first.
And it's cool to see what people begin to do and we will bring new features and correct limitations progressively!

Accessing the JsArray sequence value directly isn't really a great solution in my case, as I'm trying to write generic transformers based on a JSON patch document. The patch document contains an embedded path string that may include array indices, which means that the transformation may involve navigating deep within a JSON document. For example, given the following JSON document:

{"foo": [{"bar": true}]}

One can submit a JSON patch document like:

[ {"op": "replace", "path" : "/foo/0/bar", "value" : false} ]

to yield the following transformed JSON:

{"foo" : [{"bar": false} ] }


Writing these generically using JSON transformers is basically a one-liner per operation:


Except that if the path contains an array, even if it's just selecting an element and navigating further into the document, the transformers break.

JsPath can be improved it's clear! We should certainly think about adding a few more features concerning Arrays as you're not the first one to ask for it.

Can you open an issue with test cases ?

Thanks a lot for using this API deeper and deeper ;)
 
Pascal

John Kodumal

unread,
Apr 3, 2013, 4:58:37 PM4/3/13
to play-fr...@googlegroups.com
Thanks Pascal. Filed as:


with a series of examples.

Pascal Voitot Dev

unread,
Apr 3, 2013, 5:06:40 PM4/3/13
to play-fr...@googlegroups.com
Cool Thanks!

Reply all
Reply to author
Forward
0 new messages