CPANPLUS
view release on metacpan or search on metacpan
inc/bundle/JSON/PP.pm view on Meta::CPAN
# If it didn't work, it could be that there is a full legal character
# followed by a partial or malformed one. Narrow the window and
# try again.
$limit--;
}
# Failed to find a legal UTF-8 character.
$utf8_len = 0;
return;
}
sub decode_error {
my $error = shift;
my $no_rep = shift;
my $str = defined $text ? substr($text, $at) : '';
my $mess = '';
my $type = 'U*';
for my $c ( unpack( $type, $str ) ) { # emulate pv_uni_display() ?
my $chr_c = chr($c);
$mess .= $chr_c eq '\\' ? '\\\\'
: $chr_c =~ /[[:print:]]/ ? $chr_c
: $chr_c eq '\a' ? '\a'
: $chr_c eq '\t' ? '\t'
: $chr_c eq '\n' ? '\n'
: $chr_c eq '\r' ? '\r'
: $chr_c eq '\f' ? '\f'
: sprintf('\x{%x}', $c)
;
if ( length $mess >= 20 ) {
$mess .= '...';
last;
}
}
unless ( length $mess ) {
$mess = '(end of string)';
}
Carp::croak (
$no_rep ? "$error" : "$error, at character offset $at (before \"$mess\")"
);
}
sub _json_object_hook {
my $o = $_[0];
my @ks = keys %{$o};
if ( $cb_sk_object and @ks == 1 and exists $cb_sk_object->{ $ks[0] } and ref $cb_sk_object->{ $ks[0] } ) {
my @val = $cb_sk_object->{ $ks[0] }->( $o->{$ks[0]} );
if (@val == 0) {
return $o;
}
elsif (@val == 1) {
return $val[0];
}
else {
Carp::croak("filter_json_single_key_object callbacks must not return more than one scalar");
}
}
my @val = $cb_object->($o) if ($cb_object);
if (@val == 0) {
return $o;
}
elsif (@val == 1) {
return $val[0];
}
else {
Carp::croak("filter_json_object callbacks must not return more than one scalar");
}
}
sub PP_decode_box {
{
text => $text,
at => $at,
ch => $ch,
len => $len,
depth => $depth,
encoding => $encoding,
is_valid_utf8 => $is_valid_utf8,
};
}
} # PARSE
sub _decode_surrogates { # from perlunicode
my $uni = 0x10000 + (hex($_[0]) - 0xD800) * 0x400 + (hex($_[1]) - 0xDC00);
my $un = pack('U*', $uni);
utf8::encode( $un );
return $un;
}
sub _decode_unicode {
my $un = pack('U', hex shift);
utf8::encode( $un );
return $un;
}
sub incr_parse {
local $Carp::CarpLevel = 1;
( $_[0]->{_incr_parser} ||= JSON::PP::IncrParser->new )->incr_parse( @_ );
}
sub incr_skip {
( $_[0]->{_incr_parser} ||= JSON::PP::IncrParser->new )->incr_skip;
}
sub incr_reset {
( $_[0]->{_incr_parser} ||= JSON::PP::IncrParser->new )->incr_reset;
}
sub incr_text : lvalue {
$_[0]->{_incr_parser} ||= JSON::PP::IncrParser->new;
if ( $_[0]->{_incr_parser}->{incr_pos} ) {
Carp::croak("incr_text cannot be called when the incremental parser already started parsing");
}
$_[0]->{_incr_parser}->{incr_text};
}
###############################
# Utilities
inc/bundle/JSON/PP.pm view on Meta::CPAN
argument being the object to serialise, and the second argument being the
constant string C<JSON> to distinguish it from other serialisers.
The C<FREEZE> method can return any number of values (i.e. zero or
more). These values and the paclkage/classname of the object will then be
encoded as a tagged JSON value in the following format:
("classname")[FREEZE return values...]
e.g.:
("URI")["http://www.google.com/"]
("MyDate")[2013,10,29]
("ImageData::JPEG")["Z3...VlCg=="]
For example, the hypothetical C<My::Object> C<FREEZE> method might use the
objects C<type> and C<id> members to encode the object:
sub My::Object::FREEZE {
my ($self, $serialiser) = @_;
($self->{type}, $self->{id})
}
=item 2. C<convert_blessed> is enabled and the object has a C<TO_JSON> method.
In this case, the C<TO_JSON> method of the object is invoked in scalar
context. It must return a single scalar that can be directly encoded into
JSON. This scalar replaces the object in the JSON text.
For example, the following C<TO_JSON> method will convert all L<URI>
objects to JSON strings when serialised. The fact that these values
originally were L<URI> objects is lost.
sub URI::TO_JSON {
my ($uri) = @_;
$uri->as_string
}
=item 3. C<allow_bignum> is enabled and the object is a C<Math::BigInt> or C<Math::BigFloat>.
The object will be serialised as a JSON number value.
=item 4. C<allow_blessed> is enabled.
The object will be serialised as a JSON null value.
=item 5. none of the above
If none of the settings are enabled or the respective methods are missing,
C<JSON::PP> throws an exception.
=back
=head3 DESERIALISATION
For deserialisation there are only two cases to consider: either
nonstandard tagging was used, in which case C<allow_tags> decides,
or objects cannot be automatically be deserialised, in which
case you can use postprocessing or the C<filter_json_object> or
C<filter_json_single_key_object> callbacks to get some real objects our of
your JSON.
This section only considers the tagged value case: a tagged JSON object
is encountered during decoding and C<allow_tags> is disabled, a parse
error will result (as if tagged values were not part of the grammar).
If C<allow_tags> is enabled, C<JSON::PP> will look up the C<THAW> method
of the package/classname used during serialisation (it will not attempt
to load the package as a Perl module). If there is no such method, the
decoding will fail with an error.
Otherwise, the C<THAW> method is invoked with the classname as first
argument, the constant string C<JSON> as second argument, and all the
values from the JSON array (the values originally returned by the
C<FREEZE> method) as remaining arguments.
The method must then return the object. While technically you can return
any Perl scalar, you might have to enable the C<allow_nonref> setting to
make that work in all cases, so better return an actual blessed reference.
As an example, let's implement a C<THAW> function that regenerates the
C<My::Object> from the C<FREEZE> example earlier:
sub My::Object::THAW {
my ($class, $serialiser, $type, $id) = @_;
$class->new (type => $type, id => $id)
}
=head1 ENCODING/CODESET FLAG NOTES
This section is taken from JSON::XS.
The interested reader might have seen a number of flags that signify
encodings or codesets - C<utf8>, C<latin1> and C<ascii>. There seems to be
some confusion on what these do, so here is a short comparison:
C<utf8> controls whether the JSON text created by C<encode> (and expected
by C<decode>) is UTF-8 encoded or not, while C<latin1> and C<ascii> only
control whether C<encode> escapes character values outside their respective
codeset range. Neither of these flags conflict with each other, although
some combinations make less sense than others.
Care has been taken to make all flags symmetrical with respect to
C<encode> and C<decode>, that is, texts encoded with any combination of
these flag values will be correctly decoded when the same flags are used
- in general, if you use different flag settings while encoding vs. when
decoding you likely have a bug somewhere.
Below comes a verbose discussion of these flags. Note that a "codeset" is
simply an abstract set of character-codepoint pairs, while an encoding
takes those codepoint numbers and I<encodes> them, in our case into
octets. Unicode is (among other things) a codeset, UTF-8 is an encoding,
and ISO-8859-1 (= latin 1) and ASCII are both codesets I<and> encodings at
the same time, which can be confusing.
=over 4
=item C<utf8> flag disabled
( run in 0.721 second using v1.01-cache-2.11-cpan-39bf76dae61 )