Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support encoding core booleans #62

Merged
merged 1 commit into from
Apr 3, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 }
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It there a reason for not making these two functions return native bools?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See #63. It would be unwise to use native bools unless the application is equipped to deal with them everywhere.

Isn't !!0 a native bool (in perl versions where they exist)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

!!0 is a native bool, and both is_bool and isa should be returning native bools.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or, if you were referring to the true and false functions, that is because many things expect the bools coming from JSON::PP to be JSON::PP::Boolean objects. For now, I've left those unchanged for backwards compatibility. Also, this PR is only meant to deal with encoding. #63 exists to start dealing with decoding.

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