Jasonify

 view release on metacpan or  search on metacpan

Changes  view on Meta::CPAN

Revision history for Jasonify.

v0.20.064 2020-03-04

 - Use keymap functionality in Datify to support proper sorting of -Infinity,
   Infinity, and NaN.
 - Change representations of -Infinity, Infinity, and NaN to exclude quotes
   (but the quotes are still there where needed).

v0.20.060 2020-02-29

 - Added LooksLike where it makes sense.

v0.20.052 2020-02-21

 - Another fix for long double support.

lib/Jasonify.pm  view on Meta::CPAN

    #qpairs  => undef,
    #qquotes => undef,
);



__PACKAGE__->set(
    # Numify options
    infinite  => 'Infinity',
    -infinite => '-Infinity',
    nonnumber => 'NaN',
    #num_sep  => undef,
);



__PACKAGE__->set(
    # Lvalueify options
    lvalue    => '$lvalue',
);

lib/Jasonify.pm  view on Meta::CPAN

        $_,

        "infinity"  => [ $Jasonify::Number::inf,  Jasonify->get("infinite")  ],
        "-infinity" => [ $Jasonify::Number::ninf, Jasonify->get("-infinite") ],
        "nan"       => [ $Jasonify::Number::nan,  Jasonify->get("nonnumber") ],
    );
    # key     string         sortby
    # ======= ============== ===========
    # "inf",  '"Infinity"',  "Infinity"
    # "-inf", '"-Infinity"', "-Infinity"
    # "nan",  '"NaN"',       "NaN"
    return Jasonify::_key->new( $_, @$rep );
}

sub _objectify_via {
    my $self   = shift;
    my $object = shift;

    if ( my $method_name = shift ) {
        return $object->can($method_name);
    }

lib/Jasonify.pm  view on Meta::CPAN

=back

=head2 Numify options

=over

=item I<infinite>  => B<Infinifty>,

=item I<-infinite> => B<-Infinifty>,

=item I<nonnumber> => B<NaN>,

How to encode the values for infinity, negative infinity, and not-a-number.

=back

=head2 Lvalueify options

=over

=item I<lvalue> => B<'$lvalue'>

t/00-basic.t  view on Meta::CPAN

my $inf  = 9**9**9;
my $nan  = $inf / $inf;

my $numstr  = "$num";
my $numfstr = "1234567890.123457";

is( Jasonify->encode($int),     '9876543210', ' int  encodes correctly' );
is( Jasonify->encode($num),          $numstr, ' num  encodes correctly' );
is( Jasonify->encode( -$inf ), '"-Infinity"', '-inf  encodes correctly' );
is( Jasonify->encode($inf),     '"Infinity"', ' inf  encodes correctly' );
is( Jasonify->encode($nan),     '"NaN"',      ' nan  encodes correctly' );

is( Jasonify->encode($numf), $numfstr, 'formatted number encodes correctly' );


# Scalars
is( Jasonify->encode( undef ), 'null', 'scalar undef' );
is( Jasonify->encode( '' ),    '""',   'scalar ""' );
is( Jasonify->encode( 0 ),     '0',    'scalar 0' );
is( Jasonify->encode( 1 ),     '1',    'scalar 1' );

t/00-basic.t  view on Meta::CPAN

push @array, [ undef, $false, $true, '', 0, 1 ];
push @value, '[null, false, true, "", 0, 1]';
is( Jasonify->encode($array[-1]), $value[-1], 'array of scalars' );

push @array, [ \undef, \$false, \$true, \'', \0, \1 ];
push @value, '[null, false, true, false, false, true]';
is( Jasonify->encode($array[-1]), $value[-1], 'array of scalar references' );

push @array, [ $int, $num, $numf, $inf, $nan ];
push @value,
    qq![9876543210, $numstr, $numfstr, "Infinity", "NaN"]!;
is( Jasonify->encode($array[-1]), $value[-1],
    'array of numbers encodes correctly' );

push @array, [ \$int, \$num, \$inf, \$nan ];
push @value, '[true, true, true, true]';
is( Jasonify->encode( $array[-1] ), $value[-1],
    'array of number references encodes correctly' );

my $value = join( ', ', @value );
is( Jasonify->encode( \@array ), "[$value]",

t/00-basic.t  view on Meta::CPAN

    map { sprintf( '"%s" : %s, ', $_, $hash{second}{$_} ) }
        sort { $a <=> $b }
            keys %{ $hash{second} }
);
$hash{second}{$inf} = $inf;
$hash{second}{$nan} = $nan;
$value{second}
    = '{'
    . $value{second}
    . '"Infinity" : "Infinity", '
    . '"NaN" : "NaN"'
    . '}';
is( Jasonify->encode( $hash{second} ), $value{second},
    'hash of numbers encodes correctly' );

$hash{third} = { map { $_ => \$_ } $int, $num, $inf, $nan };
$value{third}
    = '{'
    . qq!"$numstr" : true, !
    . '"9876543210" : true, '
    . '"Infinity" : true, '
    . '"NaN" : true'
    . '}';
is( Jasonify->encode( $hash{third} ), $value{third},
    'hash of number references encodes correctly' );

$hash{fourth} = {%hash};
$value{fourth} = '{'
    . join( ', ',
        map { sprintf( '"%s" : %s', $_, $value{$_} ) }
            qw( first second third ) )
    . '}';

t/00-basic.t  view on Meta::CPAN

         "infinite"         : "Infinity",
         "io"               : "null",
         "json_method"      : "TO_JSON",
         "keyfilter"        : null,
         "keyfilterdefault" : 1,
         "keymap"           : null,
         "keysort"          : null,
         "list_sep"         : ", ",
         "longstr"          : -1,
         "lvalue"           : "$lvalue",
         "nonnumber"        : "NaN",
         "null"             : "null",
         "object"           : "$data",
         "overloads"        : ["\"\"", "0+"],
         "pair"             : "$key : $value",
         "quote"            : "\"",
         "quote2"           : "\"",
         "reference"        : "$_",
         "tag"              : null,
         "tag_method"       : "FREEZE",
         "true"             : "true",



( run in 0.252 second using v1.01-cache-2.11-cpan-fd5d4e115d8 )