JSON5
view release on metacpan or search on metacpan
## decode
```
$perl_scalar = $json5->decode($json5_text)
```
JSON5 numbers and strings become simple Perl scalars. JSON5 arrays become
Perl arrayrefs and JSON5 objects become Perl hashrefs. `true` becomes
`1` (`JSON::PP::true`), `false` becomes `0` (`JSON::PP::false`),
`NaN` becomes `'NaN'`, `Infinity` becomes `'Inf'`, and
`null` becomes `undef`.
# SEE ALSO
[JSON::PP](https://metacpan.org/pod/JSON::PP)
# LICENSE
Copyright (C) karupanerura.
lib/JSON5.pm view on Meta::CPAN
If no argument is given, the limit check will be deactivated (same as when
C<0> is specified).
=head2 decode
$perl_scalar = $json5->decode($json5_text)
JSON5 numbers and strings become simple Perl scalars. JSON5 arrays become
Perl arrayrefs and JSON5 objects become Perl hashrefs. C<true> becomes
C<1> (C<JSON::PP::true>), C<false> becomes C<0> (C<JSON::PP::false>),
C<NaN> becomes C<'NaN'>, C<Infinity> becomes C<'Inf'>, and
C<null> becomes C<undef>.
=head1 SEE ALSO
L<JSON::PP>
=head1 LICENSE
Copyright (C) karupanerura.
lib/JSON5/Parser.pm view on Meta::CPAN
our $ROOT;
our $POINTER;
sub new {
my $class = shift;
return bless +{
utf8 => 0,
allow_nonref => 0,
max_size => 0,
inflate_boolean => sub { $_[0] eq 'true' ? JSON::PP::true : JSON::PP::false },
inflate_nan => sub { 0+'NaN' },
inflate_null => sub { undef },
inflate_infinity => sub { $_[0] eq '+' ? 0+'Inf' : 0+'-Inf' },
} => $class;
}
# define accessors
BEGIN {
# boolean accessors
for my $attr (qw/utf8 allow_nonref/) {
my $attr_accessor = sub {
lib/JSON5/Parser.pm view on Meta::CPAN
}
sub _parse_number {
my $self = shift;
if (/\G([-+])?Infinity/mgc) {
my $number = $self->{inflate_infinity}->($1 || '+');
${$POINTER} = $number;
return 1;
}
elsif (/\GNaN/mgc) {
my $number = $self->{inflate_nan}->();
${$POINTER} = $number;
return 1;
}
elsif (/\G([-+]?)0x([0-9a-f]+)/imgc) {
my $number = hex $2;
$number *= -1 if $1 && $1 eq '-';
${$POINTER} = $number;
return 1;
}
t/parser/01_value.t view on Meta::CPAN
use Test::More 0.98;
use JSON5;
use JSON5::Parser;
my $parser = JSON5::Parser->new->allow_nonref;
is $parser->parse('null'), undef, 'value: null';
is $parser->parse('true'), JSON5::true, 'value: true';
is $parser->parse('false'), JSON5::false, 'value: false';
is $parser->parse('NaN'), 0+'NaN', 'value: NaN';
is $parser->parse('Infinity'), 0+'Inf', 'value: Infinity';
is $parser->parse('-Infinity'), 0+'-Inf', 'value: -Infinity';
is $parser->parse('-1'), -1, 'value: -1';
is $parser->parse('0'), 0, 'value: 0';
is $parser->parse('1'), 1, 'value: 1';
is $parser->parse('-1.1'), -1.1, 'value: -1.1';
is $parser->parse('0.1'), 0.1, 'value: 0.1';
is $parser->parse('1.1'), 1.1, 'value: 1.1';
is $parser->parse('-.1'), -0.1, 'value: -.1';
is $parser->parse('.1'), 0.1, 'value: .1';
t/parser/03_struct.t view on Meta::CPAN
===
--- name: empty array
--- input
[]
--- expected
[]
===
--- name: array values
--- input
[null, true, false, NaN, Infinity, -Infinity, -1, 0, 1, -1.1, 0.1, 1.1, -.1, .1, 0x12AB, 0xcd34, 'str1', "str2", [], {},]
--- expected
[undef, JSON5::true, JSON5::false, 0+'NaN', 0+'Inf', 0+'-Inf', -1, 0, 1, -1.1, 0.1, 1.1, -0.1, 0.1, 4779, 52532, 'str1', "str2", [], {},]
===
--- name: nested object values
--- input
[
{
$1: [
{$2: [3, 4.5, {$6: {$7: 8, $9: "10"}}, "1-1"]},
{$2: [3, 4.5, {$6: {$7: 8, $9: "10"}}, "1-2"]},
],
t/parser/05_public.t view on Meta::CPAN
use JSON5::Parser;
my $json = JSON::PP->new->allow_nonref->utf8;
Test::Base::Less::register_filter(json => sub { $json->decode($_[0]) });
filters {
expected => [qw/json/],
};
my $parser = JSON5::Parser->new
->inflate_nan(sub { "<<<SPECIAL VALUE:NaN>>>" })
->inflate_infinity(sub { "<<<SPECIAL VALUE:$_[0]Infinity>>>" })
->allow_nonref
->utf8;
for my $block (blocks) {
my $parsed = eval { $parser->parse($block->input) };
is_deeply $parsed, $block->expected, $block->get_section('name')
or diag $block->input;
diag $@ if $@;
}
t/parser/05_public.t view on Meta::CPAN
===
--- name: numbers - float-trailing-decimal-point-with-integer-exponent
--- input
5.e4
--- expected
50000
===
--- name: numbers - nan
--- input
NaN
--- expected
"<<<SPECIAL VALUE:NaN>>>"
===
--- name: numbers - negative-float-trailing-decimal-point
--- input
-5.
--- expected
-5
===
--- name: numbers - hexadecimal
--- input
( run in 0.670 second using v1.01-cache-2.11-cpan-4d50c553e7e )