JSON-SIMD

 view release on metacpan or  search on metacpan

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

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

use utf8;
use JSON::SIMD;

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

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

eval { JSON::SIMD->new->allow_nonref (1)->utf8 (1)->decode ('"ü"') };
ok $@ =~ /input is not valid UTF-8/;

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

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

use utf8;
use JSON::SIMD;
no warnings;

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

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

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

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

use utf8;
use Types::Serialiser;
use JSON::SIMD;

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

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

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

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

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

use JSON::SIMD;

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/113_limit.t  view on Meta::CPAN

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

use JSON::SIMD;

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

my $def = 512;

my $js = JSON::SIMD->new->use_simdjson(0);

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/122_trailing.t  view on Meta::CPAN

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

use utf8;
use JSON::SIMD;
no warnings;

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

# more tests with trailing garbage
# the good old legacy parser naturally and trivially detects all these cases correctly,
# however, with simdjson we have to test the various dodgy workarounds :(
for (
    [ q/"a" foo/,           'garbage', 3],
    [ q/"a" "b"/,           'garbage', 3],

t/123_decode_at_pointer.t  view on Meta::CPAN

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

use utf8;
use JSON::SIMD;
no warnings;

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

sub test {
	my ($J, $escaped) = @_;

	my $obj = '{
		"foo" : [
			{ "bar": "baz" },

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

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

use JSON::SIMD;

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::SIMD;

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

my $def = 512;

my $js = JSON::SIMD->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::SIMD;
use Tie::Hash;
use Tie::Array;

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

my $js = JSON::SIMD->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..10\n"; }

use utf8;
use JSON::SIMD;

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

my $json = JSON::SIMD->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/199_binary.t  view on Meta::CPAN

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

use JSON::SIMD;

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

sub test($) {
   my $js;

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

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

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

use JSON::SIMD;

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

sub test($) {
   my $js;

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



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