From: Graham Knop Date: Sun, 28 Jan 2018 02:13:28 +0000 (+0100) Subject: make true/false available as subs or methods on MaybeXS itself X-Git-Tag: v1.004000~3 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=048d1726ed9ead4981a9d2ec10253e80a25ee2b5;p=p5sagit%2FJSON-MaybeXS.git make true/false available as subs or methods on MaybeXS itself --- diff --git a/lib/JSON/MaybeXS.pm b/lib/JSON/MaybeXS.pm index eee84f2..ff0a580 100644 --- a/lib/JSON/MaybeXS.pm +++ b/lib/JSON/MaybeXS.pm @@ -29,6 +29,9 @@ sub _choose_json_module { BEGIN { our $JSON_Class = _choose_json_module(); $JSON_Class->import(qw(encode_json decode_json)); + no strict 'refs'; + *$_ = $JSON_Class->can($_) + for qw(true false); } our @EXPORT = qw(encode_json decode_json JSON); @@ -229,6 +232,14 @@ To include JSON-aware booleans (C, C) in your data, just do: my $true = JSON()->true; my $false = JSON()->false; +The booleans are also available as subs or methods on JSON::MaybeXS. + + use JSON::MaybeXS (); + my $true = JSON::MaybeXS::true; + my $true = JSON::MaybeXS->true; + my $false = JSON::MaybeXS::false; + my $false = JSON::MaybeXS->false; + =head1 CONVERTING FROM JSON::Any L used to be the favoured compatibility layer above the various diff --git a/t/lib/is_bool.pm b/t/lib/is_bool.pm index 28fd0cf..333b0e3 100644 --- a/t/lib/is_bool.pm +++ b/t/lib/is_bool.pm @@ -21,4 +21,28 @@ ok( JSON() . ': int does not decode to a bool', ); +is( + JSON::MaybeXS::encode_json([JSON::MaybeXS::true]), + '[true]', + JSON() . ': true sub encodes as correct boolean', +); + +is( + JSON::MaybeXS::encode_json([JSON::MaybeXS->true]), + '[true]', + JSON() . ': true method encodes as correct boolean', +); + +is( + JSON::MaybeXS::encode_json([JSON::MaybeXS::false]), + '[false]', + JSON() . ': false sub encodes as correct boolean', +); + +is( + JSON::MaybeXS::encode_json([JSON::MaybeXS->false]), + '[false]', + JSON() . ': false method encodes as correct boolean', +); + 1;