From: Karen Etheridge Date: Fri, 10 Oct 2014 05:47:08 +0000 (-0700) Subject: is_bool() X-Git-Tag: v1.002004~3 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=1ca3b561cb6fd6381767d8df4564eeff4de7dc98;p=p5sagit%2FJSON-MaybeXS.git is_bool() --- diff --git a/Changes b/Changes index 7404d56..4262114 100644 --- a/Changes +++ b/Changes @@ -2,6 +2,7 @@ Revision history for JSON-MaybeXS - 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 diff --git a/Makefile.PL b/Makefile.PL index 2982b81..8b45eb5 100644 --- a/Makefile.PL +++ b/Makefile.PL @@ -35,6 +35,7 @@ my %WriteMakefileArgs = ( }, runtime => { requires => { + 'Safe::Isa' => '0', 'JSON::PP' => '2.27202', # we may also add a runtime prereq for Cpanel::JSON::XS, on the # installer's machine diff --git a/lib/JSON/MaybeXS.pm b/lib/JSON/MaybeXS.pm index 30ca083..d29dae6 100644 --- a/lib/JSON/MaybeXS.pm +++ b/lib/JSON/MaybeXS.pm @@ -31,6 +31,7 @@ BEGIN { } our @EXPORT = qw(encode_json decode_json JSON); +our @EXPORT_OK = qw(is_bool); sub JSON () { our $JSON_Class } @@ -42,6 +43,15 @@ sub new { return $new; } +use Safe::Isa; + +sub is_bool { + die 'is_bool is not a method' if $_[1]; + + $_[0]->$_isa('JSON::XS::Boolean') + or $_[0]->$_isa('JSON::PP::Boolean'); +} + 1; =head1 NAME @@ -77,11 +87,12 @@ we provide our own C method that supports that. =head1 EXPORTS -All of C, C and C are exported by default. +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); # functions only + use JSON::MaybeXS qw(encode_json decode_json is_bool); # functions only use JSON::MaybeXS qw(JSON); # JSON constant only @@ -110,6 +121,18 @@ 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 diff --git a/t/cpanel.t b/t/cpanel.t index 5db64ec..8bf0383 100644 --- a/t/cpanel.t +++ b/t/cpanel.t @@ -21,4 +21,6 @@ is( \&decode_json, 'Correct encode_json function' ); +require 't/lib/is_bool.pm'; + done_testing; diff --git a/t/lib/is_bool.pm b/t/lib/is_bool.pm new file mode 100644 index 0000000..a2fea95 --- /dev/null +++ b/t/lib/is_bool.pm @@ -0,0 +1,21 @@ +use strict; +use warnings; + +use Test::More; +use JSON::MaybeXS; + +my $data = JSON::MaybeXS->new->decode('{"foo": true, "bar": false, "baz": 1}'); +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; diff --git a/t/pp.t b/t/pp.t index 6cef8af..9d1bf06 100644 --- a/t/pp.t +++ b/t/pp.t @@ -17,4 +17,6 @@ is( 'Correct encode_json function' ); +require 't/lib/is_bool.pm'; + done_testing; diff --git a/t/preload_cpanel.t b/t/preload_cpanel.t index f3a4cef..b7007d8 100644 --- a/t/preload_cpanel.t +++ b/t/preload_cpanel.t @@ -16,4 +16,6 @@ is( 'Correct encode_json function' ); +require 't/lib/is_bool.pm'; + done_testing; diff --git a/t/preload_xs.t b/t/preload_xs.t index 01f14bc..f5c94b6 100644 --- a/t/preload_xs.t +++ b/t/preload_xs.t @@ -9,4 +9,6 @@ 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; diff --git a/t/xs.t b/t/xs.t index 41ffcae..ed7804a 100644 --- a/t/xs.t +++ b/t/xs.t @@ -16,4 +16,6 @@ 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; diff --git a/xt/json_pm.t b/xt/json_pm.t new file mode 100644 index 0000000..3a0891d --- /dev/null +++ b/xt/json_pm.t @@ -0,0 +1,24 @@ +use strict; +use warnings FATAL => 'all'; +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;