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 71 72 73 74 75 76 77 78 79 80 81 82 83
|
#!perl
$!++;
use strict;
use Data::Dumper;
use Test::More;
use Storable;
eval "use JSON::Any";
if ($@) {
plan skip_all => "$@";
exit;
}
$Data::Dumper::Indent = 0;
$Data::Dumper::Terse = 1;
my @round_trip = (
'"\""',
'"\b"',
'"\f"',
'"\n"',
'"\r"',
'"\t"',
'"\u0001"',
);
# Seems most handlers decode the escaped slash (solidus), but don't
# encode it escaped. TODO: What does the spec *really* say?
# For now, just test decoding. Improper decoding breaks things.
my %one_way = (
'"\/"' => '/', # escaped solidus
);
test ($_) for qw(XS JSON DWIW);
TODO: {
local $TODO = q[JSON::Syck doesn't escape things properly];
test ($_) for qw(Syck);
}
sub test {
my ($backend) = @_;
my $j = eval {
JSON::Any->import($backend);
JSON::Any->new;
};
diag "$backend: " . $@ and next if $@;
$j and $j->handler or next;
diag "handler is " . ( ref( $j->handler ) || $j->handlerType );
plan 'no_plan' unless $ENV{JSON_ANY_RAN_TESTS};
$ENV{JSON_ANY_RAN_TESTS} = 1;
for my $test_orig ( @round_trip ) {
my $test = "[$test_orig]"; # make it an array
my $data = eval { JSON::Any->jsonToObj($test) };
my $json = JSON::Any->objToJson($data);
# don't bother white spaces
for ($test, $json) {
s/([,:]) /$1/eg;
}
my $desc = "roundtrip $test -> " . Dumper($data) . " -> $json";
utf8::encode($desc);
is $json, $test, $desc;
}
while ( my ($encoded, $expected) = each %one_way ) {
my $test = "[$encoded]";
my $data = eval { JSON::Any->jsonToObj($test) };
my $desc = "oneway $test -> " . Dumper($data);
utf8::encode($desc);
is $data->[0], $expected, $desc;
}
}
|