Skip to content

Commit

Permalink
fixed null decoding error and improved Mojo::JSON tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Mar 4, 2014
1 parent d73ea17 commit 3ea1eb3
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 107 deletions.
3 changes: 2 additions & 1 deletion lib/Mojo/JSON.pm
Expand Up @@ -42,7 +42,8 @@ sub decode {
}

sub decode_json {
eval { _decode(shift) } // croak _chomp($@);
my $value;
return eval { $value = _decode(shift); 1 } ? $value : croak _chomp($@);
}

sub encode { encode_json($_[1]) }
Expand Down
210 changes: 107 additions & 103 deletions t/mojo/json.t
Expand Up @@ -14,90 +14,89 @@ use Mojo::JSON qw(decode_json encode_json j);
use Mojo::Util 'encode';

# Decode array
my $json = Mojo::JSON->new;
my $array = $json->decode('[]');
my $array = decode_json '[]';
is_deeply $array, [], 'decode []';
$array = $json->decode('[ [ ]]');
$array = decode_json '[ [ ]]';
is_deeply $array, [[]], 'decode [ [ ]]';

# Decode number
$array = $json->decode('[0]');
$array = decode_json '[0]';
is_deeply $array, [0], 'decode [0]';
$array = $json->decode('[1]');
$array = decode_json '[1]';
is_deeply $array, [1], 'decode [1]';
$array = $json->decode('[ "-122.026020" ]');
$array = decode_json '[ "-122.026020" ]';
is_deeply $array, ['-122.026020'], 'decode [ -122.026020 ]';
$array = $json->decode('[ -122.026020 ]');
$array = decode_json '[ -122.026020 ]';
is_deeply $array, ['-122.02602'], 'decode [ -122.026020 ]';
$array = $json->decode('[0.0]');
$array = decode_json '[0.0]';
cmp_ok $array->[0], '==', 0, 'value is 0';
$array = $json->decode('[0e0]');
$array = decode_json '[0e0]';
cmp_ok $array->[0], '==', 0, 'value is 0';
$array = $json->decode('[1,-2]');
$array = decode_json '[1,-2]';
is_deeply $array, [1, -2], 'decode [1,-2]';
$array = $json->decode('["10e12" , [2 ]]');
$array = decode_json '["10e12" , [2 ]]';
is_deeply $array, ['10e12', [2]], 'decode ["10e12" , [2 ]]';
$array = $json->decode('[10e12 , [2 ]]');
$array = decode_json '[10e12 , [2 ]]';
is_deeply $array, [10000000000000, [2]], 'decode [10e12 , [2 ]]';
$array = $json->decode('[37.7668 , [ 20 ]] ');
$array = decode_json '[37.7668 , [ 20 ]] ';
is_deeply $array, [37.7668, [20]], 'decode [37.7668 , [ 20 ]] ';
$array = $json->decode('[1e3]');
$array = decode_json '[1e3]';
cmp_ok $array->[0], '==', 1e3, 'value is 1e3';
my $value = $json->decode('0');
my $value = decode_json '0';
cmp_ok $value, '==', 0, 'decode 0';
$value = $json->decode('23.3');
$value = decode_json '23.3';
cmp_ok $value, '==', 23.3, 'decode 23.3';

# Decode name
$array = $json->decode('[true]');
$array = decode_json '[true]';
is_deeply $array, [Mojo::JSON->true], 'decode [true]';
$array = $json->decode('[null]');
$array = decode_json '[null]';
is_deeply $array, [undef], 'decode [null]';
$array = $json->decode('[true, false]');
$array = decode_json '[true, false]';
is_deeply $array, [Mojo::JSON->true, Mojo::JSON->false],
'decode [true, false]';
$value = $json->decode('true');
$value = decode_json 'true';
is $value, Mojo::JSON->true, 'decode true';
$value = $json->decode('false');
$value = decode_json 'false';
is $value, Mojo::JSON->false, 'decode false';
$value = $json->decode('null');
$value = decode_json 'null';
is $value, undef, 'decode null';

