Vendor Perl and libraries
|
What has changed
|
encode_json([$$, "" . $$])
Perl Version | Library | Result |
---|---|---|
5.10.1 | JSON::XS | ["213","213"] |
5.16.3 | JSON::XS | [213,"213"] |
5.16.3 | JSON::PP | [213,"213"] |
5.18.2 | Cpanel::JSON::XS | [213,"213"] |
$x = 12; utf8::decode($x); encode_json([$x])
Perl Version | Library | Result |
---|---|---|
5.10.1 | JSON::XS | [12] |
5.16.3 | JSON::XS | ["12"] |
5.16.3 | JSON::PP | ["12"] |
5.18.2 | Cpanel::JSON::XS | ["12"] |
$x = '19a'; $x += 0; encode_json([$x])
Perl Version | Library | Result |
---|---|---|
5.10.1 | JSON::XS | [19] |
5.16.3 | JSON::XS | [19] |
5.16.3 | JSON::PP | [19] |
5.18.2 | Cpanel::JSON::XS | [19.0] |
use Data::Dumper; Dumper(decode_json('[1e4]'))
Perl Version | Library | Result |
---|---|---|
5.10.1 | JSON::XS | ['10000'] |
5.16.3 | JSON::XS | ['10000'] |
5.16.3 | JSON::PP | [10000] |
5.18.2 | Cpanel::JSON::XS | ['10000'] |
$x = 1844674407370955161; encode_json([$x, $x / 10])
Perl Version | Library | Result |
---|---|---|
5.10.1 | JSON::XS | [1.84467440737096e+18,1.84467440737096e+17] |
5.16.3 | JSON::XS | [1.84467440737096e+18,1.84467440737096e+17] |
5.16.3 | JSON::PP | [1844674407370955161,1.84467440737096e+17] |
5.18.2 | Cpanel::JSON::XS | [1.84467440737096e+18,1.84467440737096e+17] |
#! /usr/bin/perl use warnings; use strict; { package MyIncrementer; use Tie::Scalar; use parent -norequire => 'Tie::StdScalar'; sub TIESCALAR { my ($class, $val) = @_; bless \$val, $class } sub FETCH { my $s = shift; $$s++ } } use JSON::XS; my $json = 'JSON::XS'->new->allow_nonref; tie my $x, 'MyIncrementer', 'Xa'; print $json->encode($x) for 1 .. 4;
Perl Version | Library | Result |
---|---|---|
5.10.1 | JSON::XS | "Xb""Xd""Xf""Xh" |
5.16.3 | JSON::XS | "Xb""Xd""Xf""Xh" |
5.16.3 | JSON::PP | "Xa""Xb""Xc""Xd" |
5.18.2 | Cpanel::JSON::XS | "Xa""Xb""Xc""Xd" |
$j = 'JSON::XS'->new->allow_nonref; $x = 12; print $j->decode($x), $j->encode($x);
Perl Version | Library | Result |
---|---|---|
5.10.1 | JSON::XS !! 2.27 | 12"12" |
5.10.1 | JSON::XS !! 3.01 | 1212 |
5.16.3 | JSON::PP | 1212 |
5.18.2 | Cpanel::JSON::XS | 1212 |
#! /usr/bin/perl use warnings; use strict; use JSON::XS; my $integer = "12"; my $string = 42; my $float = "122e-1"; print encode_json([ int $integer, "" . $string, 0 + $float ]);
Output:
[12,"42",12.2]
JSON_TYPE_INT, JSON_TYPE_FLOAT, JSON_TYPE_STRING, JSON_TYPE_BOOL, (+ JSON_TYPE_NULL)
[], {}
json_type_arrayof(), json_type_hashof()
json_type_anyof()
JSON_TYPE_INT_OR_NULL, …
json_type_null_or_anyof()
json_type_anyof()
my $type = parse_api_definition($symbol); $json->encode($data, $type);
#! /usr/bin/perl use warnings; use strict; use Cpanel::JSON::XS; use Cpanel::JSON::XS::Type; my $type = {count => JSON_TYPE_INT, average => JSON_TYPE_FLOAT, name => JSON_TYPE_STRING, is_enabled => JSON_TYPE_BOOL, orders => json_type_arrayof(JSON_TYPE_INT)}; print 'Cpanel::JSON::XS'->new->pretty->canonical ->encode({count => '12', average => '11.2', name => 100 / 3, is_enabled => 1, orders => [ 1 .. 10 ] }, $type);
{ "average" : 11.2, "count" : 12, "is_enabled" : true, "name" : "33.3333333333333", "orders" : [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ] }
#! /usr/bin/perl use warnings; use strict; use Cpanel::JSON::XS; use Cpanel::JSON::XS::Type; my $node = {value => JSON_TYPE_STRING}; $node->{children} = json_type_arrayof($node); print 'Cpanel::JSON::XS'->new->pretty->encode({ value => 'root', children => [{ value => 'child1', children => [ {value => 'grandchild1'} ] }], }, $node);
#! /usr/bin/perl use warnings; use strict; use Cpanel::JSON::XS; use Cpanel::JSON::XS::Type; my $node = {value => JSON_TYPE_STRING}; $node->{children} = json_type_arrayof($node); # Creates reference cycle: -----------^^^^^ print 'Cpanel::JSON::XS'->new->pretty->encode({ value => 'root', children => [{ value => 'child1', children => [ {value => 'grandchild1'} ] }], }, $node);
#! /usr/bin/perl use warnings; use strict; use Cpanel::JSON::XS; use Cpanel::JSON::XS::Type; my $node = {value => JSON_TYPE_STRING}; $node->{children} = json_type_arrayof( json_type_weaken($node) ); print 'Cpanel::JSON::XS'->new->pretty->encode({ value => 'root', children => [{ value => 'child1', children => [ {value => 'grandchild1'} ] }], }, $node);
json_type_weaken()
already merged, but not yet released to CPAN
{ "children" : [ { "children" : [ { "value" : "grandchild1" } ], "value" : "child1" } ], "value" : "root" }
Coming soon!Pull request not yet merged #! /usr/bin/perl use warnings; use strict; use Cpanel::JSON::XS; use Cpanel::JSON::XS::Type; my $struct = 'Cpanel::JSON::XS'->new ->decode('[null,1,1.1,"1",[0],true]', my $type); /* types */ #define JSON_TYPE_SCALAR 0x0000 #define JSON_TYPE_BOOL 0x0001 #define JSON_TYPE_INT 0x0002 #define JSON_TYPE_FLOAT 0x0003 #define JSON_TYPE_STRING 0x0004 /* flags */ #define JSON_TYPE_CAN_BE_NULL 0x0100 /* null type */ #define JSON_TYPE_NULL JSON_TYPE_CAN_BE_NULL |
$struct = [ undef, 1, '1.1', '1', [ 0 ], 1 ]; $type = [ 256, 2, 3, 4, [ 2 ], 1 ]; |
~ ^ & |
TPCiG | 2018 |