Add initial JSON formatter
It works and can format all "plain" Perl data, but has a few known bugs and limitations that still need to be worked out. It's about 8x smaller than JSON::XS's encoder and *much* smaller than Cpanel::JSON::XS, but this is just a first attempt, it'll grow.
This commit is contained in:
parent
9c80f2465a
commit
c16a9fa493
10 changed files with 421 additions and 0 deletions
17
FU/Util.pm
Normal file
17
FU/Util.pm
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
package FU::Util 0.1;
|
||||
|
||||
use v5.36;
|
||||
use FU::XS;
|
||||
use Exporter 'import';
|
||||
|
||||
our @EXPORT_OK = qw/json_format/;
|
||||
|
||||
|
||||
sub json_format($val, %opt) {
|
||||
my $r = FU::XS::json_format($val);
|
||||
# XXX: Does this go over the bytes? If so, not setting SvUTF8_on() in the first place would be much faster.
|
||||
utf8::encode($r) if $opt{utf8};
|
||||
$r
|
||||
}
|
||||
|
||||
1;
|
||||
60
FU/Util.pod
Normal file
60
FU/Util.pod
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
=head1 NAME
|
||||
|
||||
FU::Util - Miscellaneous utility functions that really should have been part of
|
||||
a core Perl installation but aren't for some reason because the Perl community
|
||||
doesn't believe in the concept of a "batteries included" standard library.
|
||||
</rant>
|
||||
|
||||
=head1 SYNOPSIS
|
||||
|
||||
use FU::Util qw/json_format/;
|
||||
|
||||
my $data = json_format [1, 2, 3];
|
||||
|
||||
=head1 DESCRIPTION
|
||||
|
||||
=head2 JSON parsing & formatting
|
||||
|
||||
This module comes with a custom C-based JSON parser and formatter. These
|
||||
functions conform strictly to L<RFC-8259|https://tools.ietf.org/html/rfc8259>,
|
||||
non-standard extensions are not supported and never will be.
|
||||
|
||||
JSON booleans are decoded into C<builtin::true> and C<builtin::false>. When
|
||||
formatting, those builtin constants are the I<only> recognized boolean values -
|
||||
alternative representations such as C<JSON::PP::true> and C<JSON::PP::false>
|
||||
are not recognized and attempting to format such values will croak.
|
||||
|
||||
I<TODO: point to benchmarks.>
|
||||
|
||||
I<TODO: FU::JSON wrapper with somewhat-compatible JSON::{PP,XS} API>
|
||||
|
||||
=over
|
||||
|
||||
=item json_format($scalar, %options)
|
||||
|
||||
Format a Perl value as JSON.
|
||||
|
||||
With the default options, this function behaves roughly similar to:
|
||||
|
||||
JSON::PP->new->allow_nonref->core_bools->convert_blessed->encode($scalar);
|
||||
|
||||
This function croaks when attempting to format a floating point C<NaN> or
|
||||
C<Inf>.
|
||||
|
||||
Some modules escape the slash character in encoded strings to prevent a
|
||||
potential XSS vulnerability when embedding JSON inside C<< <script> ..
|
||||
</script> >> tags. This function does I<not> do that because it might not even
|
||||
be sufficient. The following is probably an improvement:
|
||||
|
||||
json_format($data) =~ s{</}{<\\/}rg =~ s/<!--/<\\u0021--/rg;
|
||||
|
||||
=back
|
||||
|
||||
(Why the hell yet another JSON codec when CPAN is already full of them!? Well,
|
||||
L<JSON::XS> is pretty cool but isn't going to be updated to support Perl's new
|
||||
builtin booleans. L<JSON::PP> is slow and while L<Cpanel::JSON::XS> is
|
||||
perfectly adequate, its codebase is a little too messy for my taste - too many
|
||||
unnecessary features and C<#ifdef>s to support ancient perls and esoteric
|
||||
configurations. Still, if you need anything not provided by these functions,
|
||||
L<JSON::PP> and L<Cpanel::JSON::XS> are perfectly fine alternatives.
|
||||
L<JSON::SIMD> and L<Mojo::JSON> also look like good and maintained candidates.)
|
||||
1
FU/XS.pm
1
FU/XS.pm
|
|
@ -1,3 +1,4 @@
|
|||
# This module is for internal use by other FU modules.
|
||||
package FU::XS 0.1;
|
||||
use Carp; # may be called by XS.
|
||||
use XSLoader;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue