JSON-Create

 view release on metacpan or  search on metacpan

Changes  view on Meta::CPAN

0.19 2015-12-31
* Better documentation of Unicode handling.
* Unicode upgrade applied for callback return values.
* UTF-8 check applied for callback return values.

0.18 2015-12-30
* Better documentation of Unicode handling.
* Add "downgrade_utf8" method (experimental).

0.17 2015-12-29
* Add handlers for NaN and infinity.
* Documented how to remove values set by obj_handler or type_handler.

0.16 2015-12-26
* Change required version of JSON::Parse to 0.38.

0.15 2015-12-24
* Test repo is updated correctly.
* Added response to dishonest review of module.

0.14 2015-11-08

Changes  view on Meta::CPAN

* User option to escape / into \/.
* User-generated JSON callbacks
* Validation of user-generated JSON
* Boolean round-trips with JSON::Parse 0.37

0.06 2015-10-27
* More tests and benchmarks for floating point
* Fixes bug with ASCII control characters

0.05 2015-10-26
* Handling of NaN, negative and positive infinity via
  stringification.
* Removed dereferencing behaviour for objects.
* Documentation clarifies some behaviours and performance.
* Further performance wrangling for integers.

0.04 2015-10-22
* Performance increases for output of strings and integers
* Partial Unicode handling added

0.03 2015-10-21

lib/JSON/Create.pod  view on Meta::CPAN

    $jc->strict (1);

This switches on rejection of ambiguous inputs, which means

=over

=item * all non-data types, including objects,

=item * strings containing non-ASCII bytes (bytes with values of from 128 to 255) which are not marked as C<utf8> (character strings),

=item * non-finite floating point numbers (NaN or infinite values), and

=item * scalar references.

=back

Calling L</create> with such inputs results in a return value of C<undef>
(the undefined value) and a warning being printed. You can override
the behaviour for objects with L</obj>, L</obj_handler>, for non-data
types and scalar references with L</type_handler>, and for non-finite
numbers with L</non_finite_handler>.

lib/JSON/Create.pod  view on Meta::CPAN

(This example is included as L<F<examples/js-safe.pl>|https://fastapi.metacpan.org/source/BKB/JSON-Create-0.35/examples/js-safe.pl> in the distribution.)


See also L</U+2028 and U+2029 (JavaScript clashes)>.

=head2 non_finite_handler

   $jc->non_finite_handler (\& handler);

This overrides the default behaviour for handling non-finite floating
point numbers, in other words NaN (not a number) and negative or
positive infinity, with a user-defined routine. The default behaviour
of this module is described at L</Floating point numbers>. 

The routine C<handler> is supplied with the non-finite number as its
sole argument, and returns one argument, the output JSON. 

=head3 Using null for infinity and NaN

To always use C<null> in place of the default, supply a function like
the following:

    
    use JSON::Create;
    my $bread = { 'curry' => -sin(9**9**9) };
    my $jcnfh = JSON::Create->new ();
    print $jcnfh->run ($bread), "\n";
    $jcnfh->non_finite_handler(sub { return 'null'; });

lib/JSON/Create.pod  view on Meta::CPAN


=head3 Floating point numbers

Finite floating point numbers are printed using printf formatting via
C<"%g">, like

    printf ("%g", $number);

This behaviour can be altered with the method L</set_fformat>

JSON does not allow NaN or infinity as bare values. From page 6 of
L</RFC 8259>:

=over

Numeric values that cannot be represented in the grammar below (such
as Infinity and NaN) are not permitted.

=back

L</create_json> converts NaN (not a number) values to
C<"nan"> (the letters C<nan> surrounded by double quotes), and
positive and negative infinity to C<"inf"> and C<"-inf">
respectively.

L</create_json_strict> disallows non-finite numbers. If a non-finite
number appears within its input, it prints a warning L</Non-finite
number in input> and returns the undefined value:

    
    use JSON::Create 'create_json_strict';

ppport.h  view on Meta::CPAN

    s++; if (s == send || (*s != 'F' && *s != 'f')) return 0;
    s++; if (s < send && (*s == 'I' || *s == 'i')) {
      s++; if (s == send || (*s != 'N' && *s != 'n')) return 0;
      s++; if (s == send || (*s != 'I' && *s != 'i')) return 0;
      s++; if (s == send || (*s != 'T' && *s != 't')) return 0;
      s++; if (s == send || (*s != 'Y' && *s != 'y')) return 0;
      s++;
    }
    sawinf = 1;
  } else if (*s == 'N' || *s == 'n') {
    /* XXX TODO: There are signaling NaNs and quiet NaNs. */
    s++; if (s == send || (*s != 'A' && *s != 'a')) return 0;
    s++; if (s == send || (*s != 'N' && *s != 'n')) return 0;
    s++;
    sawnan = 1;
  } else
    return 0;

  if (sawinf) {
    numtype &= IS_NUMBER_NEG; /* Keep track of sign  */
    numtype |= IS_NUMBER_INFINITY | IS_NUMBER_NOT_INT;

t/float.t  view on Meta::CPAN

# http://www.cpantesters.org/cpan/report/7405efdc-7a21-11e5-8b14-c5605feaeb47

# sub double_from_hex
# {
#     unpack 'd', scalar reverse pack 'H*', $_[0] 
# }

# use constant POS_INF => double_from_hex '7FF0000000000000';
# use constant NEG_INF => double_from_hex 'FFF0000000000000';

# use constant qNaN    => double_from_hex '7FF8000000000000';
# use constant NaN     => qNaN;

#my $nan = 'nan';
#my $nan = NaN;
# http://stackoverflow.com/questions/1185822/how-do-i-create-or-test-for-nan-or-infinity-in-perl

my $inf = 9**9**9;
my $neginf = -9**9**9;
my $nan = -sin(9**9**9);

# Seems to work on Perl 5.8:
# http://codepad.org/Dum7uLwD

#note "$inf $neginf $nan\n";

t/float.t  view on Meta::CPAN

SKIP: {
    if (! $nan || ! $inf || ! $neginf) {
	skip 'Could not get nan or inf or neginf', 3*(
	    1   # tests with create_json
	    +2  # tests with create_json_strict
	    +2  # tests with object.
	);
    }

    my $bread = {
	#    'curry' => NaN,
	'curry' => $nan,
    };

    my $rice = {
	#    'rice' => POS_INF,
	'rice' => $inf,
    };

    my $lice = {
	#    'lice' => NEG_INF,



( run in 0.910 second using v1.01-cache-2.11-cpan-4d50c553e7e )