用Perl解析JavaScript对象数据
我正在寻找一种从Perl中解析真实JavaScript对象数据的方法(由于许多原因,它不符合JSON标准) 我发现,如果启用了用Perl解析JavaScript对象数据,javascript,json,perl,Javascript,Json,Perl,我正在寻找一种从Perl中解析真实JavaScript对象数据的方法(由于许多原因,它不符合JSON标准) 我发现,如果启用了allow_singlequote和allow_barekey选项,则该模块的工作相当不错,但我仍然无法解析包含转义单引号和未转义双引号的单引号值。比如说, { label : 'can\'t process' } 及 扔 及 分别,因为模块只需要转义标准字符集,而不考虑包含引号的字符集 我以为我已经找到了可以在模块中工作的东西,但它自2010年以来就没有更新过,我无法
allow_singlequote
和allow_barekey
选项,则该模块的工作相当不错,但我仍然无法解析包含转义单引号和未转义双引号的单引号值。比如说,
{ label : 'can\'t process' }
及
扔
及
分别,因为模块只需要转义标准字符集,而不考虑包含引号的字符集
我以为我已经找到了可以在模块中工作的东西,但它自2010年以来就没有更新过,我无法安装它
到目前为止,我唯一的答案是使用安装一个完整的JavaScript引擎,并将字符串作为JavaScript代码运行。这很好,但远不是直接的,而且对于我想要的东西来说是非常过分的
有人对我可以尝试的替代方案有什么建议吗?CPAN上有很多JSON模块,其中很多都有容错模式。然而,似乎没有人能够处理这种特殊情况。(我认为有可能,但遗憾的是没有。不过,我想如果你报道这个案件,作者可能会很想让它奏效。) 我的快速而肮脏的建议是使用现有的纯PerlJSON模块,并对其进行破解以使其正常工作。他是个很好的候选人。CPAN上最新JSON::Tiny发行版的以下补丁似乎可以解决您提供的两个简短示例:
--- Tiny.orig 2014-02-22 22:17:50.923272286 +0000
+++ Tiny.pm 2014-02-22 22:18:23.847435546 +0000
@@ -160,11 +160,13 @@
until (m/\G$WHITESPACE_RE\}/gc) {
# Quote
- m/\G$WHITESPACE_RE"/gc
+ m/\G$WHITESPACE_RE(["']|[^\W0-9]\w+)/gc
or _exception('Expected string while parsing object');
# Key
- my $key = _decode_string();
+ my $key = ($1 =~ /['"]/)
+ ? _decode_string()
+ : $1;
# Colon
m/\G$WHITESPACE_RE:/gc
@@ -187,13 +189,14 @@
}
sub _decode_string {
+ my $quote = shift;
my $pos = pos;
# Extract string with escaped characters
- m!\G((?:(?:[^\x00-\x1f\\"]|\\(?:["\\/bfnrt]|u[0-9a-fA-F]{4})){0,32766})*)!gc; # segfault on 5.8.x in t/20-mojo-json.t #83
+ m!\G((?:(?:[^\x00-\x1f\\$quote]|\\(?:[$quote\\/bfnrt]|u[0-9a-fA-F]{4})){0,32766})*)!gc; # segfault on 5.8.x in t/20-mojo-json.t #83
my $str = $1;
# Invalid character
- unless (m/\G"/gc) {
+ unless (m/\G$quote/gc) {
_exception('Unexpected character or invalid escape while parsing string')
if m/\G[\x00-\x1f\\]/;
_exception('Unterminated string');
@@ -247,7 +250,8 @@
m/\G$WHITESPACE_RE/gc;
# String
- return _decode_string() if m/\G"/gc;
+ return _decode_string(q["]) if m/\G"/gc;
+ return _decode_string(q[']) if m/\G'/gc;
# Array
return _decode_array() if m/\G\[/gc;
@@ -268,6 +272,9 @@
# Null
return undef if m/\Gnull/gc; ## no critic (return)
+ # Bareword string
+ return $1 if m/\G([^\W0-9]\w+)/gc;
+
# Invalid character
_exception('Expected string, array, object, number, boolean or null');
}
illegal backslash escape sequence in string
invalid character encountered while parsing JSON string
--- Tiny.orig 2014-02-22 22:17:50.923272286 +0000
+++ Tiny.pm 2014-02-22 22:18:23.847435546 +0000
@@ -160,11 +160,13 @@
until (m/\G$WHITESPACE_RE\}/gc) {
# Quote
- m/\G$WHITESPACE_RE"/gc
+ m/\G$WHITESPACE_RE(["']|[^\W0-9]\w+)/gc
or _exception('Expected string while parsing object');
# Key
- my $key = _decode_string();
+ my $key = ($1 =~ /['"]/)
+ ? _decode_string()
+ : $1;
# Colon
m/\G$WHITESPACE_RE:/gc
@@ -187,13 +189,14 @@
}
sub _decode_string {
+ my $quote = shift;
my $pos = pos;
# Extract string with escaped characters
- m!\G((?:(?:[^\x00-\x1f\\"]|\\(?:["\\/bfnrt]|u[0-9a-fA-F]{4})){0,32766})*)!gc; # segfault on 5.8.x in t/20-mojo-json.t #83
+ m!\G((?:(?:[^\x00-\x1f\\$quote]|\\(?:[$quote\\/bfnrt]|u[0-9a-fA-F]{4})){0,32766})*)!gc; # segfault on 5.8.x in t/20-mojo-json.t #83
my $str = $1;
# Invalid character
- unless (m/\G"/gc) {
+ unless (m/\G$quote/gc) {
_exception('Unexpected character or invalid escape while parsing string')
if m/\G[\x00-\x1f\\]/;
_exception('Unterminated string');
@@ -247,7 +250,8 @@
m/\G$WHITESPACE_RE/gc;
# String
- return _decode_string() if m/\G"/gc;
+ return _decode_string(q["]) if m/\G"/gc;
+ return _decode_string(q[']) if m/\G'/gc;
# Array
return _decode_array() if m/\G\[/gc;
@@ -268,6 +272,9 @@
# Null
return undef if m/\Gnull/gc; ## no critic (return)
+ # Bareword string
+ return $1 if m/\G([^\W0-9]\w+)/gc;
+
# Invalid character
_exception('Expected string, array, object, number, boolean or null');
}