JSON::PP#

📦 std

Encode Perl data as JSON text and decode JSON text into Perl data, with JSON::PP’s options and answers.

encode_json and decode_json convert between a Perl structure and UTF-8 encoded JSON. An object made with JSON::PP->new takes JSON::XS’s options and JSON::PP’s own - allow_singlequote, allow_barekey, allow_bignum, loose, escape_slash, indent_length, sort_by, as_nonblessed, core_bools - and parses incrementally with incr_parse. JSON true and false decode to $JSON::PP::true and $JSON::PP::false.

This is JSON::PP 4.16 over pperl’s one JSON engine, the one under JSON::XS: the object and its option methods are JSON::PP’s own (JSON/PP.pm), encoding and decoding are the engine’s, in JSON::PP’s dialect.

Synopsis#

use JSON::PP;

my $text = encode_json { name => "caf\x{e9}", list => [1, 2.5, "x"] };
my $data = decode_json '{"a":[1,true,null]}';

my $json = JSON::PP->new->pretty->canonical->escape_slash;
print $json->encode({ url => "http://example.com/" });

my $loose = JSON::PP->new->allow_singlequote->allow_barekey;
my $hash = $loose->decode("{key:'value'}");

Differences from upstream#

  • With allow_tags, the values a FREEZE method returns are encoded as JSON; JSON::PP joins them as they are, which is not JSON and loses references (("T")[3,x,ARRAY(0x...)]). A FREEZE that returns the object itself among them dies, as it would recurse.

  • decode_prefix on a character string returns the number of characters it decoded, as its documentation says; JSON::PP returns the number of bytes of their UTF-8 encoding, and a caller that cuts the text there loses characters. incr_parse keeps the rest of its buffer as characters too; JSON::PP turns it into bytes.

  • incr_parse on character strings takes the text as it is; JSON::PP decodes it as UTF-8 first, which changes Latin-1 text that happens to form UTF-8 ("\xc3\xa9" becomes "\xe9").

  • A bare key with non-ASCII characters under allow_barekey keeps them; JSON::PP gives the bytes of their UTF-8 ({café:1} decodes to the key “café”).

  • A tag with no value under allow_tags - a lone (, or (null) - is an error; JSON::PP returns nothing for it, which decodes to undef or fails later with an unrelated message.

  • A high surrogate escape followed by another ("\ud800\ud83d\ude00") is an error; JSON::PP drops the first one and its character.

  • A character from U+110000 on dies when encoded, as under JSON::XS; JSON::PP writes two \u escapes that decode to other characters.

  • Errors perl itself raises on a wrong invocant (Not a HASH reference and the like) name the caller’s line; JSON::PP’s name a line in its own file.

Modules#

Functions#

Data serialisation#

encode#

Perl data to JSON text, with this object’s options.

Also available as JSON::PP::PP_encode_json.

decode#

JSON text to Perl data, with this object’s options.

decode_prefix#

Decode the JSON text at the start of a string.

encode_json#

Perl data to UTF-8 encoded JSON text.

decode_json#

UTF-8 encoded JSON text to Perl data.

to_json#

The old name of encode_json; it only dies, saying so.

What you get back

Nothing: it croaks “JSON::PP::to_json has been renamed to encode_json.”. from_json does the same for decode_json.

from_json#

The old name of decode_json; it only dies, saying so.

new#

A new encoder/decoder with the defaults: allow_nonref on, a maximum depth of 512, an indent of 3, everything else off.

ascii#

The option setters: ascii, latin1, utf8, indent, canonical, space_before, space_after, allow_nonref, shrink, allow_blessed, convert_blessed, relaxed, allow_unknown, allow_tags, and JSON::PP’s own allow_singlequote, allow_barekey, allow_bignum, loose, escape_slash and as_nonblessed.

get_ascii#

The option getters: get_ascii, get_utf8, get_allow_barekey and the others of each setter.

Synopsis

print JSON::PP->new->get_allow_nonref ? "on\n" : "off\n";   # on

What you get back

1 when the option is on, the empty string otherwise.

pretty#

Set indent, space_before and space_after together.

Synopsis

print JSON::PP->new->pretty->encode({ a => [1, 2] });

What you get back

The object; pretty(0) turns the three off.

max_depth#

The deepest nesting of arrays and objects that encode and decode accept.

get_max_depth#

The limit max_depth set, as it was given.

max_size#

The largest JSON text, in bytes, that decode accepts.

Synopsis

my $json = JSON::PP->new->max_size(1 << 20);

What you get back

The object. 0 (the default, and without an argument) means no limit, and so does 1. get_max_size returns the value as it was set.

get_max_size#

The limit max_size set, as it was given.

boolean_values#

The values JSON false and true decode to.

get_boolean_values#

The values set by boolean_values, false first.

What you get back

The two values, or the empty list when none are set.

core_bools#

Decode true and false to Perl’s core booleans, !!1 and !!0.

get_core_bools#

Whether core_bools is on.

unblessed_bool#

The same as core_bools.

get_unblessed_bool#

The same as get_core_bools.

filter_json_object#

A sub that each decoded object is passed to; what it returns takes the object’s place.

filter_json_single_key_object#

A sub for objects of a single key, by that key.

indent_length#

The number of spaces each level indents by under indent, 0 to 15.

Synopsis

print JSON::PP->new->pretty->indent_length(2)->encode({ a => [1] });

What you get back

The object. A value outside 0 to 15 warns and leaves the length as it was; the default is 3.

get_indent_length#

The indent indent_length set.

sort_by#

The order in which an object’s keys are written.

allow_bigint#

The old name of allow_bignum: it warns and turns allow_bignum on.

incr_parse#

Parse JSON texts as they arrive, a piece at a time.

incr_skip#

Drop the text the incremental parser has looked at, after an error.

Synopsis

eval { $json->incr_parse };
$json->incr_skip if $@;

incr_reset#

Forget everything the incremental parser holds.

incr_text#

The text the incremental parser holds, as an lvalue.

Synopsis

$json->incr_text = '[1,2]';
my $data = $json->incr_parse;

What you get back

The buffer itself. It dies once parsing has started on its text.

is_bool#

Whether a value is a JSON boolean.

true#

$JSON::PP::true, the value JSON true decodes to.

false#

$JSON::PP::false, the value JSON false decodes to.

null#

undef, the value JSON null decodes to.