Cpanel-JSON-XS
view release on metacpan or search on metacpan
relaxed implements allow_dupkeys.
- support all 5 unicode L<BOM|/BOM>'s: UTF-8, UTF-16LE, UTF-16BE, UTF-32LE,
UTF-32BE, encoding internally to UTF-8.
=cut
our @ISA = qw(Exporter);
our @EXPORT = qw(encode_json decode_json to_json from_json);
sub to_json($@) {
if ($] >= 5.008) {
require Carp;
Carp::croak ("Cpanel::JSON::XS::to_json has been renamed to encode_json,".
" either downgrade to pre-2.0 versions of Cpanel::JSON::XS or".
" rename the call");
} else {
_to_json(@_);
}
}
sub from_json($@) {
if ($] >= 5.008) {
require Carp;
Carp::croak ("Cpanel::JSON::XS::from_json has been renamed to decode_json,".
" either downgrade to pre-2.0 versions of Cpanel::JSON::XS or".
" rename the call");
} else {
_from_json(@_);
}
}
}
}
BEGIN {
my $const_true = $true;
my $const_false = $false;
*true = sub () { $const_true };
*false = sub () { $const_false };
}
sub is_bool($) {
shift if @_ == 2; # as method call
(ref($_[0]) and UNIVERSAL::isa( $_[0], JSON::PP::Boolean::))
or (exists $INC{'Types/Serialiser.pm'} and Types::Serialiser::is_bool($_[0]))
}
XSLoader::load 'Cpanel::JSON::XS', $XS_VERSION;
1;
=head1 SEE ALSO
#if (1) {
# use Storable;
# open my $fh, "<:unix", "/opt/crossfire/share/cfserver/faces" or die "$!";
# my $faces = Storable::thaw do { <$fh> };
# $json = objToJson $faces;
# open my $fh2, ">:unix", "faces.json" or die "$!";
# print $fh2 $json;
# warn length $json;
#}
sub bench($) {
my ($code) = @_;
my $pst = Storable::nfreeze Cpanel::JSON::XS::decode_json $json; # seperately decode as storable stringifies :/
my $perl = Cpanel::JSON::XS::decode_json $json;
my $count = 5;
my $times = 200;
my $cent = eval "sub { my \$t = Time::HiRes::time; " . (join ";", ($code) x $count) . "; Time::HiRes::time - \$t }";
$cent->();
t/03_types.t view on Meta::CPAN
BEGIN { $| = 1; print "1..87\n"; }
use utf8;
use Cpanel::JSON::XS;
our $test;
sub ok($;$) {
print $_[0] ? "" : "not ", "ok ", ++$test;
print @_ > 1 ? " # $_[1]\n" : "\n";
}
ok (!defined Cpanel::JSON::XS->new->allow_nonref->decode ('null'));
my $null = Cpanel::JSON::XS->new->allow_nonref->decode ('null');
my $true = Cpanel::JSON::XS->new->allow_nonref->decode ('true');
my $false = Cpanel::JSON::XS->new->allow_nonref->decode ('false');
ok ($true == 1, sprintf("true: numified %d", 0+$true));
ok ($false == 0, sprintf("false: numified %d", 0+$false));
t/99_binary.t view on Meta::CPAN
use Test::More tests => 300;
use Cpanel::JSON::XS;
use B ();
my $as = Cpanel::JSON::XS->new->ascii->shrink;
my $us = Cpanel::JSON::XS->new->utf8->shrink;
my $bs = Cpanel::JSON::XS->new->binary;
sub test($) {
my $c = $_[0];
my $js = $as->encode([$c]);
is ($c, ((decode_json $js)->[0]), "ascii ".B::cstring($c));
$js = $us->encode([$c]);
is ($c, ($us->decode($js))->[0], "utf8 ".B::cstring($c));
}
sub test_bin($) {
my $c = $_[0];
my $js = $bs->encode([$c]);
is ($js, $bs->encode($bs->decode($js)), "binary ".B::cstring($c));
}
srand 0; # doesn't help too much, but it's at least more deterministic
for (1..25) {
test join "", map chr ($_ & 255), 0..$_;
test_bin join "", map chr ($_ & 255), 0..$_;
( run in 0.884 second using v1.01-cache-2.11-cpan-65fba6d93b7 )