App-Kritika
view release on metacpan or search on metacpan
kritika.fatpack view on Meta::CPAN
keys is preserved (JSON does not preserver object key ordering itself).
=item array
A JSON array becomes a reference to an array in Perl.
=item string
A JSON string becomes a string scalar in Perl - Unicode codepoints in JSON
are represented by the same codepoints in the Perl string, so no manual
decoding is necessary.
=item number
A JSON number becomes either an integer, numeric (floating point) or
string scalar in perl, depending on its range and any fractional parts. On
the Perl level, there is no difference between those as Perl handles all
the conversion details, but an integer may take slightly less memory and
might represent more values exactly than floating point numbers.
If the number consists of digits only, this module will try to represent
it as an integer value. If that fails, it will try to represent it as
a numeric (floating point) value if that is possible without loss of
precision. Otherwise it will preserve the number as a string value (in
which case you lose roundtripping ability, as the JSON number will be
re-encoded to a JSON string).
Numbers containing a fractional or exponential part will always be
represented as numeric (floating point) values, possibly at a loss of
precision (in which case you might lose perfect roundtripping ability, but
the JSON number will still be re-encoded as a JSON number).
Note that precision is not accuracy - binary floating point values cannot
represent most decimal fractions exactly, and when converting from and to
floating point, this module only guarantees precision up to but not including
the least significant bit.
=item true, false
These JSON atoms become C<JSON::true> and C<JSON::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<JSON::is_bool> function.
=item null
A JSON null atom becomes C<undef> in Perl.
=item shell-style comments (C<< # I<text> >>)
As a nonstandard extension to the JSON syntax that is enabled by the
C<relaxed> setting, shell-style comments are allowed. They can start
anywhere outside strings and go till the end of the line.
=back
=head2 PERL -> JSON
The mapping from Perl to JSON is slightly more difficult, as Perl is a
truly typeless language, so we can only guess which JSON type is meant by
a Perl value.
=over 4
=item hash references
Perl hash references become JSON objects. As there is no inherent
ordering in hash keys (or JSON objects), they will usually be encoded
in a pseudo-random order. This module can optionally sort the hash keys
(determined by the I<canonical> flag), so the same data structure will
serialise to the same JSON text (given same settings and version of
the same backend), but this incurs a runtime overhead and is only rarely useful,
e.g. when you want to compare some JSON text against another for equality.
=item array references
Perl array references become JSON arrays.
=item other references
Other unblessed references are generally not allowed and will cause an
exception to be thrown, except for references to the integers C<0> and
C<1>, which get turned into C<false> and C<true> atoms in JSON. You can
also use C<JSON::false> and C<JSON::true> to improve readability.
encode_json [\0,JSON::true] # yields [false,true]
=item JSON::true, JSON::false, JSON::null
These special values become JSON true and JSON false values,
respectively. You can also use C<\1> and C<\0> directly if you want.
=item blessed objects
Blessed objects are not directly representable in JSON, but C<JSON::XS>
allows various ways of handling objects. See L<OBJECT SERIALISATION>,
below, for details.
=item simple scalars
Simple Perl scalars (any scalar that is not a reference) are the most
difficult objects to encode: this module will encode undefined scalars as
JSON C<null> values, scalars that have last been used in a string context
before encoding as JSON strings, and anything else as number value:
# dump as number
encode_json [2] # yields [2]
encode_json [-3.0e17] # yields [-3e+17]
my $value = 5; encode_json [$value] # yields [5]
# used as string, so dump as string
print $value;
encode_json [$value] # yields ["5"]
# undef becomes null
encode_json [undef] # yields [null]
You can force the type to be a string by stringifying it:
my $x = 3.1; # some variable containing a number
( run in 1.097 second using v1.01-cache-2.11-cpan-d7f47b0818f )