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.
我有一个对象数组其中有些对象具有循环引用。因此,在通过JSON和JSON发送对象时,我使用了JSON.decycle。另一端是逆行。
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.
问题是“refactor_data”中的一些对象已经转换了JSONPath引用,而另一些则没有,我不知道为什么。
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
编辑:这是一个OK对象的示例:http://pastebin.com/1hZDCipn,这是一个破碎对象的示例: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 ?
EDIT2:我认为那些被‘替换’的参考文献实际上是原始文献,而逆行器实际上对它们没有任何作用。这可能是因为它们在对象结构中“太深”吗?
EDIT3: I've tried to run in FireBug the eval that retrocycle should run:
(I think it's normal for this not to work)
我试过在FireBug中运行逆向循环应该运行的eval:
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.
我添加了一个控制台。日志中的JSON。retrocycle函数和eval()返回正确的对象,但是返回的JSON和原始JSON是不变的。
Thanks.
谢谢。
1 个解决方案
#1
3
The JSON-js cycle.js retrocycle function does not expect a path to have an array index greater than 9.
JSON-js周期。js反周期函数不期望路径的数组索引大于9。
One example path that is not being retrocycled is:
不进行逆向循环的一个例子是:
$[11]["LegList"][0]["ItenaryList"][0]["Ar"]
A similar path that does get retrocycled is:
类似的方法也可以进行逆向循环:
$[9]["LegList"][0]["ItenaryList"][0]["Ar"]
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.
看看这个循环。您将看到,要对路径进行反向循环,必须对正则表达式进行验证。
// [...] A PATH is expected to be
// reasonably short. A PATH is allowed to belong to a very restricted subset of
// Goessner's JSONPath.
// So,
// var s = '[{"$ref":"$"}]';
// return JSON.retrocycle(JSON.parse(s));
// produces an array containing a single element which is the array itself.
var px =
/^\$(?:\[(?:\d?|\"(?:[^\\\"\u0000-\u001f]|\\([\\\"\/bfnrt]|u[0-9a-zA-Z]{4}))*\")\])*$/;
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.
在px正则表达式中,只允许一个数字数组索引,不允许两个。由于路径与模式不匹配,因此不会替换引用。
You could try using a local copy of cycle.js and changing the px regular expression to:
您可以尝试使用循环的本地副本。并将px正则表达式改为:
var px = /^\$(?:\[(?:\d+|\"(?:[^\\\"\u0000-\u001f]|\\([\\\"\/bfnrt]|u[0-9a-zA-Z]{4}))*\")\])*$/;
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 +.
注意与前面的正则表达式的区别:我们说现在允许数组索引中的一个或多个数字。这是通过替换单个来完成的吗?在第一个d后面加上a +的字符。
Edit: Newer versions of the cycle.js retrocycle function now allow array indices that are greater than 9.
编辑:周期的更新版本。js反周期函数现在允许数组索引大于9。
#1
3
The JSON-js cycle.js retrocycle function does not expect a path to have an array index greater than 9.
JSON-js周期。js反周期函数不期望路径的数组索引大于9。
One example path that is not being retrocycled is:
不进行逆向循环的一个例子是:
$[11]["LegList"][0]["ItenaryList"][0]["Ar"]
A similar path that does get retrocycled is:
类似的方法也可以进行逆向循环:
$[9]["LegList"][0]["ItenaryList"][0]["Ar"]
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.
看看这个循环。您将看到,要对路径进行反向循环,必须对正则表达式进行验证。
// [...] A PATH is expected to be
// reasonably short. A PATH is allowed to belong to a very restricted subset of
// Goessner's JSONPath.
// So,
// var s = '[{"$ref":"$"}]';
// return JSON.retrocycle(JSON.parse(s));
// produces an array containing a single element which is the array itself.
var px =
/^\$(?:\[(?:\d?|\"(?:[^\\\"\u0000-\u001f]|\\([\\\"\/bfnrt]|u[0-9a-zA-Z]{4}))*\")\])*$/;
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.
在px正则表达式中,只允许一个数字数组索引,不允许两个。由于路径与模式不匹配,因此不会替换引用。
You could try using a local copy of cycle.js and changing the px regular expression to:
您可以尝试使用循环的本地副本。并将px正则表达式改为:
var px = /^\$(?:\[(?:\d+|\"(?:[^\\\"\u0000-\u001f]|\\([\\\"\/bfnrt]|u[0-9a-zA-Z]{4}))*\")\])*$/;
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 +.
注意与前面的正则表达式的区别:我们说现在允许数组索引中的一个或多个数字。这是通过替换单个来完成的吗?在第一个d后面加上a +的字符。
Edit: Newer versions of the cycle.js retrocycle function now allow array indices that are greater than 9.
编辑:周期的更新版本。js反周期函数现在允许数组索引大于9。