diff options
author | TizenOpenSource <tizenopensrc@samsung.com> | 2023-12-28 17:46:21 +0900 |
---|---|---|
committer | TizenOpenSource <tizenopensrc@samsung.com> | 2023-12-28 17:46:21 +0900 |
commit | 782d8bd355c14b4617bde530e43d31b89ec298f5 (patch) | |
tree | 8c4fb6771f3e13e1e98543e5bf179c989659d5b9 /lib/JSON | |
parent | 486774dd555cba55fb31657143ea9cc9ad64a561 (diff) | |
download | perl-json-782d8bd355c14b4617bde530e43d31b89ec298f5.tar.gz perl-json-782d8bd355c14b4617bde530e43d31b89ec298f5.tar.bz2 perl-json-782d8bd355c14b4617bde530e43d31b89ec298f5.zip |
Imported Upstream version 4.10upstream/4.10upstream
Diffstat (limited to 'lib/JSON')
-rw-r--r-- | lib/JSON/backportPP.pm | 84 | ||||
-rw-r--r-- | lib/JSON/backportPP/Boolean.pm | 3 |
2 files changed, 83 insertions, 4 deletions
diff --git a/lib/JSON/backportPP.pm b/lib/JSON/backportPP.pm index 1bec128..6870697 100644 --- a/lib/JSON/backportPP.pm +++ b/lib/JSON/backportPP.pm @@ -15,7 +15,7 @@ use JSON::backportPP::Boolean; use Carp (); #use Devel::Peek; -$JSON::backportPP::VERSION = '4.10'; +$JSON::backportPP::VERSION = '4.12'; @JSON::PP::EXPORT = qw(encode_json decode_json from_json to_json); @@ -47,6 +47,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; my $invalid_char_re; @@ -213,13 +214,54 @@ sub boolean_values { my ($false, $true) = @_; $self->{false} = $false; $self->{true} = $true; + if (CORE_BOOL) { + BEGIN { CORE_BOOL and warnings->unimport(qw(experimental::builtin)) } + if (builtin::is_bool($true) && builtin::is_bool($false) && $true && !$false) { + $self->{core_bools} = !!1; + } + else { + delete $self->{core_bools}; + } + } } else { delete $self->{false}; delete $self->{true}; + delete $self->{core_bools}; } return $self; } +sub core_bools { + my $self = shift; + my $core_bools = defined $_[0] ? $_[0] : 1; + if ($core_bools) { + $self->{true} = !!1; + $self->{false} = !!0; + $self->{core_bools} = !!1; + } + else { + $self->{true} = $JSON::PP::true; + $self->{false} = $JSON::PP::false; + $self->{core_bools} = !!0; + } + return $self; +} + +sub get_core_bools { + my $self = shift; + return !!$self->{core_bools}; +} + +sub unblessed_bool { + my $self = shift; + return $self->core_bools(@_); +} + +sub get_unblessed_bool { + my $self = shift; + return $self->get_core_bools(@_); +} + sub get_boolean_values { my $self = shift; if (exists $self->{true} and exists $self->{false}) { @@ -480,7 +522,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); @@ -1526,7 +1572,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 } @@ -1865,6 +1924,9 @@ Returns true if the passed scalar represents either JSON::PP::true or JSON::PP::false, two constants that act like C<1> and C<0> respectively and are also used to represent JSON C<true> and C<false> in Perl strings. +On perl 5.36 and above, will also return true when given one of perl's +standard boolean values, such as the result of a comparison. + See L<MAPPING>, below, for more information on how JSON values are mapped to Perl. @@ -2281,6 +2343,22 @@ to their default values. C<get_boolean_values> will return both C<$false> and C<$true> values, or the empty list when they are set to the default. +=head2 core_bools + + $json->core_bools([$enable]); + +If C<$enable> is true (or missing), then C<decode>, will produce standard +perl boolean values. Equivalent to calling: + + $json->boolean_values(!!1, !!0) + +C<get_core_bools> will return true if this has been set. On perl 5.36, it will +also return true if the boolean values have been set to perl's core booleans +using the C<boolean_values> method. + +The methods C<unblessed_bool> and C<get_unblessed_bool> are provided as aliases +for compatibility with L<Cpanel::JSON::XS>. + =head2 filter_json_object $json = $json->filter_json_object([$coderef]) diff --git a/lib/JSON/backportPP/Boolean.pm b/lib/JSON/backportPP/Boolean.pm index 30cd6b9..08228b1 100644 --- a/lib/JSON/backportPP/Boolean.pm +++ b/lib/JSON/backportPP/Boolean.pm @@ -4,6 +4,7 @@ package # This is JSON::backportPP use strict; require overload; local $^W; +overload::unimport('overload', qw(0+ ++ -- fallback)); overload::import('overload', "0+" => sub { ${$_[0]} }, "++" => sub { $_[0] = ${$_[0]} + 1 }, @@ -11,7 +12,7 @@ overload::import('overload', fallback => 1, ); -$JSON::backportPP::Boolean::VERSION = '4.10'; +$JSON::backportPP::Boolean::VERSION = '4.12'; 1; |