# Decode string
$array = $json->decode('[" "]');
$array = decode_json '[" "]';
is_deeply $array, [' '], 'decode [" "]';
$array = $json->decode('["hello world!"]');
$array = decode_json '["hello world!"]';
is_deeply $array, ['hello world!'], 'decode ["hello world!"]';
$array = $json->decode('["hello\nworld!"]');
$array = decode_json '["hello\nworld!"]';
is_deeply $array, ["hello\nworld!"], 'decode ["hello\nworld!"]';
$array = $json->decode('["hello\t\"world!"]');
$array = decode_json '["hello\t\"world!"]';
is_deeply $array, ["hello\t\"world!"], 'decode ["hello\t\"world!"]';
$array = $json->decode('["hello\u0152world\u0152!"]');
$array = decode_json '["hello\u0152world\u0152!"]';
is_deeply $array, ["hello\x{0152}world\x{0152}!"],
'decode ["hello\u0152world\u0152!"]';
$array = $json->decode('["0."]');
$array = decode_json '["0."]';
is_deeply $array, ['0.'], 'decode ["0."]';
$array = $json->decode('[" 0"]');
$array = decode_json '[" 0"]';
is_deeply $array, [' 0'], 'decode [" 0"]';
$array = $json->decode('["1"]');
$array = decode_json '["1"]';
is_deeply $array, ['1'], 'decode ["1"]';
$array = $json->decode('["\u0007\b\/\f\r"]');
$array = decode_json '["\u0007\b\/\f\r"]';
is_deeply $array, ["\a\b/\f\r"], 'decode ["\u0007\b\/\f\r"]';
$value = $json->decode('""');
$value = decode_json '""';
is $value, '', 'decode ""';
$value = $json->decode('"hell\no"');
$value = decode_json '"hell\no"';
is $value, "hell\no", 'decode "hell\no"';

# Decode object
my $hash = $json->decode('{}');
my $hash = decode_json '{}';
is_deeply $hash, {}, 'decode {}';
$hash = $json->decode('{"foo": "bar"}');
$hash = decode_json '{"foo": "bar"}';
is_deeply $hash, {foo => 'bar'}, 'decode {"foo": "bar"}';
$hash = $json->decode('{"foo": [23, "bar"]}');
$hash = decode_json '{"foo": [23, "bar"]}';
is_deeply $hash, {foo => [qw(23 bar)]}, 'decode {"foo": [23, "bar"]}';

# Decode full spec example
$hash = $json->decode(<<EOF);
$hash = decode_json <<EOF;
{
"Image": {
"Width": 800,
Expand Down Expand Up @@ -125,81 +124,81 @@ is $hash->{Image}{IDs}[2], 234, 'right value';
is $hash->{Image}{IDs}[3], 38793, 'right value';

# Encode array
my $bytes = $json->encode([]);
my $bytes = encode_json [];
is $bytes, '[]', 'encode []';
$bytes = $json->encode([[]]);
$bytes = encode_json [[]];
is $bytes, '[[]]', 'encode [[]]';
$bytes = $json->encode([[], []]);
$bytes = encode_json [[], []];
is $bytes, '[[],[]]', 'encode [[], []]';
$bytes = $json->encode([[], [[]], []]);
$bytes = encode_json [[], [[]], []];
is $bytes, '[[],[[]],[]]', 'encode [[], [[]], []]';

# Encode string
$bytes = $json->encode(['foo']);
$bytes = encode_json ['foo'];
is $bytes, '["foo"]', 'encode [\'foo\']';
$bytes = $json->encode(["hello\nworld!"]);
$bytes = encode_json ["hello\nworld!"];
is $bytes, '["hello\nworld!"]', 'encode ["hello\nworld!"]';
$bytes = $json->encode(["hello\t\"world!"]);
$bytes = encode_json ["hello\t\"world!"];
is $bytes, '["hello\t\"world!"]', 'encode ["hello\t\"world!"]';
$bytes = $json->encode(["hello\x{0003}\x{0152}world\x{0152}!"]);
$bytes = encode_json ["hello\x{0003}\x{0152}world\x{0152}!"];
is b($bytes)->decode('UTF-8'), "[\"hello\\u0003\x{0152}world\x{0152}!\"]",
'encode ["hello\x{0003}\x{0152}world\x{0152}!"]';
$bytes = $json->encode(["123abc"]);
$bytes = encode_json ["123abc"];
is $bytes, '["123abc"]', 'encode ["123abc"]';
$bytes = $json->encode(["\x00\x1f \a\b/\f\r"]);
$bytes = encode_json ["\x00\x1f \a\b/\f\r"];
is $bytes, '["\\u0000\\u001F \\u0007\\b\/\f\r"]',
'encode ["\x00\x1f \a\b/\f\r"]';
$bytes = $json->encode('');
$bytes = encode_json '';
is $bytes, '""', 'encode ""';
$bytes = $json->encode("hell\no");
$bytes = encode_json "hell\no";
is $bytes, '"hell\no"', 'encode "hell\no"';

# Encode object
$bytes = $json->encode({});
$bytes = encode_json {};
is $bytes, '{}', 'encode {}';
$bytes = $json->encode({foo => {}});
$bytes = encode_json {foo => {}};
is $bytes, '{"foo":{}}', 'encode {foo => {}}';
$bytes = $json->encode({foo => 'bar'});
$bytes = encode_json {foo => 'bar'};
is $bytes, '{"foo":"bar"}', 'encode {foo => \'bar\'}';
$bytes = $json->encode({foo => []});
$bytes = encode_json {foo => []};
is $bytes, '{"foo":[]}', 'encode {foo => []}';
$bytes = $json->encode({foo => ['bar']});
$bytes = encode_json {foo => ['bar']};
is $bytes, '{"foo":["bar"]}', 'encode {foo => [\'bar\']}';

# Encode name
$bytes = $json->encode([Mojo::JSON->true]);
$bytes = encode_json [Mojo::JSON->true];
is $bytes, '[true]', 'encode [Mojo::JSON->true]';
$bytes = $json->encode([undef]);
$bytes = encode_json [undef];
is $bytes, '[null]', 'encode [undef]';
$bytes = $json->encode([Mojo::JSON->true, Mojo::JSON->false]);
$bytes = encode_json [Mojo::JSON->true, Mojo::JSON->false];
is $bytes, '[true,false]', 'encode [Mojo::JSON->true, Mojo::JSON->false]';
$bytes = $json->encode(Mojo::JSON->true);
$bytes = encode_json(Mojo::JSON->true);
is $bytes, 'true', 'encode Mojo::JSON->true';
$bytes = $json->encode(Mojo::JSON->false);
$bytes = encode_json(Mojo::JSON->false);
is $bytes, 'false', 'encode Mojo::JSON->false';
$bytes = $json->encode(undef);
$bytes = encode_json undef;
is $bytes, 'null', 'encode undef';

# Encode number
$bytes = $json->encode([1]);
$bytes = encode_json [1];
is $bytes, '[1]', 'encode [1]';
$bytes = $json->encode(["1"]);
$bytes = encode_json ["1"];
is $bytes, '["1"]', 'encode ["1"]';
$bytes = $json->encode(['-122.026020']);
$bytes = encode_json ['-122.026020'];
is $bytes, '["-122.026020"]', 'encode [\'-122.026020\']';
$bytes = $json->encode([-122.026020]);
$bytes = encode_json [-122.026020];
is $bytes, '[-122.02602]', 'encode [-122.026020]';
$bytes = $json->encode([1, -2]);
$bytes = encode_json [1, -2];
is $bytes, '[1,-2]', 'encode [1, -2]';
$bytes = $json->encode(['10e12', [2]]);
$bytes = encode_json ['10e12', [2]];
is $bytes, '["10e12",[2]]', 'encode [\'10e12\', [2]]';
$bytes = $json->encode([10e12, [2]]);
$bytes = encode_json [10e12, [2]];
is $bytes, '[10000000000000,[2]]', 'encode [10e12, [2]]';
$bytes = $json->encode([37.7668, [20]]);
$bytes = encode_json [37.7668, [20]];
is $bytes, '[37.7668,[20]]', 'encode [37.7668, [20]]';
$bytes = $json->encode(0);
$bytes = encode_json 0;
is $bytes, '0', 'encode 0';
$bytes = $json->encode(23.3);
$bytes = encode_json 23.3;
is $bytes, '23.3', 'encode 23.3';

# Faihu roundtrip
Expand All @@ -209,71 +208,70 @@ $array = j($bytes);
is_deeply $array, ["\x{10346}"], 'successful roundtrip';

# Decode faihu surrogate pair
$array = $json->decode('["\\ud800\\udf46"]');
$array = decode_json '["\\ud800\\udf46"]';
is_deeply $array, ["\x{10346}"], 'decode [\"\\ud800\\udf46\"]';

# Decode object with duplicate keys
$hash = $json->decode('{"foo": 1, "foo": 2}');
$hash = decode_json '{"foo": 1, "foo": 2}';
is_deeply $hash, {foo => 2}, 'decode {"foo": 1, "foo": 2}';

# Complicated roudtrips
$bytes = '{"":""}';
$hash = $json->decode($bytes);
$hash = decode_json $bytes;
is_deeply $hash, {'' => ''}, 'decode {"":""}';
is $json->encode($hash), $bytes, 're-encode';
is encode_json($hash), $bytes, 're-encode';
$bytes = '[null,false,true,"",0,1]';
$array = $json->decode($bytes);
$array = decode_json $bytes;
is_deeply $array, [undef, Mojo::JSON->false, Mojo::JSON->true, '', 0, 1],
'decode [null,false,true,"",0,1]';
is $json->encode($array), $bytes, 're-encode';
is encode_json($array), $bytes, 're-encode';
$array = [undef, 0, 1, '', Mojo::JSON->true, Mojo::JSON->false];
$bytes = $json->encode($array);
$bytes = encode_json($array);
ok $bytes, 'defined value';
is_deeply $json->decode($bytes), $array, 'successful roundtrip';
is_deeply decode_json($bytes), $array, 'successful roundtrip';

# Real world roundtrip
$bytes = encode_json({foo => 'c:\progra~1\mozill~1\firefox.exe'});
is $bytes, '{"foo":"c:\\\\progra~1\\\\mozill~1\\\\firefox.exe"}',
'encode {foo => \'c:\progra~1\mozill~1\firefox.exe\'}';
$hash = decode_json($bytes);
$hash = decode_json $bytes;
is_deeply $hash, {foo => 'c:\progra~1\mozill~1\firefox.exe'},
'successful roundtrip';

# Huge string
$bytes = $json->encode(['a' x 32768]);
is_deeply $json->decode($bytes), ['a' x 32768], 'successful roundtrip';
is $json->error, undef, 'no error';
$bytes = encode_json(['a' x 32768]);
is_deeply decode_json($bytes), ['a' x 32768], 'successful roundtrip';

# u2028 and u2029
$bytes = $json->encode(["\x{2028}test\x{2029}123"]);
$bytes = encode_json ["\x{2028}test\x{2029}123"];
is index($bytes, b("\x{2028}")->encode), -1, 'properly escaped';
is index($bytes, b("\x{2029}")->encode), -1, 'properly escaped';
is_deeply $json->decode($bytes), ["\x{2028}test\x{2029}123"],
is_deeply decode_json($bytes), ["\x{2028}test\x{2029}123"],
'successful roundtrip';

# Blessed reference
$bytes = $json->encode([b('test')]);
is_deeply $json->decode($bytes), ['test'], 'successful roundtrip';
$bytes = encode_json [b('test')];
is_deeply decode_json($bytes), ['test'], 'successful roundtrip';

# Blessed reference with TO_JSON method
$bytes = $json->encode(JSONTest->new);
is_deeply $json->decode($bytes), {}, 'successful roundtrip';
$bytes = $json->encode(
$bytes = encode_json(JSONTest->new);
is_deeply decode_json($bytes), {}, 'successful roundtrip';
$bytes = encode_json(
JSONTest->new(something => {just => 'works'}, else => {not => 'working'}));
is_deeply $json->decode($bytes), {just => 'works'}, 'successful roundtrip';
is_deeply decode_json($bytes), {just => 'works'}, 'successful roundtrip';

# Boolean shortcut
is $json->encode({true => \1}), '{"true":true}', 'encode {true => \1}';
is $json->encode({false => \0}), '{"false":false}', 'encode {false => \0}';
is encode_json({true => \1}), '{"true":true}', 'encode {true => \1}';
is encode_json({false => \0}), '{"false":false}', 'encode {false => \0}';
$bytes = 'some true value';
is $json->encode({true => \!!$bytes}), '{"true":true}',
is encode_json({true => \!!$bytes}), '{"true":true}',
'encode true boolean from double negated reference';
is $json->encode({true => \$bytes}), '{"true":true}',
is encode_json({true => \$bytes}), '{"true":true}',
'encode true boolean from reference';
$bytes = '';
is $json->encode({false => \!!$bytes}), '{"false":false}',
is encode_json({false => \!!$bytes}), '{"false":false}',
'encode false boolean from double negated reference';
is $json->encode({false => \$bytes}), '{"false":false}',
is encode_json({false => \$bytes}), '{"false":false}',
'encode false boolean from reference';

# Stringify booleans
Expand All @@ -283,30 +281,36 @@ is(Mojo::JSON->false, 0, 'right value');
# Upgraded numbers
my $num = 3;
my $str = "$num";
is $json->encode({test => [$num, $str]}), '{"test":[3,"3"]}',
is encode_json({test => [$num, $str]}), '{"test":[3,"3"]}',
'upgraded number detected';
$num = 3.21;
$str = "$num";
is $json->encode({test => [$num, $str]}), '{"test":[3.21,"3.21"]}',
is encode_json({test => [$num, $str]}), '{"test":[3.21,"3.21"]}',
'upgraded number detected';
$str = '0 but true';
$num = 1 + $str;
is $json->encode({test => [$num, $str]}), '{"test":[1,0]}',
is encode_json({test => [$num, $str]}), '{"test":[1,0]}',
'upgraded number detected';

# Ensure numbers and strings are not upgraded
my $mixed = [3, 'three', '3', 0, "0"];
is $json->encode($mixed), '[3,"three","3",0,"0"]',
is encode_json($mixed), '[3,"three","3",0,"0"]',
'all have been detected correctly';
is $json->encode($mixed), '[3,"three","3",0,"0"]',
is encode_json($mixed), '[3,"three","3",0,"0"]',
'all have been detected correctly again';

# "inf" and "nan"
like $json->encode({test => 9**9**9}), qr/^{"test":".*"}$/,
like encode_json({test => 9**9**9}), qr/^{"test":".*"}$/,
'encode "inf" as string';
like $json->encode({test => -sin(9**9**9)}), qr/^{"test":".*"}$/,
like encode_json({test => -sin(9**9**9)}), qr/^{"test":".*"}$/,
'encode "nan" as string';

# "null"
my $json = Mojo::JSON->new;
is $json->decode('null'), undef, 'decode null';
ok !$json->error, 'no error';
is j('null'), undef, 'decode null';

# Errors
is $json->decode('test'), undef, 'syntax error';
is $json->error, 'Malformed JSON: Expected string, array, object, number,'
Expand Down

0 comments on commit 3ea1eb3

Please sign in to comment.