JSON-XS

 view release on metacpan or  search on metacpan

t/01_utf8.t  view on Meta::CPAN

BEGIN { $| = 1; print "1..9\n"; }

use utf8;
use JSON::XS;

our $test;
sub ok($) {
   print $_[0] ? "" : "not ", "ok ", ++$test, "\n";
}

ok (JSON::XS->new->allow_nonref (1)->utf8 (1)->encode ("ü") eq "\"\xc3\xbc\"");
ok (JSON::XS->new->allow_nonref (1)->encode ("ü") eq "\"ü\"");
ok (JSON::XS->new->allow_nonref (1)->ascii (1)->utf8 (1)->encode (chr 0x8000) eq '"\u8000"');
ok (JSON::XS->new->allow_nonref (1)->ascii (1)->utf8 (1)->pretty (1)->encode (chr 0x10402) eq "\"\\ud801\\udc02\"\n");

eval { JSON::XS->new->allow_nonref (1)->utf8 (1)->decode ('"ü"') };
ok $@ =~ /malformed UTF-8/;

t/02_error.t  view on Meta::CPAN

BEGIN { $| = 1; print "1..35\n"; }

use utf8;
use JSON::XS;
no warnings;

our $test;
sub ok($) {
   print $_[0] ? "" : "not ", "ok ", ++$test, "\n";
}

eval { JSON::XS->new->encode ([\-1]) }; ok $@ =~ /cannot encode reference/;
eval { JSON::XS->new->encode ([\undef]) }; ok $@ =~ /cannot encode reference/;
eval { JSON::XS->new->encode ([\2]) }; ok $@ =~ /cannot encode reference/;
eval { JSON::XS->new->encode ([\{}]) }; ok $@ =~ /cannot encode reference/;
eval { JSON::XS->new->encode ([\[]]) }; ok $@ =~ /cannot encode reference/;
eval { JSON::XS->new->encode ([\\1]) }; ok $@ =~ /cannot encode reference/;

t/03_types.t  view on Meta::CPAN

BEGIN { $| = 1; print "1..76\n"; }

use utf8;
use Types::Serialiser;
use JSON::XS;

our $test;
sub ok($) {
   print $_[0] ? "" : "not ", "ok ", ++$test, "\n";
}

ok (!defined JSON::XS->new->allow_nonref (1)->decode ('null'));
ok (JSON::XS->new->allow_nonref (1)->decode ('true') == 1);
ok (JSON::XS->new->allow_nonref (1)->decode ('false') == 0);

my $true  = JSON::XS->new->allow_nonref (1)->decode ('true');
ok ($true eq 1);
ok (Types::Serialiser::is_bool $true);

t/12_blessed.t  view on Meta::CPAN

BEGIN { $| = 1; print "1..16\n"; }

use JSON::XS;

our $test;
sub ok($;$) {
   print $_[0] ? "" : "not ", "ok ", ++$test, "\n";
}

my $o1 = bless { a => 3 }, "XX";
my $o2 = bless \(my $dummy = 1), "YY";

sub XX::TO_JSON {
   {__,""}
}

t/13_limit.t  view on Meta::CPAN

BEGIN { $| = 1; print "1..11\n"; }

use JSON::XS;

our $test;
sub ok($;$) {
   print $_[0] ? "" : "not ", "ok ", ++$test, "\n";
}

my $def = 512;

my $js = JSON::XS->new;

ok (!eval { $js->decode (("[" x ($def + 1)) . ("]" x ($def + 1))) });
ok (ref $js->decode (("[" x $def) . ("]" x $def)));
ok (ref $js->decode (("{\"\":" x ($def - 1)) . "[]" . ("}" x ($def - 1))));

t/16_tied.t  view on Meta::CPAN

BEGIN { $| = 1; print "1..2\n"; }

use JSON::XS;
use Tie::Hash;
use Tie::Array;

our $test;
sub ok($;$) {
   print $_[0] ? "" : "not ", "ok ", ++$test, "\n";
}

my $js = JSON::XS->new;

tie my %h, 'Tie::StdHash';
%h = (a => 1);

ok ($js->encode (\%h) eq '{"a":1}');

t/17_relaxed.t  view on Meta::CPAN

BEGIN { $| = 1; print "1..8\n"; }

use utf8;
use JSON::XS;

our $test;
sub ok($) {
   print $_[0] ? "" : "not ", "ok ", ++$test, "\n";
}

my $json = JSON::XS->new->relaxed;

ok ('[1,2,3]' eq encode_json $json->decode (' [1,2, 3]'));
ok ('[1,2,4]' eq encode_json $json->decode ('[1,2, 4 , ]'));
ok (!eval { $json->decode ('[1,2, 3,4,,]') });
ok (!eval { $json->decode ('[,1]') });

t/99_binary.t  view on Meta::CPAN

BEGIN { $| = 1; print "1..24576\n"; }

use JSON::XS;

our $test;
sub ok($;$) {
   print $_[0] ? "" : "not ", "ok ", ++$test, " - $_[1]\n";
}

sub test($) {
   my $js;

   $js = JSON::XS->new->allow_nonref(0)->utf8->ascii->shrink->encode ([$_[0]]);
   ok ($_[0] eq ((decode_json $js)->[0]), 0);
   $js = JSON::XS->new->allow_nonref(0)->utf8->ascii->encode ([$_[0]]);
   ok ($_[0] eq (JSON::XS->new->utf8->shrink->decode($js))->[0], 1);



( run in 1.797 second using v1.01-cache-2.11-cpan-5b529ec07f3 )