File: 02-JSON-XS.t

package info (click to toggle)
libjson-any-perl 1.28-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 252 kB
  • sloc: perl: 2,701; makefile: 2
file content (70 lines) | stat: -rw-r--r-- 1,774 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!perl -T

use utf8;
use strict;
use Test::More;

eval "use JSON::Any qw(XS)";
if ($@) {
    plan skip_all => "JSON::XS not installed: $@";
}
else {
    plan tests => 24;
}

skip "JSON::XS not installed: $@", 1 if $@;
diag("Testing JSON::XS backend");

is( JSON::Any->encode({foo=>'bar'}), qq[{"foo":"bar"}] );

my ( $json, $js, $obj );
ok( $json = JSON::Any->new( allow_nonref => 1, utf8 => 1 ) );
is( $json->encode("ü"), qq["ü"] );

foreach my $bytes ( 0, 1 ) {
    foreach my $utf8_support ( 0, 1 ) {
        my $str = "ü";
        utf8::encode($str) if $bytes;
        ok( $json = JSON::Any->new( allow_nonref => 1, utf8 => $utf8_support ) );

        if ( $utf8_support ) { # only then do we make assumptions about the output json
            is( $json->encode($str), qq["$str"] );
        }

        is( $json->decode($json->encode($str)), $str, "round tripping" );
    }
}

ok( $json = JSON::Any->new( allow_nonref => 1, ascii => 1, utf8 => 1 ) );
is( $json->encode( chr 0x8000 ), '"\u8000"' );
ok(
    $json = JSON::Any->new(
        allow_nonref => 1,
        ascii        => 1,
        utf8         => 1,
        pretty       => 1
    )
);
like( $json->encode( chr 0x10402 ), qr/"\\ud801\\udc02"\n?/ );
ok( $json = JSON::Any->new( allow_nonref => 1, utf8 => 1 ) );

is( $json->encode("ü"), qq["\xc3\xbc\"] );

is( JSON::Any->encode({foo=>'bar'}), qq[{"foo":"bar"}] );

# testing the truth
$obj = { foo => JSON::Any->true };
$js = JSON::Any->objToJson($obj);
is($js,'{"foo":true}');

$obj = { foo => JSON::Any->false };
$js = JSON::Any->objToJson($obj);
is($js,'{"foo":false}');

$obj = { foo => $json->true };
$js = $json->objToJson($obj);
is($js,'{"foo":true}');

$obj = { foo => $json->false };
$js = $json->objToJson($obj);
is($js,'{"foo":false}');