Created
September 14, 2020 13:55
-
-
Save kfly8/57b02767ef93ac19d3f5231a8bc955d8 to your computer and use it in GitHub Desktop.
JSON::Types vs Cpanel::JSON::XS::Type
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| use v5.32.0; | |
| use warnings; | |
| use Benchmark qw(cmpthese); | |
| use JSON::XS (); | |
| use Cpanel::JSON::XS (); | |
| use Cpanel::JSON::XS::Type; | |
| use JSON::Types; | |
| use constant Item => { | |
| id => JSON_TYPE_INT, | |
| name => JSON_TYPE_STRING, | |
| fg => JSON_TYPE_BOOL, | |
| }; | |
| use constant Items => { | |
| items => json_type_arrayof(Item), | |
| count => JSON_TYPE_INT, | |
| }; | |
| say "perl version - $]"; | |
| say "JSON::XS - ${JSON::XS::VERSION}"; | |
| say "Cpanel::JSON::XS - ${Cpanel::JSON::XS::VERSION}"; | |
| say "JSON::Types - ${JSON::Types::VERSION}"; | |
| say 'Encode small object'; | |
| cmpthese -1, { | |
| json_xs_with_json_types => sub { | |
| my $obj = { | |
| count => number 123, | |
| items => [ | |
| { | |
| id => number 1, | |
| name => string 'foo', | |
| fg => bool 1, | |
| }, | |
| { | |
| id => number 2, | |
| name => string 'bar', | |
| fg => bool 0, | |
| }, | |
| { | |
| id => number 3, | |
| name => string 'baz', | |
| fg => bool 1, | |
| }, | |
| { | |
| id => number 4, | |
| name => string 'baz', | |
| fg => bool 0, | |
| }, | |
| { | |
| id => number 5, | |
| name => string 'boo', | |
| fg => bool 1, | |
| } | |
| ], | |
| }; | |
| JSON::XS::encode_json($obj); | |
| }, | |
| json_xs_with_json_types_lazy => sub { | |
| my $obj = { | |
| count => number 123, | |
| items => [ | |
| { | |
| id => number 1, | |
| name => 'foo', | |
| fg => bool 1, | |
| }, | |
| { | |
| id => number 2, | |
| name => 'bar', | |
| fg => bool 0, | |
| }, | |
| { | |
| id => number 3, | |
| name => 'baz', | |
| fg => bool 1, | |
| }, | |
| { | |
| id => number 4, | |
| name => 'baz', | |
| fg => bool 0, | |
| }, | |
| { | |
| id => number 5, | |
| name => 'boo', | |
| fg => bool 1, | |
| } | |
| ], | |
| }; | |
| JSON::XS::encode_json($obj); | |
| }, | |
| cpanel_json_with_types => sub { | |
| my $obj = { | |
| count => 123, | |
| items => [ | |
| { | |
| id => 1, | |
| name => 'foo', | |
| fg => 1, | |
| }, | |
| { | |
| id => 2, | |
| name => 'bar', | |
| fg => 0, | |
| }, | |
| { | |
| id => 3, | |
| name => 'baz', | |
| fg => 1, | |
| }, | |
| { | |
| id => 4, | |
| name => 'baz', | |
| fg => 0, | |
| }, | |
| { | |
| id => 5, | |
| name => 'boo', | |
| fg => 1, | |
| } | |
| ], | |
| }; | |
| Cpanel::JSON::XS::encode_json($obj, Items); | |
| }, | |
| cpanel_json_but_wrong => sub { | |
| my $obj = { | |
| count => 123, | |
| items => [ | |
| { | |
| id => 1, | |
| name => 'foo', | |
| fg => 1, | |
| }, | |
| { | |
| id => 2, | |
| name => 'bar', | |
| fg => 0, | |
| }, | |
| { | |
| id => 3, | |
| name => 'baz', | |
| fg => 1, | |
| }, | |
| { | |
| id => 4, | |
| name => 'baz', | |
| fg => 0, | |
| }, | |
| { | |
| id => 5, | |
| name => 'boo', | |
| fg => 1, | |
| } | |
| ], | |
| }; | |
| Cpanel::JSON::XS::encode_json($obj); | |
| }, | |
| json_xs_but_wrong => sub { | |
| my $obj = { | |
| count => 123, | |
| items => [ | |
| { | |
| id => 1, | |
| name => 'foo', | |
| fg => 1, | |
| }, | |
| { | |
| id => 2, | |
| name => 'bar', | |
| fg => 0, | |
| }, | |
| { | |
| id => 3, | |
| name => 'baz', | |
| fg => 1, | |
| }, | |
| { | |
| id => 4, | |
| name => 'baz', | |
| fg => 0, | |
| }, | |
| { | |
| id => 5, | |
| name => 'boo', | |
| fg => 1, | |
| } | |
| ], | |
| }; | |
| JSON::XS::encode_json($obj); | |
| } | |
| } | |
Author
kfly8
commented
Sep 14, 2020
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment