Skip to content

Commit

Permalink
support encoding core booleans
Browse files Browse the repository at this point in the history
Perl now has support for tracking boolean values. Use this to encode
perl booleans as json booleans.
  • Loading branch information
haarg committed Apr 1, 2022
1 parent 3173bc2 commit d35f6d7
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions lib/JSON/PP.pm
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ use constant P_ALLOW_TAGS => 19;

use constant OLD_PERL => $] < 5.008 ? 1 : 0;
use constant USE_B => $ENV{PERL_JSON_PP_USE_B} || 0;
use constant CORE_BOOL => defined &builtin::is_bool;

BEGIN {
if (USE_B) {
Expand Down Expand Up @@ -476,7 +477,11 @@ sub allow_bigint {
my $type = ref($value);

if (!$type) {
if (_looks_like_number($value)) {
BEGIN { CORE_BOOL and warnings->unimport('experimental::builtin') }
if (CORE_BOOL && builtin::is_bool($value)) {
return $value ? 'true' : 'false';
}
elsif (_looks_like_number($value)) {
return $value;
}
return $self->string_to_json($value);
Expand Down Expand Up @@ -1493,7 +1498,20 @@ BEGIN {
$JSON::PP::true = do { bless \(my $dummy = 1), "JSON::PP::Boolean" };
$JSON::PP::false = do { bless \(my $dummy = 0), "JSON::PP::Boolean" };

sub is_bool { blessed $_[0] and ( $_[0]->isa("JSON::PP::Boolean") or $_[0]->isa("Types::Serialiser::BooleanBase") or $_[0]->isa("JSON::XS::Boolean") ); }
sub is_bool {
if (blessed $_[0]) {
return (
$_[0]->isa("JSON::PP::Boolean")
or $_[0]->isa("Types::Serialiser::BooleanBase")
or $_[0]->isa("JSON::XS::Boolean")
);
}
elsif (CORE_BOOL) {
BEGIN { CORE_BOOL and warnings->unimport('experimental::builtin') }
return builtin::is_bool($_[0]);
}
return !!0;
}

sub true { $JSON::PP::true }
sub false { $JSON::PP::false }
Expand Down

0 comments on commit d35f6d7

Please sign in to comment.