JSON-MaybeXS-1.003005/000700 000766 000024 00000000000 12503636114 014263 5ustar00etherstaff000000 000000 JSON-MaybeXS-1.003005/Changes000644 000766 000024 00000003766 12503636102 015601 0ustar00etherstaff000000 000000 Revision history for JSON-MaybeXS 1.003005 - 2015-03-22 - fix x_contributors metadata that was killing metacpan (see https://github.com/CPAN-API/cpan-api/issues/401) 1.003004 - 2015-03-14 - caveat added to documentation about type checking the object returned by new() (John SJ Anderson, RT#102733) 1.003003 - 2014-12-06 - ensure an old Cpanel::JSON::XS is upgraded if it is too old, as it will always be used in preference to JSON::XS - avoid "JSON::XS::Boolean::* redefined" warnings caused by an old JSON::XS loaded at the same time as a newer Cpanel::JSON::XS 1.003002 - 2014-11-16 - correctly fix boolean interoperability with older Cpanel::JSON::MaybeXS 1.003001 - 2014-11-13 - fix boolean interoperability with older Cpanel::JSON::MaybeXS 1.003_000 - 2014-10-30 - add :legacy tag to support legacy apps 1.002006 - 2014-10-22 - add some additional test diagnostics, to help find bad version combinations of JSON backends 1.002005 - 2014-10-12 - fix "can I haz XS?" logic precedence in Makefile.PL - added the ':all' export tag - removed dependency on Safe::Isa - repository moved to git://git.shadowcat.co.uk/p5sagit/JSON-MaybeXS.git 1.002004 - 2014-10-11 - support use of PUREPERL_ONLY in Makefile.PL to avoid adding an XS dependency - new is_bool() interface 1.002003 - 2014-10-07 - document how to use booleans 1.002002 - 2014-04-23 - more metadata fiddling, to remove the Cpanel::JSON::XS dependency visible to static analyzers (the prequisites at install time remain unchanged) 1.002001 - 2014-04-23 - fix installation on older perls with an older ExtUtils::MakeMaker (RT#94964) 1.002000 - 2014-04-22 - Now also supports JSON::XS, when available: Cpanel::JSON::XS will only be installed when there is no XS backend already installed; Cpanel::JSON::XS will be used over JSON::XS when both are installed. 1.001000 - 2013-12-11 - Add a constructor for people who want to forget how silly the JSON API is 1.000000 - 2013-05-22 - Released on an unsuspecting world. JSON-MaybeXS-1.003005/MANIFEST000644 000766 000024 00000001000 12503636114 015415 0ustar00etherstaff000000 000000 Changes lib/JSON/MaybeXS.pm maint/Makefile.PL.include Makefile.PL MANIFEST This list of files t/cpanel.t t/legacy.t t/lib/is_bool.pm t/new.t t/none.t t/pp.t t/preload_cpanel.t t/preload_xs.t t/xs.t xt/json_pm.t xt/json_pm_legacy.t xt/json_pp_legacy.t xt/json_xs_legacy.t META.yml Module YAML meta-data (added by MakeMaker) META.json Module JSON meta-data (added by MakeMaker) README README file (added by Distar) JSON-MaybeXS-1.003005/META.json000600 000766 000024 00000003663 12503636114 015716 0ustar00etherstaff000000 000000 { "abstract" : "Use L with a fallback to L and L", "author" : [ "mst - Matt S. Trout (cpan:MSTROUT) " ], "dynamic_config" : 1, "generated_by" : "ExtUtils::MakeMaker version 6.98, CPAN::Meta::Converter version 2.150001", "license" : [ "perl_5" ], "meta-spec" : { "url" : "http://search.cpan.org/perldoc?CPAN::Meta::Spec", "version" : "2" }, "name" : "JSON-MaybeXS", "no_index" : { "directory" : [ "t", "inc" ] }, "prereqs" : { "configure" : { "requires" : { "ExtUtils::CBuilder" : "0.27", "ExtUtils::MakeMaker" : "0", "File::Spec" : "0", "File::Temp" : "0" } }, "runtime" : { "recommends" : { "Cpanel::JSON::XS" : "2.3310" }, "requires" : { "Carp" : "0", "JSON::PP" : "2.27202", "Scalar::Util" : "0", "perl" : "5.006" } }, "test" : { "requires" : { "Test::More" : "0.88", "Test::Without::Module" : "0.17" } } }, "release_status" : "stable", "resources" : { "bugtracker" : { "mailto" : "bug-JSON-MaybeXS@rt.cpan.org", "web" : "https://rt.cpan.org/Public/Dist/Display.html?Name=JSON-MaybeXS" }, "repository" : { "type" : "git", "url" : "git://git.shadowcat.co.uk/p5sagit/JSON-MaybeXS.git", "web" : "http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=p5sagit/JSON-MaybeXS.git" } }, "version" : "1.003005", "x_contributors" : [ "Clinton Gormley ", "Graham Knop ", "John SJ Anderson ", "Karen Etheridge ", "Kieren Diment ", "Matt S Trout " ] } JSON-MaybeXS-1.003005/META.yml000600 000766 000024 00000002222 12503636114 015534 0ustar00etherstaff000000 000000 --- abstract: 'Use L with a fallback to L and L' author: - 'mst - Matt S. Trout (cpan:MSTROUT) ' build_requires: Test::More: '0.88' Test::Without::Module: '0.17' configure_requires: ExtUtils::CBuilder: '0.27' ExtUtils::MakeMaker: '0' File::Spec: '0' File::Temp: '0' dynamic_config: 1 generated_by: 'ExtUtils::MakeMaker version 6.98, CPAN::Meta::Converter version 2.150001' license: perl meta-spec: url: http://module-build.sourceforge.net/META-spec-v1.4.html version: '1.4' name: JSON-MaybeXS no_index: directory: - t - inc recommends: Cpanel::JSON::XS: '2.3310' requires: Carp: '0' JSON::PP: '2.27202' Scalar::Util: '0' perl: '5.006' resources: bugtracker: https://rt.cpan.org/Public/Dist/Display.html?Name=JSON-MaybeXS repository: git://git.shadowcat.co.uk/p5sagit/JSON-MaybeXS.git version: '1.003005' x_contributors: - 'Clinton Gormley ' - 'Graham Knop ' - 'John SJ Anderson ' - 'Karen Etheridge ' - 'Kieren Diment ' - 'Matt S Trout ' JSON-MaybeXS-1.003005/Makefile.PL000644 000766 000024 00000015064 12503631277 016262 0ustar00etherstaff000000 000000 use strict; use warnings FATAL => 'all'; use ExtUtils::MakeMaker; (do 'maint/Makefile.PL.include' or die $@) unless -f 'META.yml'; my %WriteMakefileArgs = ( NAME => 'JSON::MaybeXS', VERSION_FROM => 'lib/JSON/MaybeXS.pm', META_MERGE => { 'meta-spec' => { version => 2 }, dynamic_config => 1, resources => { # r/w: p5sagit@git.shadowcat.co.uk:JSON-MaybeXS.git repository => { url => 'git://git.shadowcat.co.uk/p5sagit/JSON-MaybeXS.git', web => 'http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=p5sagit/JSON-MaybeXS.git', type => 'git', }, bugtracker => { mailto => 'bug-JSON-MaybeXS@rt.cpan.org', web => 'https://rt.cpan.org/Public/Dist/Display.html?Name=JSON-MaybeXS', }, }, x_contributors => [ # manually added, from git shortlog -e -s 'Clinton Gormley ', 'Graham Knop ', 'John SJ Anderson ', 'Karen Etheridge ', 'Kieren Diment ', 'Matt S Trout ', ], }, META_ADD => { prereqs => { configure => { requires => { 'ExtUtils::MakeMaker' => '0', 'ExtUtils::CBuilder' => '0.27', 'File::Spec' => '0', 'File::Temp' => '0', }, }, runtime => { requires => { 'Scalar::Util' => '0', 'Carp' => '0', 'JSON::PP' => '2.27202', # we may also add a runtime prereq for Cpanel::JSON::XS, on the # installer's machine perl => '5.006', }, recommends => { 'Cpanel::JSON::XS' => '2.3310' }, }, test => { requires => { 'Test::Without::Module' => '0.17', 'Test::More' => '0.88', }, }, }, }, ); my $eumm_version = eval $ExtUtils::MakeMaker::VERSION; for (qw(configure build test runtime)) { my $key = $_ eq 'runtime' ? 'PREREQ_PM' : uc $_.'_REQUIRES'; next unless exists $WriteMakefileArgs{META_ADD}{prereqs}{$_} or exists $WriteMakefileArgs{$key}; my $r = $WriteMakefileArgs{$key} = { %{$WriteMakefileArgs{META_ADD}{prereqs}{$_}{requires} || {}}, %{delete $WriteMakefileArgs{$key} || {}}, }; defined $r->{$_} or delete $r->{$_} for keys %$r; } # dynamic prereqs get added here. # we require Cpanel::JSON::XS, except if JSON::XS is already installed. # (we also always recommend Cpanel::JSON::XS, just to make sure.) $WriteMakefileArgs{PREREQ_PM}{'Cpanel::JSON::XS'} = '2.3310' if not parse_args()->{PUREPERL_ONLY} and ((eval { require Cpanel::JSON::XS; 1 } and not eval { Cpanel::JSON::XS->VERSION('2.3310'); 1 }) or (not eval { require JSON::XS; 1; } and can_xs())); # avoid "JSON::XS::Boolean::* redefined" warnings caused by incompatibilities # between JSON::XS 2.x and 3.0 -- # if JSON::XS is installed and < 3.0 # and Cpanel::JSON::JSON is (or is about to be) >= 3.0, # then update JSON::XS to eliminate the incompatibility $WriteMakefileArgs{PREREQ_PM}{'JSON::XS'} = '3.00' if not parse_args()->{PUREPERL_ONLY} and eval { require JSON::XS; 1 } and not eval { JSON::XS->VERSION('3.0'); 1 } and (eval { require Cpanel::JSON::XS; Cpanel::JSON::XS->VERSION('3.0'); 1 } # we presume here that if we are installing Cpanel::JSON::XS, we # are installing the latest version or exists $WriteMakefileArgs{PREREQ_PM}{'Cpanel::JSON::XS'}); $WriteMakefileArgs{MIN_PERL_VERSION} = delete $WriteMakefileArgs{PREREQ_PM}{perl} || 0; die 'attention developer: you need to do a sane meta merge here!' if keys %{$WriteMakefileArgs{BUILD_REQUIRES}}; $WriteMakefileArgs{BUILD_REQUIRES} = { %{$WriteMakefileArgs{BUILD_REQUIRES} || {}}, %{delete $WriteMakefileArgs{TEST_REQUIRES}} } if $eumm_version < 6.63_03; $WriteMakefileArgs{PREREQ_PM} = { %{$WriteMakefileArgs{PREREQ_PM}}, %{delete $WriteMakefileArgs{BUILD_REQUIRES}} } if $eumm_version < 6.55_01; delete $WriteMakefileArgs{CONFIGURE_REQUIRES} if $eumm_version < 6.51_03; delete $WriteMakefileArgs{MIN_PERL_VERSION} if $eumm_version < 6.48; delete @WriteMakefileArgs{qw(META_ADD META_MERGE)} if $eumm_version < 6.46; delete $WriteMakefileArgs{LICENSE} if $eumm_version < 6.31; WriteMakefile(%WriteMakefileArgs); sub parse_args { # copied from EUMM require ExtUtils::MakeMaker; require Text::ParseWords; ExtUtils::MakeMaker::parse_args( my $tmp = {}, Text::ParseWords::shellwords($ENV{PERL_MM_OPT} || ''), @ARGV, ); return $tmp->{ARGS} || {}; } # can we locate a (the) C compiler sub can_cc { my @chunks = split(/ /, $Config::Config{cc}) or return; # $Config{cc} may contain args; try to find out the program part while (@chunks) { return can_run("@chunks") || (pop(@chunks), next); } return; } # check if we can run some command sub can_run { my ($cmd) = @_; return $cmd if -x $cmd; if (my $found_cmd = MM->maybe_command($cmd)) { return $found_cmd; } require File::Spec; for my $dir ((split /$Config::Config{path_sep}/, $ENV{PATH}), '.') { next if $dir eq ''; my $abs = File::Spec->catfile($dir, $cmd); return $abs if (-x $abs or $abs = MM->maybe_command($abs)); } return; } # Can our C compiler environment build XS files sub can_xs { # Do we have the configure_requires checker? local $@; eval "require ExtUtils::CBuilder; ExtUtils::CBuilder->VERSION(0.27)"; if ( $@ ) { # They don't obey configure_requires, so it is # someone old and delicate. Try to avoid hurting # them by falling back to an older simpler test. return can_cc(); } # Do we have a working C compiler my $builder = ExtUtils::CBuilder->new( quiet => 1, ); unless ( $builder->have_compiler ) { # No working C compiler return 0; } # Write a C file representative of what XS becomes require File::Temp; my ( $FH, $tmpfile ) = File::Temp::tempfile( "compilexs-XXXXX", SUFFIX => '.c', ); binmode $FH; print $FH <<'END_C'; #include "EXTERN.h" #include "perl.h" #include "XSUB.h" int main(int argc, char **argv) { return 0; } int boot_sanexs() { return 1; } END_C close $FH; # Can the C compiler access the same headers XS does my @libs = (); my $object = undef; eval { local $^W = 0; $object = $builder->compile( source => $tmpfile, ); @libs = $builder->link( objects => $object, module_name => 'sanexs', ); }; my $result = $@ ? 0 : 1; # Clean up all the build files foreach ( $tmpfile, $object, @libs ) { next unless defined $_; 1 while unlink; } return $result; } JSON-MaybeXS-1.003005/README000600 000766 000024 00000012050 12503636114 015143 0ustar00etherstaff000000 000000 NAME JSON::MaybeXS - Use Cpanel::JSON::XS with a fallback to JSON::XS and JSON::PP SYNOPSIS use JSON::MaybeXS; my $data_structure = decode_json($json_input); my $json_output = encode_json($data_structure); my $json = JSON->new; my $json_with_args = JSON::MaybeXS->new(utf8 => 1); # or { utf8 => 1 } DESCRIPTION This module first checks to see if either Cpanel::JSON::XS or JSON::XS is already loaded, in which case it uses that module. Otherwise it tries to load Cpanel::JSON::XS, then JSON::XS, then JSON::PP in order, and either uses the first module it finds or throws an error. It then exports the "encode_json" and "decode_json" functions from the loaded module, along with a "JSON" constant that returns the class name for calling "new" on. If you're writing fresh code rather than replacing JSON.pm usage, you might want to pass options as constructor args rather than calling mutators, so we provide our own "new" method that supports that. EXPORTS "encode_json", "decode_json" and "JSON" are exported by default; "is_bool" is exported on request. To import only some symbols, specify them on the "use" line: use JSON::MaybeXS qw(encode_json decode_json is_bool); # functions only use JSON::MaybeXS qw(JSON); # JSON constant only To import all available sensible symbols ("encode_json", "decode_json", and "is_bool"), use ":all": use JSON::MaybeXS ':all'; To import all symbols including those needed by legacy apps that use JSON::PP: use JSON::MaybeXS ':legacy'; This imports the "to_json" and "from_json" symbols as well as everything in ":all". NOTE: This is to support legacy code that makes extensive use of "to_json" and "from_json" which you are not yet in a position to refactor. DO NOT use this import tag in new code, in order to avoid the crawling horrors of getting UTF8 support subtly wrong. See the documentation for JSON for further details. encode_json This is the "encode_json" function provided by the selected implementation module, and takes a perl data structure which is serialised to JSON text. my $json_text = encode_json($data_structure); decode_json This is the "decode_json" function provided by the selected implementation module, and takes a string of JSON text to deserialise to a perl data structure. my $data_structure = decode_json($json_text); to_json, from_json See JSON for details. These are included to support legacy code only. JSON The "JSON" constant returns the selected implementation module's name for use as a class name - so: my $json_obj = JSON->new; # returns a Cpanel::JSON::XS or JSON::PP object and that object can then be used normally: my $data_structure = $json_obj->decode($json_text); # etc. is_bool $is_boolean = is_bool($scalar) Returns true if the passed scalar represents either "true" or "false", two constants that act like 1 and 0, respectively and are used to represent JSON "true" and "false" values in Perl. Since this is a bare sub in the various backend classes, it cannot be called as a class method like the other interfaces; it must be called as a function, with no invocant. It supports the representation used in all JSON backends. CONSTRUCTOR new With JSON::PP, JSON::XS and Cpanel::JSON::XS you are required to call mutators to set options, such as: my $json = $class->new->utf8(1)->pretty(1); Since this is a trifle irritating and noticeably un-perlish, we also offer: my $json = JSON::MaybeXS->new(utf8 => 1, pretty => 1); which works equivalently to the above (and in the usual tradition will accept a hashref instead of a hash, should you so desire). BOOLEANS To include JSON-aware booleans ("true", "false") in your data, just do: use JSON::MaybeXS; my $true = JSON->true; my $false = JSON->false; CAVEATS The "new()" method in this module is technically a factory, not a constructor, because the objects it returns will *NOT* be blessed into the "JSON::MaybeXS" class. If you are using an object returned by this module as a Moo(se) attribute, this type constraint code: is 'json' => ( isa => 'JSON::MaybeXS' ); will *NOT* do what you expect. Instead, either rely on the "JSON" class constant described above, as so: is 'json' => ( isa => JSON::MaybeXS::JSON() ); Alternatively, you can use duck typing: use Moose::Util::TypeConstraints 'duck_type'; is 'json' => ( isa => Object , duck_type([qw/ encode decode /])); AUTHOR mst - Matt S. Trout (cpan:MSTROUT) CONTRIBUTORS * Clinton Gormley * Karen Etheridge * Kieren Diment COPYRIGHT Copyright (c) 2013 the "JSON::MaybeXS" "AUTHOR" and "CONTRIBUTORS" as listed above. LICENSE This library is free software and may be distributed under the same terms as perl itself. JSON-MaybeXS-1.003005/lib/000700 000766 000024 00000000000 12503636114 015031 5ustar00etherstaff000000 000000 JSON-MaybeXS-1.003005/maint/000700 000766 000024 00000000000 12503636114 015373 5ustar00etherstaff000000 000000 JSON-MaybeXS-1.003005/t/000700 000766 000024 00000000000 12503636114 014526 5ustar00etherstaff000000 000000 JSON-MaybeXS-1.003005/xt/000700 000766 000024 00000000000 12503636114 014716 5ustar00etherstaff000000 000000 JSON-MaybeXS-1.003005/xt/json_pm.t000644 000766 000024 00000000702 12477001527 016565 0ustar00etherstaff000000 000000 use strict; use warnings; use Test::More; unless ( eval { require JSON; 1 } ) { plan skip_all => 'No JSON'; } my $data = JSON->new->decode('{"foo": true, "bar": false, "baz": 1}'); ok( JSON::is_bool($data->{foo}), 'JSON.pm: true decodes to a bool', ); ok( JSON::is_bool($data->{bar}), 'JSON.pm:: false decodes to a bool', ); ok( !JSON::is_bool($data->{baz}), 'JSON.pm: int does not decode to a bool', ); done_testing; JSON-MaybeXS-1.003005/xt/json_pm_legacy.t000644 000766 000024 00000002470 12477001527 020115 0ustar00etherstaff000000 000000 use warnings; use strict; use Test::More; # some tests to check JSON::PP versus JSON::MaybeXS for legacy methods unless ( eval { require JSON; 1 }) { plan skip_all => 'No JSON'; } use JSON::MaybeXS qw/:legacy/; use Encode; use utf8; my @hovercraft = ( 'My hovercraft is full of eels', 'Automjeti im është plot me ngjala', 'حَوّامتي مُمْتِلئة بِأَنْقَلَيْسون', ' Маё судна на паветранай падушцы поўна вуграмі', '我的氣墊船裝滿了鱔魚 ', 'Il mio hovercraft/aeroscivolante è pieno di anguille', 'សុទ្ធតែឣន្ចងពេញទូកហាះយើង ។', "Tá m'árthach foluaineach lán d'eascanna." ); foreach my $h (@hovercraft) { $h = '["' . $h . '"]'; my $j_perl = JSON::from_json($h); my $j_json = JSON::to_json($j_perl); my $h_enc = Encode::encode_utf8($h); my $j_perl_enc = JSON::from_json($h_enc); my $j_json_enc = JSON::to_json($j_perl_enc); my $jm_perl = from_json($h); my $jm_json = to_json($jm_perl); my $jm_perl_enc = from_json($h_enc); my $jm_json_enc = to_json($jm_perl_enc); is_deeply($j_perl, $jm_perl); is_deeply($j_perl_enc, $jm_perl_enc); is ($j_json, $jm_json); is ($j_json_enc, $jm_json_enc); } done_testing(); JSON-MaybeXS-1.003005/xt/json_pp_legacy.t000644 000766 000024 00000000141 12477001527 020111 0ustar00etherstaff000000 000000 use strict; use warnings; $ENV{PERL_JSON_BACKEND} = 'JSON::PP'; require 'xt/json_pm_legacy.t'; JSON-MaybeXS-1.003005/xt/json_xs_legacy.t000644 000766 000024 00000000141 12477001527 020124 0ustar00etherstaff000000 000000 use strict; use warnings; $ENV{PERL_JSON_BACKEND} = 'JSON::XS'; require 'xt/json_pm_legacy.t'; JSON-MaybeXS-1.003005/t/cpanel.t000644 000766 000024 00000000773 12477001527 016202 0ustar00etherstaff000000 000000 use strict; use warnings; use Test::More; use JSON::MaybeXS; unless ( eval { require Cpanel::JSON::XS; 1 } ) { plan skip_all => 'No Cpanel::JSON::XS'; } diag 'Using Cpanel::JSON::XS ', Cpanel::JSON::XS->VERSION; is( JSON, 'Cpanel::JSON::XS', 'Correct JSON class' ); is( \&encode_json, \&Cpanel::JSON::XS::encode_json, 'Correct encode_json function' ); is( \&decode_json, \&Cpanel::JSON::XS::decode_json, 'Correct encode_json function' ); require 't/lib/is_bool.pm'; done_testing; JSON-MaybeXS-1.003005/t/legacy.t000644 000766 000024 00000002166 12477001527 016202 0ustar00etherstaff000000 000000 use strict; use warnings; use Test::Without::Module 'Cpanel::JSON::XS'; use Test::More; use JSON::MaybeXS qw/:legacy/; my $in = '[1, 2, 3, 4]'; my $arr = from_json($in); my $j = to_json($arr); is($j, '[1,2,3,4]'); is(ref($arr), 'ARRAY'); my $json = 'JSON::MaybeXS'; diag "using invocant: $json"; like( do { eval { $json->from_json($in) }; $@ }, qr/from_json should not be called as a method/, 'blessed invocant detected in from_json', ); like( do { eval { $json->to_json($arr, { blah => 1 } ) }; $@ }, qr/to_json should not be called as a method/, 'blessed invocant detected in to_json', ); done_testing; __END__ to_json $json_text = to_json($perl_scalar) Converts the given Perl data structure to a json string. This function call is functionally identical to: $json_text = JSON->new->encode($perl_scalar) from_json $perl_scalar = from_json($json_text) The opposite of "to_json": expects a json string and tries to parse it, returning the resulting reference. This function call is functionally identical to: $perl_scalar = JSON->decode($json_text) JSON-MaybeXS-1.003005/t/lib/000700 000766 000024 00000000000 12503636114 015274 5ustar00etherstaff000000 000000 JSON-MaybeXS-1.003005/t/new.t000644 000766 000024 00000001017 12477001527 015521 0ustar00etherstaff000000 000000 use strict; use warnings; use Test::More; use JSON::MaybeXS (); our @call; sub Fake::new { bless({}, 'Fake') } sub Fake::foo { push @call, a => $_[1] } sub Fake::bar { push @call, c => $_[1] } { local $JSON::MaybeXS::JSON_Class = 'Fake'; my @args = (foo => 'b', bar => 'd'); foreach my $args (\@args, [ { @args } ]) { local @call; my $obj = JSON::MaybeXS->new(@$args); is(ref($obj), 'Fake', 'Object of correct class'); is(join(' ', sort @call), 'a b c d', 'Methods called'); } } done_testing; JSON-MaybeXS-1.003005/t/none.t000644 000766 000024 00000000660 12477001527 015672 0ustar00etherstaff000000 000000 use strict; use warnings; use Test::Without::Module 'Cpanel::JSON::XS'; use Test::Without::Module 'JSON::XS'; use Test::Without::Module 'JSON::PP'; use Test::More; ok(!eval { require JSON::MaybeXS; 1 }, 'Class failed to load'); # Test::Without::Module always causes 'did not return a true value' errors like( $@, qr{Cpanel/JSON/XS.pm did not.*JSON/XS.pm did not.*JSON/PP.pm did not}s, 'All errors reported' ); done_testing; JSON-MaybeXS-1.003005/t/pp.t000644 000766 000024 00000000763 12477001527 015356 0ustar00etherstaff000000 000000 use strict; use warnings; use Test::Without::Module 'Cpanel::JSON::XS', 'JSON::XS'; use if !eval { require JSON::PP; 1; }, 'Test::More', skip_all => 'No JSON::PP'; use Test::More; use JSON::MaybeXS; diag 'Using JSON::PP ', JSON::PP->VERSION; is(JSON, 'JSON::PP', 'Correct JSON class'); is( \&encode_json, \&JSON::PP::encode_json, 'Correct encode_json function' ); is( \&decode_json, \&JSON::PP::decode_json, 'Correct encode_json function' ); require 't/lib/is_bool.pm'; done_testing; JSON-MaybeXS-1.003005/t/preload_cpanel.t000644 000766 000024 00000000760 12477001527 017704 0ustar00etherstaff000000 000000 use strict; use warnings; use if !eval { require Cpanel::JSON::XS; 1; }, 'Test::More', skip_all => 'No Cpanel::JSON::XS'; use Test::More; use JSON::MaybeXS; diag 'Using Cpanel::JSON::XS ', Cpanel::JSON::XS->VERSION; is(JSON, 'Cpanel::JSON::XS', 'Correct JSON class'); is( \&encode_json, \&Cpanel::JSON::XS::encode_json, 'Correct encode_json function' ); is( \&decode_json, \&Cpanel::JSON::XS::decode_json, 'Correct encode_json function' ); require 't/lib/is_bool.pm'; done_testing; JSON-MaybeXS-1.003005/t/preload_xs.t000644 000766 000024 00000000662 12477001527 017075 0ustar00etherstaff000000 000000 use strict; use warnings; use if !eval { require JSON::XS; 1; }, 'Test::More', skip_all => 'No JSON::XS'; use Test::More; use JSON::MaybeXS; diag 'Using JSON::XS ', JSON::XS->VERSION; is( JSON, 'JSON::XS', 'Correct JSON class' ); is( \&encode_json, \&JSON::XS::encode_json, 'Correct encode_json function' ); is( \&decode_json, \&JSON::XS::decode_json, 'Correct encode_json function' ); require 't/lib/is_bool.pm'; done_testing; JSON-MaybeXS-1.003005/t/xs.t000644 000766 000024 00000000741 12477001527 015365 0ustar00etherstaff000000 000000 use strict; use warnings; use Test::Without::Module 'Cpanel::JSON::XS'; use Test::More; use JSON::MaybeXS; unless ( eval { require JSON::XS; 1 } ) { plan skip_all => 'No JSON::XS'; } diag 'Using JSON::XS ', JSON::XS->VERSION; is( JSON, 'JSON::XS', 'Correct JSON class' ); is( \&encode_json, \&JSON::XS::encode_json, 'Correct encode_json function' ); is( \&decode_json, \&JSON::XS::decode_json, 'Correct encode_json function' ); require 't/lib/is_bool.pm'; done_testing; JSON-MaybeXS-1.003005/t/lib/is_bool.pm000644 000766 000024 00000000774 12477001527 017306 0ustar00etherstaff000000 000000 use strict; use warnings; use Test::More; use JSON::MaybeXS; my $data = JSON::MaybeXS->new->decode('{"foo": true, "bar": false, "baz": 1}'); diag 'true is: ', explain $data->{foo}; diag 'false is: ', explain $data->{bar}; ok( JSON::MaybeXS::is_bool($data->{foo}), JSON() . ': true decodes to a bool', ); ok( JSON::MaybeXS::is_bool($data->{bar}), JSON() . ': false decodes to a bool', ); ok( !JSON::MaybeXS::is_bool($data->{baz}), JSON() . ': int does not decode to a bool', ); 1; JSON-MaybeXS-1.003005/maint/Makefile.PL.include000644 000766 000024 00000000304 12474221000 020765 0ustar00etherstaff000000 000000 BEGIN { -e 'Distar' or system("git clone git://git.shadowcat.co.uk/p5sagit/Distar.git") } use lib 'Distar/lib'; use Distar; author 'mst - Matt S. Trout (cpan:MSTROUT) '; 1; JSON-MaybeXS-1.003005/lib/JSON/000700 000766 000024 00000000000 12503636114 015602 5ustar00etherstaff000000 000000 JSON-MaybeXS-1.003005/lib/JSON/MaybeXS.pm000644 000766 000024 00000016402 12503635737 017477 0ustar00etherstaff000000 000000 package JSON::MaybeXS; use strict; use warnings FATAL => 'all'; use base qw(Exporter); our $VERSION = '1.003005'; $VERSION = eval $VERSION; sub _choose_json_module { return 'Cpanel::JSON::XS' if $INC{'Cpanel/JSON/XS.pm'}; return 'JSON::XS' if $INC{'JSON/XS.pm'}; my @err; return 'Cpanel::JSON::XS' if eval { require Cpanel::JSON::XS; 1; }; push @err, "Error loading Cpanel::JSON::XS: $@"; return 'JSON::XS' if eval { require JSON::XS; 1; }; push @err, "Error loading JSON::XS: $@"; return 'JSON::PP' if eval { require JSON::PP; 1 }; push @err, "Error loading JSON::PP: $@"; die join( "\n", "Couldn't load a JSON module:", @err ); } BEGIN { our $JSON_Class = _choose_json_module(); $JSON_Class->import(qw(encode_json decode_json)); } our @EXPORT = qw(encode_json decode_json JSON); my @EXPORT_ALL = qw(is_bool); our @EXPORT_OK = qw(is_bool to_json from_json); our %EXPORT_TAGS = ( all => [ @EXPORT, @EXPORT_ALL ], legacy => [ @EXPORT, @EXPORT_OK ], ); sub JSON () { our $JSON_Class } sub new { shift; my %args = @_ == 1 ? %{$_[0]} : @_; my $new = (our $JSON_Class)->new; $new->$_($args{$_}) for keys %args; return $new; } use Scalar::Util (); sub is_bool { die 'is_bool is not a method' if $_[1]; Scalar::Util::blessed($_[0]) and ($_[0]->isa('JSON::XS::Boolean') or $_[0]->isa('Cpanel::JSON::XS::Boolean') or $_[0]->isa('JSON::PP::Boolean')); } # (mostly) CopyPasta from JSON.pm version 2.90 use Carp (); sub from_json ($@) { if ( ref($_[0]) =~ /^JSON/ or $_[0] =~ /^JSON/ ) { Carp::croak "from_json should not be called as a method."; } my $json = JSON()->new; if (@_ == 2 and ref $_[1] eq 'HASH') { my $opt = $_[1]; for my $method (keys %$opt) { $json->$method( $opt->{$method} ); } } return $json->decode( $_[0] ); } sub to_json ($@) { if ( ref($_[0]) =~ /^JSON/ or (@_ > 2 and $_[0] =~ /^JSON/) ) { Carp::croak "to_json should not be called as a method."; } my $json = JSON()->new; if (@_ == 2 and ref $_[1] eq 'HASH') { my $opt = $_[1]; for my $method (keys %$opt) { $json->$method( $opt->{$method} ); } } $json->encode($_[0]); } 1; =head1 NAME JSON::MaybeXS - Use L with a fallback to L and L =head1 SYNOPSIS use JSON::MaybeXS; my $data_structure = decode_json($json_input); my $json_output = encode_json($data_structure); my $json = JSON->new; my $json_with_args = JSON::MaybeXS->new(utf8 => 1); # or { utf8 => 1 } =head1 DESCRIPTION This module first checks to see if either L or L is already loaded, in which case it uses that module. Otherwise it tries to load L, then L, then L in order, and either uses the first module it finds or throws an error. It then exports the C and C functions from the loaded module, along with a C constant that returns the class name for calling C on. If you're writing fresh code rather than replacing L usage, you might want to pass options as constructor args rather than calling mutators, so we provide our own C method that supports that. =head1 EXPORTS C, C and C are exported by default; C is exported on request. To import only some symbols, specify them on the C line: use JSON::MaybeXS qw(encode_json decode_json is_bool); # functions only use JSON::MaybeXS qw(JSON); # JSON constant only To import all available sensible symbols (C, C, and C), use C<:all>: use JSON::MaybeXS ':all'; To import all symbols including those needed by legacy apps that use L: use JSON::MaybeXS ':legacy'; This imports the C and C symbols as well as everything in C<:all>. NOTE: This is to support legacy code that makes extensive use of C and C which you are not yet in a position to refactor. DO NOT use this import tag in new code, in order to avoid the crawling horrors of getting UTF8 support subtly wrong. See the documentation for L for further details. =head2 encode_json This is the C function provided by the selected implementation module, and takes a perl data structure which is serialised to JSON text. my $json_text = encode_json($data_structure); =head2 decode_json This is the C function provided by the selected implementation module, and takes a string of JSON text to deserialise to a perl data structure. my $data_structure = decode_json($json_text); =head2 to_json, from_json See L for details. These are included to support legacy code B. =head2 JSON The C constant returns the selected implementation module's name for use as a class name - so: my $json_obj = JSON->new; # returns a Cpanel::JSON::XS or JSON::PP object and that object can then be used normally: my $data_structure = $json_obj->decode($json_text); # etc. =head2 is_bool $is_boolean = is_bool($scalar) Returns true if the passed scalar represents either C or C, two constants that act like C<1> and C<0>, respectively and are used to represent JSON C and C values in Perl. Since this is a bare sub in the various backend classes, it cannot be called as a class method like the other interfaces; it must be called as a function, with no invocant. It supports the representation used in all JSON backends. =head1 CONSTRUCTOR =head2 new With L, L and L you are required to call mutators to set options, such as: my $json = $class->new->utf8(1)->pretty(1); Since this is a trifle irritating and noticeably un-perlish, we also offer: my $json = JSON::MaybeXS->new(utf8 => 1, pretty => 1); which works equivalently to the above (and in the usual tradition will accept a hashref instead of a hash, should you so desire). =head1 BOOLEANS To include JSON-aware booleans (C, C) in your data, just do: use JSON::MaybeXS; my $true = JSON->true; my $false = JSON->false; =head1 CAVEATS The C method in this module is technically a factory, not a constructor, because the objects it returns will I be blessed into the C class. If you are using an object returned by this module as a Moo(se) attribute, this type constraint code: is 'json' => ( isa => 'JSON::MaybeXS' ); will I do what you expect. Instead, either rely on the C class constant described above, as so: is 'json' => ( isa => JSON::MaybeXS::JSON() ); Alternatively, you can use duck typing: use Moose::Util::TypeConstraints 'duck_type'; is 'json' => ( isa => Object , duck_type([qw/ encode decode /])); =head1 AUTHOR mst - Matt S. Trout (cpan:MSTROUT) =head1 CONTRIBUTORS =over 4 =item * Clinton Gormley =item * Karen Etheridge =item * Kieren Diment =back =head1 COPYRIGHT Copyright (c) 2013 the C L and L as listed above. =head1 LICENSE This library is free software and may be distributed under the same terms as perl itself. =cut