Mojo-JSON-Any

 view release on metacpan or  search on metacpan

t/json.t  view on Meta::CPAN

is($string, '["foo"]', 'encode [\'foo\']');
$string = $json->encode(["hello\nworld!"]);
is($string, '["hello\nworld!"]', 'encode ["hello\nworld!"]');
$string = $json->encode(["hello\t\"world!"]);
is($string, '["hello\t\"world!"]', 'encode ["hello\t\"world!"]');
$string = $json->encode(["hello\x{0003}\x{0152}world\x{0152}!"]);
is( b($string)->decode('UTF-8'),
    "[\"hello\\u0003\x{0152}world\x{0152}!\"]",
    'encode ["hello\x{0003}\x{0152}world\x{0152}!"]'
);
$string = $json->encode(["123abc"]);
is($string, '["123abc"]', 'encode ["123abc"]');

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

# Encode name
$string = $json->encode([$json->true]);
is($string, '[true]', 'encode [$json->true]');
$string = $json->encode([undef]);
is($string, '[null]', 'encode [undef]');
$string = $json->encode([$json->true, $json->false]);
is($string, '[true,false]', 'encode [$json->true, $json->false]');

# Encode number
$string = $json->encode([1]);
is($string, '[1]', 'encode [1]');
#$string = $json->encode(['-122.026020']);
#is($string, '[-122.026020]', 'encode [\'-122.026020\']');
#$string = $json->encode([1, -2]);
#is($string, '[1,-2]', 'encode [1, -2]');
#$string = $json->encode(['10e12', [2]]);
#is($string, '[10e12,[2]]', 'encode [\'10e12\', [2]]');
$string = $json->encode([37.7668, [20]]);
is($string, '[37.7668,[20]]', 'encode [37.7668, [20]]');

## Faihu roundtrip
#$string = $json->encode(["\x{10346}"]);
#is(b($string)->decode('UTF-8'), "[\"\x{10346}\"]", 'encode ["\x{10346}"]');
#$array = $json->decode($string);
#is_deeply($array, ["\x{10346}"], 'successful roundtrip');

# Decode UTF-16LE
$array = $json->decode(b("\x{feff}[true]")->encode('UTF-16LE'));
#is_deeply($array, [$json->true], 'decode \x{feff}[true]');
is($array->[0], $json->true);

# Decode UTF-16LE with faihu surrogate pair
$array = $json->decode(b("\x{feff}[\"\\ud800\\udf46\"]")->encode('UTF-16LE'));
is_deeply($array, ["\x{10346}"], 'decode \x{feff}[\"\\ud800\\udf46\"]');

# Decode UTF-16LE with faihu surrogate pair and BOM value
$array = $json->decode(
    b("\x{feff}[\"\\ud800\\udf46\x{feff}\"]")->encode('UTF-16LE'));
is_deeply($array, ["\x{10346}\x{feff}"],
    'decode \x{feff}[\"\\ud800\\udf46\x{feff}\"]');

## Decode UTF-16LE with missing high surrogate
#$array = $json->decode(b("\x{feff}[\"\\ud800\"]")->encode('UTF-16LE'));
#is_deeply($array, ['\ud800'], 'decode \x{feff}[\"\\ud800\"]');

## Decode UTF-16LE with missing low surrogate
#$array = $json->decode(b("\x{feff}[\"\\udf46\"]")->encode('UTF-16LE'));
#is_deeply($array, ['\udf46'], 'decode \x{feff}[\"\\udf46\"]');
#
## Decode UTF-16BE with faihu surrogate pair
#$array = $json->decode(b("\x{feff}[\"\\ud800\\udf46\"]")->encode('UTF-16BE'));
#is_deeply($array, ["\x{10346}"], 'decode \x{feff}[\"\\ud800\\udf46\"]');

# Decode UTF-32LE
$array = $json->decode(b("\x{feff}[true]")->encode('UTF-32LE'));
#is_deeply($array, [$json->true], 'decode \x{feff}[true]');
is($array->[0], $json->true);

# Decode UTF-32BE
$array = $json->decode(b("\x{feff}[true]")->encode('UTF-32BE'));
#is_deeply($array, [$json->true], 'decode \x{feff}[true]');
is($array->[0], $json->true);

# Decode UTF-16LE without BOM
$array = $json->decode(b("[\"\\ud800\\udf46\"]")->encode('UTF-16LE'));
is_deeply($array, ["\x{10346}"], 'decode [\"\\ud800\\udf46\"]');

# Decode UTF-16BE without BOM
$array = $json->decode(b("[\"\\ud800\\udf46\"]")->encode('UTF-16BE'));
is_deeply($array, ["\x{10346}"], 'decode [\"\\ud800\\udf46\"]');

# Decode UTF-32LE without BOM
$array = $json->decode(b("[\"\\ud800\\udf46\"]")->encode('UTF-32LE'));
is_deeply($array, ["\x{10346}"], 'decode [\"\\ud800\\udf46\"]');

# Decode UTF-32BE without BOM
$array = $json->decode(b("[\"\\ud800\\udf46\"]")->encode('UTF-32BE'));
is_deeply($array, ["\x{10346}"], 'decode [\"\\ud800\\udf46\"]');

# Complicated roudtrips
$string = '[null,false,true,"",0,1]';
$array  = $json->decode($string);
isa_ok($array, 'ARRAY', 'decode [null,false,true,"",0,1]');
is($json->encode($array), $string, 'reencode');
$array = [undef, 0, 1, '', $json->true, $json->false];
$string = $json->encode($array);
ok($string, 'defined value');
#is_deeply($json->decode($string), $array, 'successful roundtrip');
my $result = $json->decode($string);
ok(not defined $result->[0]);
is($result->[1], 0);
is($result->[2], 1);
is($result->[3], '');

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

# Errors
is($json->decode('[[]'), undef, 'missing right square bracket');
#is($json->error, 'Missing right square bracket near end of file.',
#    'right error');
is($json->decode('{{}'), undef, 'missing right curly bracket');
#is($json->error, 'Missing right curly bracket near end of file.',
#    'right error');
is($json->decode('[[]...'), undef,                        'syntax error');
#is($json->error,            'Syntax error near "...".',   'right error');
is($json->decode('{{}...'), undef,                        'syntax error');
#is($json->error,            'Syntax error near "...".',   'right error');
is($json->decode('[nan]'),  undef,                        'syntax error');
#is($json->error,            'Syntax error near "nan]".',  'right error');
is($json->decode('["foo]'), undef,                        'syntax error');
#is($json->error,            'Syntax error near ""foo]".', 'right error');
#is($json->decode('false'), undef, 'no object or array');
#is($json->error, 'JSON text has to be a serialized object or array.',
#    'right error');
is($json->decode(''), undef, 'no object or array');
#is($json->error, 'JSON text has to be a serialized object or array.',
#    'right error');



( run in 2.338 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )