Skip to content

Commit bdd6821

Browse files
remove use of JSON::MaybeXS
JSON::MaybeXS can still bring in JSON::XS, which is incompatible with some of our uses (e.g. it doesn't support allow_bignum). Instead piggyback on Mojo::JSON's heuristic for determining the backend.
1 parent 0ff0286 commit bdd6821

File tree

8 files changed

+20
-10
lines changed

8 files changed

+20
-10
lines changed

Changes

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
Revision history for JSON-Schema-Tiny
22

33
{{$NEXT}}
4+
- remove use of JSON::MaybeXS, to avoid potential use of JSON::XS;
5+
now we use Cpanel::JSON::XS or JSON::PP directly, using the same
6+
environment variables as in Mojo::JSON for customization.
47

58
0.022 2023-12-17 00:42:56Z
69
- new $STRINGY_NUMBERS option, for validating numbers more loosely

lib/JSON/Schema/Tiny.pm

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use Mojo::URL;
2020
use Mojo::JSON::Pointer;
2121
use Carp qw(croak carp);
2222
use Storable 'dclone';
23-
use JSON::MaybeXS 1.004001 'is_bool';
23+
use Mojo::JSON (); # for JSON_XS, MOJO_NO_JSON_XS environment variables
2424
use Feature::Compat::Try;
2525
use JSON::PP ();
2626
use List::Util 1.33 qw(any none);
@@ -1103,7 +1103,15 @@ sub get_type ($value) {
11031103
if !($flags & B::SVf_POK) && ($flags & (B::SVf_IOK | B::SVf_NOK));
11041104

11051105
croak sprintf('ambiguous type for %s',
1106-
JSON::MaybeXS->new(allow_nonref => 1, canonical => 1, utf8 => 0)->encode($value));
1106+
(Mojo::JSON::JSON_XS ? 'Cpanel::JSON::XS' : 'JSON::PP')->new->allow_nonref(1)->canonical(1)->utf8(0)->encode($value));
1107+
}
1108+
1109+
# lifted from JSON::MaybeXS
1110+
sub is_bool ($value) {
1111+
Scalar::Util::blessed($value)
1112+
and ($value->isa('JSON::PP::Boolean')
1113+
or $value->isa('Cpanel::JSON::XS::Boolean')
1114+
or $value->isa('JSON::XS::Boolean'));
11071115
}
11081116

11091117
# compares two arbitrary data payloads for equality, as per
@@ -1314,7 +1322,7 @@ validator, supporting the most popular keywords.
13141322
13151323
=head1 FUNCTIONS
13161324
1317-
=for Pod::Coverage is_type get_type is_equal is_elements_unique jsonp canonical_uri E abort
1325+
=for Pod::Coverage is_type get_type is_bool is_equal is_elements_unique jsonp canonical_uri E abort
13181326
assert_keyword_type assert_pattern assert_uri assert_non_negative_integer assert_array_schemas
13191327
new assert_uri_reference sprintf_num
13201328

t/additional-tests-draft2019-09.t

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use Test::Deep;
1515
use lib 't/lib';
1616
use Acceptance;
1717
use Path::Tiny;
18-
use JSON::MaybeXS;
18+
use Mojo::JSON 'decode_json';
1919

2020
my $version = 'draft2019-09';
2121

t/additional-tests-draft2020-12.t

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use Test::Deep;
1515
use lib 't/lib';
1616
use Acceptance;
1717
use Path::Tiny;
18-
use JSON::MaybeXS;
18+
use Mojo::JSON 'decode_json';
1919

2020
my $version = 'draft2020-12';
2121

t/equality.t

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ subtest 'equality, using inflated data' => sub {
6262
}
6363
};
6464

65-
my $decoder = JSON::MaybeXS->new(allow_nonref => 1, utf8 => 0);
65+
my $decoder = (Mojo::JSON::JSON_XS ? 'Cpanel::JSON::XS' : 'JSON::PP')->new->allow_nonref(1)->utf8(0);
6666

6767
subtest 'equality, using JSON strings' => sub {
6868
foreach my $test (

t/lib/Acceptance.pm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ sub acceptance_tests (%options) {
4040
my $js = JSON::Schema::Tiny->new($options{evaluator}->%*);
4141
my $js_short_circuit = $ENV{NO_SHORT_CIRCUIT} || JSON::Schema::Tiny->new($options{evaluator}->%*, short_circuit => 1);
4242

43-
my $encoder = JSON::MaybeXS->new(allow_nonref => 1, utf8 => 0, canonical => 1, pretty => 1);
43+
my $encoder = (Mojo::JSON::JSON_XS ? 'Cpanel::JSON::XS' : 'JSON::PP')->new->allow_nonref(1)->utf8(0)->canonical(1)->pretty(1);
4444
$encoder->indent_length(2) if $encoder->can('indent_length');
4545

4646
$accepter->acceptance(

t/lib/Helper.pm

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ use open ':std', ':encoding(UTF-8)'; # force stdin, stdout, stderr into utf8
1414
use JSON::PP ();
1515
use constant { true => JSON::PP::true, false => JSON::PP::false };
1616

17-
use JSON::MaybeXS;
18-
my $encoder = JSON::MaybeXS->new(allow_nonref => 1, utf8 => 0);
17+
my $encoder = (Mojo::JSON::JSON_XS ? 'Cpanel::JSON::XS' : 'JSON::PP')->new->allow_nonref(1)->utf8(0);
1918

2019
# like sprintf, but all list items are JSON-encoded. assumes placeholders are %s!
2120
sub json_sprintf {

t/type.t

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ foreach my $type (sort keys %inflated_data) {
6363
};
6464
}
6565

66-
my $decoder = JSON::MaybeXS->new(allow_nonref => 1, canonical => 1, utf8 => 1, allow_bignum => 1);
66+
my $decoder = (Mojo::JSON::JSON_XS ? 'Cpanel::JSON::XS' : 'JSON::PP')->new->allow_nonref(1)->canonical(1)->utf8(1)->allow_bignum(1);
6767

6868
foreach my $type (sort keys %json_data) {
6969
subtest 'JSON-encoded data, type: '.$type => sub {

0 commit comments

Comments
 (0)