JSON-MaybeXS-1.001000/0000755000372100001440000000000012251743230013565 5ustar matthewtusersJSON-MaybeXS-1.001000/lib/0000755000372100001440000000000012251743230014333 5ustar matthewtusersJSON-MaybeXS-1.001000/lib/JSON/0000755000372100001440000000000012251743230015104 5ustar matthewtusersJSON-MaybeXS-1.001000/lib/JSON/MaybeXS.pm0000644000372100001440000000666212251743135016770 0ustar matthewtuserspackage JSON::MaybeXS; use strict; use warnings FATAL => 'all'; use base qw(Exporter); our $VERSION = '1.001000'; BEGIN { our $JSON_Class; our @err; if (eval { require Cpanel::JSON::XS; 1; }) { $JSON_Class = 'Cpanel::JSON::XS'; } else { push @err, "Error loading Cpanel::JSON::XS: $@"; if (eval { require JSON::PP; 1; }) { $JSON_Class = 'JSON::PP'; } else { push @err, "Error loading JSON::PP: $@"; } } unless ($JSON_Class) { die join("\n", "Couldn't load a JSON module:", @err); } $JSON_Class->import(qw(encode_json decode_json)); } our @EXPORT = qw(encode_json decode_json JSON); 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; } 1; =head1 NAME JSON::MaybeXS - use L with a fallback to 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 tries to load L, and if that fails instead tries to load L. If neither is available, an exception will be thrown. 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 JSON.pm 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 All of C, C and C are exported by default. To import only some symbols, specify them on the C line: use JSON::MaybeXS qw(encode_json decode_json); # functions only use JSON::MaybeXS qw(JSON); # JSON constant only =head2 encode_json This is the C function provided by the selected implementation module, and takes a perl data stucture 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 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. =head1 CONSTRUCTOR =head2 new With L and L you are required to call mutators to set options, i.e. 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 AUTHOR mst - Matt S. Trout (cpan:MSTROUT) =head1 CONTRIBUTORS None yet. Well volunteered? :) =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 JSON-MaybeXS-1.001000/META.yml0000644000372100001440000000120612251743230015035 0ustar matthewtusers--- abstract: 'use L with a fallback to L' author: - 'mst - Matt S. Trout (cpan:MSTROUT) ' build_requires: Test::More: 0.88 Test::Without::Module: 0.17 configure_requires: ExtUtils::MakeMaker: 0 dynamic_config: 1 generated_by: 'ExtUtils::MakeMaker version 6.66, CPAN::Meta::Converter version 2.131560' 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 requires: Cpanel::JSON::XS: 2.3310 JSON::PP: 2.27202 Test::More: 0.88 Test::Without::Module: 0.17 version: 1.001000 JSON-MaybeXS-1.001000/META.json0000644000372100001440000000211212251743230015202 0ustar matthewtusers{ "abstract" : "use L with a fallback to L", "author" : [ "mst - Matt S. Trout (cpan:MSTROUT) " ], "dynamic_config" : 1, "generated_by" : "ExtUtils::MakeMaker version 6.66, CPAN::Meta::Converter version 2.131560", "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" : { "build" : { "requires" : { "Test::More" : "0.88", "Test::Without::Module" : "0.17" } }, "configure" : { "requires" : { "ExtUtils::MakeMaker" : "0" } }, "runtime" : { "requires" : { "Cpanel::JSON::XS" : "2.3310", "JSON::PP" : "2.27202", "Test::More" : "0.88", "Test::Without::Module" : "0.17" } } }, "release_status" : "stable", "version" : "1.001000" } JSON-MaybeXS-1.001000/Makefile.PL0000644000372100001440000000503012251741613015540 0ustar matthewtusersuse strict; use warnings FATAL => 'all'; use 5.008001; use ExtUtils::MakeMaker; (do 'maint/Makefile.PL.include' or die $@) unless -f 'META.yml'; my %BUILD_DEPS = ( 'Test::Without::Module' => '0.17', 'Test::More' => '0.88' ); WriteMakefile( NAME => 'JSON::MaybeXS', VERSION_FROM => 'lib/JSON/MaybeXS.pm', PREREQ_PM => { 'JSON::PP' => '2.27202', (can_xs() ? ('Cpanel::JSON::XS' => '2.3310') : ()), %BUILD_DEPS, }, BUILD_REQUIRES => \%BUILD_DEPS, ); # 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.001000/README0000644000372100001440000000524712251743230014455 0ustar matthewtusersNAME JSON::MaybeXS - use Cpanel::JSON::XS with a fallback to 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 tries to load Cpanel::JSON::XS, and if that fails instead tries to load JSON::PP. If neither is available, an exception will be thrown. 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 All of "encode_json", "decode_json" and "JSON" are exported by default. To import only some symbols, specify them on the "use" line: use JSON::MaybeXS qw(encode_json decode_json); # functions only use JSON::MaybeXS qw(JSON); # JSON constant only encode_json This is the "encode_json" function provided by the selected implementation module, and takes a perl data stucture 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); 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. CONSTRUCTOR new With JSON::PP and Cpanel::JSON::XS you are required to call mutators to set options, i.e. 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). AUTHOR mst - Matt S. Trout (cpan:MSTROUT) CONTRIBUTORS None yet. Well volunteered? :) 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.001000/maint/0000755000372100001440000000000012251743230014675 5ustar matthewtusersJSON-MaybeXS-1.001000/maint/Makefile.PL.include0000644000372100001440000000030412251741613020271 0ustar matthewtusersBEGIN { -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.001000/MANIFEST0000644000372100001440000000056112251743230014720 0ustar matthewtusersChanges lib/JSON/MaybeXS.pm maint/Makefile.PL.include Makefile.PL MANIFEST This list of files t/neither.t t/new.t t/pp.t t/xs.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.001000/t/0000755000372100001440000000000012251743230014030 5ustar matthewtusersJSON-MaybeXS-1.001000/t/neither.t0000644000372100001440000000060612251741613015660 0ustar matthewtusersuse strict; use warnings FATAL => 'all'; use Test::Without::Module 'Cpanel::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/PP.pm did not}s, 'Both errors reported' ); done_testing; JSON-MaybeXS-1.001000/t/pp.t0000644000372100001440000000065212251741613014642 0ustar matthewtusersuse strict; use warnings FATAL => 'all'; use Test::Without::Module 'Cpanel::JSON::XS'; use if !do { require JSON::PP; 1; }, 'Test::More', skip_all => 'No JSON::PP'; use Test::More; use JSON::MaybeXS; 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' ); done_testing; JSON-MaybeXS-1.001000/t/xs.t0000644000372100001440000000064412251741613014656 0ustar matthewtusersuse strict; use warnings FATAL => 'all'; use if !do { require Cpanel::JSON::XS; 1; }, 'Test::More', skip_all => 'No Cpanel::JSON::XS'; use Test::More; use JSON::MaybeXS; 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' ); done_testing; JSON-MaybeXS-1.001000/t/new.t0000644000372100001440000000103612251742524015013 0ustar matthewtusersuse strict; use warnings FATAL => 'all'; 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.001000/Changes0000644000372100001440000000024212251743177015070 0ustar matthewtusers1.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.