I have a array of objects some of whom have cyclic references in them. So I used JSON.decycle upon sending the object via JSON and JSON.retrocycle on the other end.
Something like this:
var refactor_data = JSON.retrocycle(JSON.parse(event.data));
The problem is that some of the ojects in ‘refactor_data’ have the JSONPath references transformed while other don’t and I can’t figure out why.
The objects are fairly large but if needed I’ll try to provide a sample.
EDIT:
Here is a sample of an OK object: http://pastebin.com/1hZDCipn
And here is a sample of an broken object: http://pastebin.com/PfYCkrGt
EDIT2:
I think the ones that have the references ‘replaced’ are actually the originals and retrocycle actually doesn’t do anything on any of them. Could this be because they are too ‘deep’ within the object structure ?
EDIT3: I’ve tried to run in FireBug the eval that retrocycle should run: (I think it’s normal for this not to work)
EDIT4: I’ve added a console.log within the JSON.retrocycle function and eval() return the correct object but the returned JSON and original JSON are unaltered.
Thanks.
The JSON-js cycle.js retrocycle function does not expect a path to have an array index greater than 9.
One example path that is not being retrocycled is:
A similar path that does get retrocycled is:
If you look at the cycle.js code, you will see that in order for a path to be retrocycled, it must validate against a regular expression.
In the px regular expression, only one digit array indexes are allowed, not two.
Since the path does not match the pattern, it will not replace the reference.
You could try using a local copy of cycle.js and changing the px regular expression to:
Note the difference from the previous regular expression: we are saying that we will now allow one or more digits in array indices. This is accomplished by replacing the single ? character after the first d with a +.
Edit: Newer versions of the cycle.js retrocycle function now allow array indices that are greater than 9.