FU
view release on metacpan or search on metacpan
JSON booleans are parsed into C<builtin::true> and C<builtin::false>. In the
other direction, the C<to_bool()> function above is used to recognize which
values to represent as JSON boolean.
JSON numbers that are too large fit into a Perl integer are parsed into a
floating point value instead. This obviously loses precision, but is consistent
with C<JSON.parse()> in JavaScript land - except Perl does support the full
range of a 64bit integer. JSON numbers with a fraction or exponent are also
converted into floating point, which may lose precision as well.
L<Math::BigInt> and L<Math::BigFloat> are not currently supported. Attempting
to format a floating point C<NaN> or C<Inf> results in an error.
=over
=item json_parse($string, %options)
Parse a JSON string and return a Perl value. With the default options, this
function is roughly similar to:
JSON::PP->new->allow_nonref->core_bools-decode($string);
FU/Validate.pm view on Meta::CPAN
C<false> according to Perl's idea of truth.
=item bool => 1
Require the input to be a boolean type as per C<to_bool()> in L<FU::Util>.
=item num => 1
Implies C<< type => 'scalar' >>. Require the input to be a number formatted
using the format permitted by JSON. Note that this is slightly more restrictive
from Perl's number formatting, in that 'NaN', 'Inf' and thousand separators are
not permitted. The value is normalized to a Perl integer or floating point
value, which means precision for large numbers may be lost.
=item int => 1
Implies C<< type => 'scalar' >>. Require the input to be an (at most) 64-bit
integer.
=item uint => 1
c/jsonfmt.c view on Meta::CPAN
SvGETMAGIC(val);
int r = fu_2bool(aTHX_ val);
if (r != -1) { /* Must check SvISBOOL() before IOKp & POKp, because it implies both flags */
if (r) fustr_write(ctx->out, "true", 4);
else fustr_write(ctx->out, "false", 5);
} else if (SvPOKp(val)) {
fujson_fmt_str(aTHX_ ctx, SvPVX(val), SvCUR(val), SvUTF8(val));
} else if (SvNOKp(val)) { /* Must check before IOKp, because integer conversion might have been lossy */
NV nv = SvNV_nomg(val);
if (isinfnan(nv)) croak("unable to format floating point NaN or Inf as JSON");
/* XXX: Cpanel::JSON::XS appears to always append a ".0" for round numbers, other modules do not. */
/* XXX#2: This doesn't support quadmath. Makefile.PL checks for that */
fustr_reserve(ctx->out, NV_DIG+32);
Gconvert(nv, NV_DIG, 0, ctx->out->cur);
ctx->out->cur += strlen(ctx->out->cur);
} else if (SvIOKp(val)) {
fujson_fmt_int(aTHX_ ctx, val);
} else if (SvROK(val)) {
/* Simply consider every reference a form of nesting. TO_JSON may
* return a scalar, but it may also return another TO_JSON object and
t/json_format.t view on Meta::CPAN
$$, $$,
''.$$, '"'.$$.'"',
do { my $x = 12; utf8::decode($x); $x }, '"12"',
do { no warnings 'numeric'; my $x = '19a'; $x += 0; $x }, '19',
$Config{uselongdouble} ? () : ( 1844674407370955161 / 10, '1.84467440737096e+17' ),
);
my @errors = (
\2, qr/unable to format reference/,
*STDOUT, qr/unable to format unknown value/,
'NaN'+0, qr/unable to format floating point NaN or Inf as JSON/,
'Inf'+0, qr/unable to format floating point NaN or Inf as JSON/,
do { my $o = {}; bless $o, 'FU::Whatever' }, qr/unable to format 'FU::Whatever' object as JSON/,
do { my $o = {}; bless $o, 'MyToJSONSelf' }, qr/MyToJSONSelf::TO_JSON method returned same object as was passed instead of a new one/,
);
for my($in, $exp) (@tests) {
my $out = json_format $in;
is $out, $exp;
ok utf8::is_utf8($out);
( run in 0.490 second using v1.01-cache-2.11-cpan-4d50c553e7e )