Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
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...
commit a7910f1ee833aa41468c5c705da3e780950fb41f 1 parent a0a5335
@moritz authored
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
2  lib/JSON/Tiny.pm
@@ -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
View
2  lib/JSON/Tiny/Grammar.pm
@@ -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
3  t/01-parse.t
@@ -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++;
}
Please sign in to comment.
Something went wrong with that request. Please try again.