Skip to content

Commit

Permalink
simplify whitespace handling in Mojo::JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Jul 14, 2014
1 parent 6ccc467 commit 710e8c7
Showing 1 changed file with 11 additions and 13 deletions.
24 changes: 11 additions & 13 deletions lib/Mojo/JSON.pm
Expand Up @@ -31,8 +31,6 @@ my %ESCAPE = (
my %REVERSE = map { $ESCAPE{$_} => "\\$_" } keys %ESCAPE;
for (0x00 .. 0x1f) { $REVERSE{pack 'C', $_} //= sprintf '\u%.4X', $_ }

my $WHITESPACE_RE = qr/[\x20\x09\x0a\x0d]*/;

sub decode {
my $self = shift->error(undef);
my $value;
Expand Down Expand Up @@ -77,23 +75,23 @@ sub _decode {
my $value = _decode_value();

# Leftover data
_exception('Unexpected data') unless m/\G$WHITESPACE_RE\z/gco;
_exception('Unexpected data') unless m/\G[\x20\x09\x0a\x0d]*\z/gc;

return $value;
}

sub _decode_array {
my @array;
until (m/\G$WHITESPACE_RE\]/gco) {
until (m/\G[\x20\x09\x0a\x0d]*\]/gc) {

# Value
push @array, _decode_value();

# Separator
redo if m/\G$WHITESPACE_RE,/gco;
redo if m/\G[\x20\x09\x0a\x0d]*,/gc;

# End
last if m/\G$WHITESPACE_RE\]/gco;
last if m/\G[\x20\x09\x0a\x0d]*\]/gc;

# Invalid character
_exception('Expected comma or right square bracket while parsing array');
Expand All @@ -104,27 +102,27 @@ sub _decode_array {

sub _decode_object {
my %hash;
until (m/\G$WHITESPACE_RE\}/gco) {
until (m/\G[\x20\x09\x0a\x0d]*\}/gc) {

# Quote
m/\G$WHITESPACE_RE"/gco
m/\G[\x20\x09\x0a\x0d]*"/gc
or _exception('Expected string while parsing object');

# Key
my $key = _decode_string();

# Colon
m/\G$WHITESPACE_RE:/gco
m/\G[\x20\x09\x0a\x0d]*:/gc
or _exception('Expected colon while parsing object');

# Value
$hash{$key} = _decode_value();

# Separator
redo if m/\G$WHITESPACE_RE,/gco;
redo if m/\G[\x20\x09\x0a\x0d]*,/gc;

# End
last if m/\G$WHITESPACE_RE\}/gco;
last if m/\G[\x20\x09\x0a\x0d]*\}/gc;

# Invalid character
_exception('Expected comma or right curly bracket while parsing object');
Expand Down Expand Up @@ -191,7 +189,7 @@ sub _decode_string {
sub _decode_value {

# Leading whitespace
m/\G$WHITESPACE_RE/gco;
m/\G[\x20\x09\x0a\x0d]*/gc;

# String
return _decode_string() if m/\G"/gc;
Expand Down Expand Up @@ -275,7 +273,7 @@ sub _encode_value {
sub _exception {

# Leading whitespace
m/\G$WHITESPACE_RE/gco;
m/\G[\x20\x09\x0a\x0d]*/gc;

# Context
my $context = 'Malformed JSON: ' . shift;
Expand Down

0 comments on commit 710e8c7

Please sign in to comment.