Permalink
Browse files

properly fix parsing of JSON strings with leading whitespace

the reason not to do it with .trim is that eventually we might
want to report line numbers of the syntax error, and .trim will throw those off
  • Loading branch information...
1 parent a0a5335 commit a7910f1ee833aa41468c5c705da3e780950fb41f @moritz committed Oct 20, 2012
Showing with 4 additions and 3 deletions.
  1. +1 −1 lib/JSON/Tiny.pm
  2. +1 −1 lib/JSON/Tiny/Grammar.pm
  3. +2 −1 t/01-parse.t
View
@@ -47,7 +47,7 @@ multi to-json(Any:D $s) {
sub from-json($text) is export {
my $a = JSON::Tiny::Actions.new();
- my $o = JSON::Tiny::Grammar.parse($text.trim, :actions($a));
+ my $o = JSON::Tiny::Grammar.parse($text, :actions($a));
return $o.ast;
}
# vim: ft=perl6
@@ -1,7 +1,7 @@
use v6;
grammar JSON::Tiny::Grammar;
-rule TOP { ^ [ <object> | <array> ] $ }
+token TOP { ^ \s* [ <object> | <array> ] \s* $ }
rule object { '{' ~ '}' <pairlist> }
rule pairlist { <?> <pair> * % \, }
rule pair { <?> <string> ':' <value> }
View
@@ -7,6 +7,7 @@ use Test;
my @t =
'{}',
'{ }',
+ ' { } ',
'{ "a" : "b" }',
'{ "a" : null }',
'{ "a" : true }',
@@ -212,7 +213,7 @@ for @n -> $t {
$desc .= subst(/\n.*$/, "\\n...[$i]");
}
my $parsed = 0;
- try { JSON::Tiny::Grammar.parse($t) && $parsed = 1 };
+ try { JSON::Tiny::Grammar.parse($t) and $parsed = 1 };
nok $parsed, "NOT parsed «$desc»";
$i++;
}

0 comments on commit a7910f1

Please sign in to comment.