- 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
},
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
}
our @EXPORT = qw(encode_json decode_json JSON);
+our @EXPORT_OK = qw(is_bool);
sub JSON () { our $JSON_Class }
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
=head1 EXPORTS
-All of C<encode_json>, C<decode_json> and C<JSON> are exported by default.
+C<encode_json>, C<decode_json> and C<JSON> are exported by default; C<is_bool>
+is exported on request.
To import only some symbols, specify them on the C<use> 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
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<true> or
+C<false>, two constants that act like C<1> and C<0>, respectively
+and are used to represent JSON C<true> and C<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.
+
=head1 CONSTRUCTOR
=head2 new
'Correct encode_json function'
);
+require 't/lib/is_bool.pm';
+
done_testing;
--- /dev/null
+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;
'Correct encode_json function'
);
+require 't/lib/is_bool.pm';
+
done_testing;
'Correct encode_json function'
);
+require 't/lib/is_bool.pm';
+
done_testing;
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;
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;
--- /dev/null
+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;