JSON-SIMD
view release on metacpan or search on metacpan
Changes.old view on Meta::CPAN
4.01 Sun Feb 24 05:03:30 CET 2019
- fix some stack corruption caused mostly when calling methods
in list context (testcase by Wesley Schwengle).
4.0 Fri Nov 16 00:06:54 CET 2018
- SECURITY IMPLICATION: this release enables allow_nonref by default
for compatibility with RFC 7159 and newer. See "old" vs. "new"
JSON under SECURITY CONSIDERATIONS.
- reworked the "old" vs. "new" JSON section.
- add ->boolean_values to provide the values to which booleans
decode (requested by Aristotle Pagaltzis).
- decode would wrongly accept ASCII NUL characters instead of
reporting them as trailing garbage.
- work around what smells like a perl bug w.r.t. exceptions
thrown in callbacks.
- incremental parser now more or less respects allow_nonref.
- json_xs json-pretty now enables canonical mode.
- add documentation section about I-JSON.
- minor documentation fixes/updates.
It also causes "decode" to parse such tagged JSON values and
deserialise them via a call to the "THAW" method.
If $enable is false (the default), then "encode" will not consider
this type of conversion, and tagged JSON values will cause a parse
error in "decode", as if tags were not part of the grammar.
This option is not compatible with "use_simdjson", and using this
option will silently disable simdjson mode.
$json->boolean_values ([$false, $true])
($false, $true) = $json->get_boolean_values
By default, JSON booleans will be decoded as overloaded
$Types::Serialiser::false and $Types::Serialiser::true objects.
With this method you can specify your own boolean values for
decoding - on decode, JSON "false" will be decoded as a copy of
$false, and JSON "true" will be decoded as $true ("copy" here is the
same thing as assigning a value to another variable, i.e. "$copy =
$false").
Calling this method without any arguments will reset the booleans to
their default values.
"get_boolean_values" will return both $false and $true values, or
the empty list when they are set to the default.
$json->core_bools([$enable])
$enabled = $json->get_core_bools
If $enable is true (or missing), then subsequent "decode"s will
produce standard perl boolean values. Equivalent to calling:
$json->boolean_values(!!0, !!1)
"get_core_bools" will return true if this has been set. On perl 5.36
or newer, it will also return true if the boolean values have been
set to perl's core booleans using the boolean_values method.
If $enable is false, the booleans are reset to their default values.
(See also "encode_core_bools" for the encode counterpart of this.)
$json = $json->encode_core_bools ([$enable])
$enabled = $json->get_encode_core_bools
If $enable is true (or missing), then subsequent "encode" operations
will recognize Perl's special boolean values !!0 and !!1 (or
"builtin::false" and "builtin::true") and encode them as JSON
quadmath, we fall back to the slower but more precise legacy number
parser in order to avoid the loss of precision.
true, false
By default, these JSON atoms become "Types::Serialiser::true" and
"Types::Serialiser::false", respectively. They are overloaded to act
almost exactly like the numbers 1 and 0. You can check whether a
scalar is a JSON boolean by using the "Types::Serialiser::is_bool"
function (after "use Types::Serialier", of course).
You can also use the "boolean_values" method to supply your own true
and false values for decoding, or the "core_bools" method to decode
to Perl's standard booleans (the special values !!0 or !!1, also
available as the aliases "false" and "true" from the (experimental)
"builtin" module since perl 5.36).
null
A JSON null atom becomes "undef" in Perl.
shell-style comments ("# *text*")
As a nonstandard extension to the JSON syntax that is enabled by the
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.
This option is not compatible with C<use_simdjson>, and using this option
will silently disable simdjson mode.
=item $json->boolean_values ([$false, $true])
=item ($false, $true) = $json->get_boolean_values
By default, JSON booleans will be decoded as overloaded
C<$Types::Serialiser::false> and C<$Types::Serialiser::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>).
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.
=item $json->core_bools([$enable])
=item $enabled = $json->get_core_bools
If C<$enable> is true (or missing), then subsequent C<decode>s will produce
standard perl boolean values. Equivalent to calling:
$json->boolean_values(!!0, !!1)
C<get_core_bools> will return true if this has been set. On perl 5.36 or newer,
it will also return true if the boolean values have been set to perl's core
booleans using the boolean_values method.
If C<$enable> is false, the booleans are reset to their default values.
(See also C<encode_core_bools> for the encode counterpart of this.)
=item $json = $json->encode_core_bools ([$enable])
=item $enabled = $json->get_encode_core_bools
If C<$enable> is true (or missing), then subsequent C<encode> operations
in order to avoid the loss of precision.
=item true, false
By default, these JSON atoms become C<Types::Serialiser::true> and
C<Types::Serialiser::false>, respectively. They are overloaded to act
almost exactly like the numbers C<1> and C<0>. You can check whether
a scalar is a JSON boolean by using the C<Types::Serialiser::is_bool>
function (after C<use Types::Serialier>, of course).
You can also use the C<boolean_values> method to supply your own true
and false values for decoding, or the C<core_bools> method to decode to
Perl's standard booleans (the special values !!0 or !!1, also available
as the aliases C<false> and C<true> from the (experimental) C<builtin>
module since perl 5.36).
=item null
A JSON null atom becomes C<undef> in Perl.
=item shell-style comments (C<< # I<text> >>)
{
SV *pv = NEWSV (0, sizeof (JSON));
SvPOK_only (pv);
json_init ((JSON *)SvPVX (pv));
XPUSHs (sv_2mortal (sv_bless (
newRV_noinc (pv),
strEQ (klass, "JSON::SIMD") ? JSON_STASH : gv_stashpv (klass, 1)
)));
}
void boolean_values (JSON *self, SV *v_false = 0, SV *v_true = 0)
PPCODE:
self->flags &= ~F_CORE_BOOLS;
self->v_false = newSVsv (v_false);
self->v_true = newSVsv (v_true);
XPUSHs (ST (0));
void get_boolean_values (JSON *self)
PPCODE:
if (self->v_false && self->v_true)
{
EXTEND (SP, 2);
PUSHs (self->v_false);
PUSHs (self->v_true);
}
void core_bools (JSON *self, int enable = 1)
PPCODE:
t/23_boolean.t view on Meta::CPAN
#! perl
use strict;
no warnings;
use Test::More;
use JSON::SIMD;
# setting to arbitrary values
{
my $J = JSON::SIMD->new->boolean_values("nay", "yea");
my $json = '[false, true]';
my $decoded = $J->decode($json);
ok $decoded->[0] eq "nay", "false is nay";
ok $decoded->[1] eq "yea", "true is yea";
my @recovered_bools = $J->get_boolean_values;
ok $recovered_bools[0] eq "nay", "false is nay";
ok $recovered_bools[1] eq "yea", "true is yea";
is $J->get_core_bools, !!0, "arbitrary booleans are not core";
}
# setting to core booleans manually
{
my $J = JSON::SIMD->new->boolean_values(!!0, !!1);
my $json = '[false, true]';
my $decoded = $J->decode($json);
ok !$decoded->[0], "false is false";
ok $decoded->[1], "true is true";
my @recovered_bools = $J->get_boolean_values;
ok !$recovered_bools[0], "false is false";
ok $recovered_bools[1], "true is true";
if ($] >= 5.036) {
is $J->get_core_bools, !!1, "manually set core booleans on >= 5.36 perls are core";
} else {
is $J->get_core_bools, !!0, "manually set core booleans on < 5.36 perls are not core";
}
}
# setting core booleans with the dedicated option
{
my $J = JSON::SIMD->new->core_bools;
my $json = '[false, true]';
my $decoded = $J->decode($json);
ok !$decoded->[0], "false is false";
ok $decoded->[1], "true is true";
my @recovered_bools = $J->get_boolean_values;
ok !$recovered_bools[0], "false is false";
ok $recovered_bools[1], "true is true";
is $J->get_core_bools, 1, "manually set core booleans on >= 5.36 perls are core";
$J->core_bools(0);
my $decoded2 = $J->decode($json);
ok !$decoded2->[0], "false is false";
ok $decoded2->[1], "true is true";
ok ref $decoded2->[0] eq 'JSON::PP::Boolean', 'default boolean';
( run in 0.283 second using v1.01-cache-2.11-cpan-9ff20fc0ed8 )