perl

 view release on metacpan or  search on metacpan

MANIFEST  view on Meta::CPAN

cpan/JSON-PP/t/107_allow_singlequote.t
cpan/JSON-PP/t/108_decode.t
cpan/JSON-PP/t/109_encode.t
cpan/JSON-PP/t/110_bignum.t
cpan/JSON-PP/t/112_upgrade.t
cpan/JSON-PP/t/113_overloaded_eq.t
cpan/JSON-PP/t/114_decode_prefix.t
cpan/JSON-PP/t/115_tie_ixhash.t
cpan/JSON-PP/t/116_incr_parse_fixed.t
cpan/JSON-PP/t/117_numbers.t
cpan/JSON-PP/t/118_boolean_values.t
cpan/JSON-PP/t/119_incr_parse_utf8.t
cpan/JSON-PP/t/120_incr_parse_truncated.t
cpan/JSON-PP/t/core_bools.t
cpan/JSON-PP/t/gh_28_json_test_suite.t
cpan/JSON-PP/t/gh_29_trailing_false_value.t
cpan/JSON-PP/t/rt_116998_wrong_character_offset.t
cpan/JSON-PP/t/rt_122270_old_xs_boolean.t
cpan/JSON-PP/t/rt_90071_incr_parse.t
cpan/JSON-PP/t/zero-mojibake.t
cpan/libnet/lib/Net/Cmd.pm		Module related to libnet

cpan/JSON-PP/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;
            }

cpan/JSON-PP/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];

cpan/JSON-PP/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

cpan/JSON-PP/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";
    }

cpan/JSON-PP/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";
}

cpan/JSON-PP/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.430 second using v1.01-cache-2.11-cpan-9ff20fc0ed8 )