JSON-PP

 view release on metacpan or  search on metacpan

Changes  view on Meta::CPAN

4.08 2022-04-10
    - remove unneeded utf8::upgrade and downgrade (GH#59, FGasper++)
    - core boolean support (GH#62, 63, haarg++)
    - EBCDIC support (GH#64, khwilliamson++)
    - shorten a test name (GH#65, khwilliamson)

4.07 2021-12-17
    - fix incr_parse($unicode_str) bug (GH#56, DabeDotCom++)

4.06 2021-01-24
    - fix return values of boolean_values for compatibility
      (yahermann++)

4.05 2020-07-09
    - no code changes
    - modify tests for perl 7 (Nicolas R)

4.04 2019-06-29
    - document indent_length option (GH#48)

4.03 2019-06-19

Changes  view on Meta::CPAN


4.00 2018-12-07
    - production release

3.99_01 2018-12-03
    - BACKWARD INCOMPATIBILITY:
      As JSON::XS 4.0 changed its policy and enabled allow_nonref
      by default, JSON::PP also enabled allow_nonref by default

    - implement allow_tags that was introduced by JSON::XS 3.0
    - add boolean_values that was introduced by JSON::XS 4.0
    - allow literal tags in strings in relaxed mode, as JSON::XS 3.02 does
    - allow PERL_JSON_PP_USE_B environmental variable to restore
      old number detection behavior for compatibility
    - various doc updates

2.97001 2017-12-21
    - tweak internal number detector to always considers a flagged
      value as a string (GH#35, haarg++)
    - clarify json_pp options (RT-123766; Dan Jacobson)

MANIFEST  view on Meta::CPAN

t/107_allow_singlequote.t
t/108_decode.t
t/109_encode.t
t/110_bignum.t
t/112_upgrade.t
t/113_overloaded_eq.t
t/114_decode_prefix.t
t/115_tie_ixhash.t
t/116_incr_parse_fixed.t
t/117_numbers.t
t/118_boolean_values.t
t/119_incr_parse_utf8.t
t/120_incr_parse_truncated.t
t/core_bools.t
t/gh_28_json_test_suite.t
t/gh_29_trailing_false_value.t
t/rt_116998_wrong_character_offset.t
t/rt_122270_old_xs_boolean.t
t/rt_90071_incr_parse.t
t/zero-mojibake.t
META.yml                                 Module YAML meta-data (added by MakeMaker)

lib/JSON/PP.pm  view on Meta::CPAN


sub max_size {
    my $max  = defined $_[1] ? $_[1] : 0;
    $_[0]->{max_size} = $max;
    $_[0];
}


sub get_max_size { $_[0]->{max_size}; }

sub boolean_values {
    my $self = shift;
    if (@_) {
        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;
            }

lib/JSON/PP.pm  view on Meta::CPAN

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}) {
        return @$self{qw/false true/};
    }
    return;
}

sub filter_json_object {
    if (defined $_[1] and ref $_[1] eq 'CODE') {
        $_[0]->{cb_object} = $_[1];

lib/JSON/PP.pm  view on Meta::CPAN

the object's class. If found, it will be used to serialise the object into
a nonstandard tagged JSON value (that JSON decoders cannot decode).

It also causes C<decode> to parse such tagged JSON values and deserialise
them via a call to the C<THAW> method.

If C<$enable> is false (the default), then C<encode> will not consider
this type of conversion, and tagged JSON values will cause a parse error
in C<decode>, as if tags were not part of the grammar.

=head2 boolean_values

    $json->boolean_values([$false, $true])

    ($false,  $true) = $json->get_boolean_values

By default, JSON booleans will be decoded as overloaded
C<$JSON::PP::false> and C<$JSON::PP::true> objects.

With this method you can specify your own boolean values for decoding -
on decode, JSON C<false> will be decoded as a copy of C<$false>, and JSON
C<true> will be decoded as C<$true> ("copy" here is the same thing as
assigning a value to another variable, i.e. C<$copy = $false>).

This is useful when you want to pass a decoded data structure directly
to other serialisers like YAML, Data::MessagePack and so on.

Note that this works only when you C<decode>. You can set incompatible
boolean objects (like L<boolean>), but when you C<encode> a data structure
with such boolean objects, you still need to enable C<convert_blessed>
(and add a C<TO_JSON> method if necessary).

Calling this method without any arguments will reset the booleans
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])

When C<$coderef> is specified, it will be called from C<decode> each
time it decodes a JSON object. The only argument is a reference to

t/118_boolean_values.t  view on Meta::CPAN

if (eval "require Types::Serialiser; 1") {
    push @tests, [Types::Serialiser::true(), Types::Serialiser::false(), 'Types::Serialiser::BooleanBase', 'Types::Serialiser::BooleanBase'];
}

plan tests => 15 * @tests;

my $json = JSON::PP->new;
for my $test (@tests) {
    my ($true, $false, $true_class, $false_class, $incompat) = @$test;

    my $ret = $json->boolean_values($false, $true);
    is $ret => $json, "returns the same object";
    my ($new_false, $new_true) = $json->get_boolean_values;
    ok defined $new_true, "new true class is defined";
    ok defined $new_false, "new false class is defined";
    ok $new_true->isa($true_class), "new true class is $true_class";
    ok $new_false->isa($false_class), "new false class is $false_class";
    SKIP: {
        skip "$true_class is not compatible with JSON::PP::Boolean", 2 if $incompat;
        ok $new_true->isa('JSON::PP::Boolean'), "new true class is also JSON::PP::Boolean";
        ok $new_false->isa('JSON::PP::Boolean'), "new false class is also JSON::PP::Boolean";
    }

t/118_boolean_values.t  view on Meta::CPAN


    SKIP: {
        skip "$true_class is not compatible with JSON::PP::Boolean", 2 if $incompat;
        my $should_true_json = eval { $json->allow_nonref(1)->encode($new_true); };
        is $should_true_json => 'true', "A $true_class object turns into JSON true";

        my $should_false_json = eval { $json->allow_nonref(1)->encode($new_false); };
        is $should_false_json => 'false', "A $false_class object turns into JSON false";
    }

    $ret = $json->boolean_values();
    is $ret => $json, "returns the same object";
    ok !$json->get_boolean_values, "reset boolean values";

    $should_true = $json->allow_nonref(1)->decode('true');
    ok $should_true->isa('JSON::PP::Boolean'), "JSON true turns into a JSON::PP::Boolean object";

    $should_false = $json->allow_nonref(1)->decode('false');
    ok $should_false->isa('JSON::PP::Boolean'), "JSON false turns into a JSON::PP::Boolean object";
}

t/core_bools.t  view on Meta::CPAN

  plan skip_all => 'no support for core boolean options'
    unless JSON::PP->can('CORE_BOOL');
}

plan tests => 24;

my $json = JSON::PP->new;

is $json->get_core_bools, !!0, 'core_bools initially false';

$json->boolean_values(!!0, !!1);
SKIP: {
    skip "core_bools option doesn't register as true for core bools without core boolean support", 1
        unless JSON::PP::CORE_BOOL;

    is $json->get_core_bools, !!1, 'core_bools true when setting bools to core bools';
}

$json->boolean_values(!!1, !!0);
is $json->get_core_bools, !!0, 'core_bools false when setting bools to anything other than correct core bools';

my $ret = $json->core_bools;
is $ret, $json,
  "returns the same object";

my ($new_false, $new_true) = $json->get_boolean_values;

# ensure this registers as true on older perls where the boolean values
# themselves can't be tracked.
is $json->get_core_bools, !!1, 'core_bools true when setting core_bools';

ok defined $new_true, "core true value is defined";
ok defined $new_false, "core false value is defined";

ok !ref $new_true, "core true value is not blessed";
ok !ref $new_false, "core falase value is not blessed";



( run in 0.359 second using v1.01-cache-2.11-cpan-9ff20fc0ed8 )