.\" Automatically generated by Pod::Man 2.22 (Pod::Simple 3.13)
.\"
.\" Standard preamble:
.\" ========================================================================
.de Sp \" Vertical space (when we can't use .PP)
.if t .sp .5v
.if n .sp
..
.de Vb \" Begin verbatim text
.ft CW
.nf
.ne \\$1
..
.de Ve \" End verbatim text
.ft R
.fi
..
.\" Set up some character translations and predefined strings. \*(-- will
.\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left
.\" double quote, and \*(R" will give a right double quote. \*(C+ will
.\" give a nicer C++. Capital omega is used to do unbreakable dashes and
.\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff,
.\" nothing in troff, for use with C<>.
.tr \(*W-
.ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p'
.ie n \{\
. ds -- \(*W-
. ds PI pi
. if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch
. if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch
. ds L" ""
. ds R" ""
. ds C` ""
. ds C' ""
'br\}
.el\{\
. ds -- \|\(em\|
. ds PI \(*p
. ds L" ``
. ds R" ''
'br\}
.\"
.\" Escape single quotes in literal strings from groff's Unicode transform.
.ie \n(.g .ds Aq \(aq
.el .ds Aq '
.\"
.\" If the F register is turned on, we'll generate index entries on stderr for
.\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index
.\" entries marked with X<> in POD. Of course, you'll have to process the
.\" output yourself in some meaningful fashion.
.ie \nF \{\
. de IX
. tm Index:\\$1\t\\n%\t"\\$2"
..
. nr % 0
. rr F
.\}
.el \{\
. de IX
..
.\}
.\" ========================================================================
.\"
.IX Title "inc::JSON::PP 3"
.TH inc::JSON::PP 3 "2015-06-22" "perl v5.10.1" "User Contributed Perl Documentation"
.\" For nroff, turn off justification. Always turn off hyphenation; it makes
.\" way too many mistakes in technical documents.
.if n .ad l
.nh
.SH "NAME"
JSON::PP \- JSON::XS compatible pure\-Perl module.
.SH "SYNOPSIS"
.IX Header "SYNOPSIS"
.Vb 1
\& use JSON::PP;
\&
\& # exported functions, they croak on error
\& # and expect/generate UTF\-8
\&
\& $utf8_encoded_json_text = encode_json $perl_hash_or_arrayref;
\& $perl_hash_or_arrayref = decode_json $utf8_encoded_json_text;
\&
\& # OO\-interface
\&
\& $coder = JSON::PP\->new\->ascii\->pretty\->allow_nonref;
\&
\& $json_text = $json\->encode( $perl_scalar );
\& $perl_scalar = $json\->decode( $json_text );
\&
\& $pretty_printed = $json\->pretty\->encode( $perl_scalar ); # pretty\-printing
\&
\& # Note that JSON version 2.0 and above will automatically use
\& # JSON::XS or JSON::PP, so you should be able to just:
\&
\& use JSON;
.Ve
.SH "VERSION"
.IX Header "VERSION"
.Vb 1
\& 2.27202
.Ve
.PP
\&\s-1JSON::XS\s0 2.27 (~2.30) compatible.
.SH "NOTE"
.IX Header "NOTE"
\&\s-1JSON::PP\s0 had been inculded in \s-1JSON\s0 distribution (\s-1CPAN\s0 module).
It was a perl core module in Perl 5.14.
.SH "DESCRIPTION"
.IX Header "DESCRIPTION"
This module is \s-1JSON::XS\s0 compatible pure Perl module.
(Perl 5.8 or later is recommended)
.PP
\&\s-1JSON::XS\s0 is the fastest and most proper \s-1JSON\s0 module on \s-1CPAN\s0.
It is written by Marc Lehmann in C, so must be compiled and
installed in the used environment.
.PP
\&\s-1JSON::PP\s0 is a pure-Perl module and has compatibility to \s-1JSON::XS\s0.
.SS "\s-1FEATURES\s0"
.IX Subsection "FEATURES"
.IP "\(bu" 4
correct unicode handling
.Sp
This module knows how to handle Unicode (depending on Perl version).
.Sp
See to \*(L"A \s-1FEW\s0 \s-1NOTES\s0 \s-1ON\s0 \s-1UNICODE\s0 \s-1AND\s0 \s-1PERL\s0\*(R" in \s-1JSON::XS\s0 and \*(L"\s-1UNICODE\s0 \s-1HANDLING\s0 \s-1ON\s0 \s-1PERLS\s0\*(R".
.IP "\(bu" 4
round-trip integrity
.Sp
When you serialise a perl data structure using only data types supported
by \s-1JSON\s0 and Perl, the deserialised data structure is identical on the Perl
level. (e.g. the string \*(L"2.0\*(R" doesn't suddenly become \*(L"2\*(R" just because
it looks like a number). There \fIare\fR minor exceptions to this, read the
\&\s-1MAPPING\s0 section below to learn about those.
.IP "\(bu" 4
strict checking of \s-1JSON\s0 correctness
.Sp
There is no guessing, no generating of illegal \s-1JSON\s0 texts by default,
and only \s-1JSON\s0 is accepted as input by default (the latter is a security feature).
But when some options are set, loose chcking features are available.
.SH "FUNCTIONAL INTERFACE"
.IX Header "FUNCTIONAL INTERFACE"
Some documents are copied and modified from \*(L"\s-1FUNCTIONAL\s0 \s-1INTERFACE\s0\*(R" in \s-1JSON::XS\s0.
.SS "encode_json"
.IX Subsection "encode_json"
.Vb 1
\& $json_text = encode_json $perl_scalar
.Ve
.PP
Converts the given Perl data structure to a \s-1UTF\-8\s0 encoded, binary string.
.PP
This function call is functionally identical to:
.PP
.Vb 1
\& $json_text = JSON::PP\->new\->utf8\->encode($perl_scalar)
.Ve
.SS "decode_json"
.IX Subsection "decode_json"
.Vb 1
\& $perl_scalar = decode_json $json_text
.Ve
.PP
The opposite of \f(CW\*(C`encode_json\*(C'\fR: expects an \s-1UTF\-8\s0 (binary) string and tries
to parse that as an \s-1UTF\-8\s0 encoded \s-1JSON\s0 text, returning the resulting
reference.
.PP
This function call is functionally identical to:
.PP
.Vb 1
\& $perl_scalar = JSON::PP\->new\->utf8\->decode($json_text)
.Ve
.SS "JSON::PP::is_bool"
.IX Subsection "JSON::PP::is_bool"
.Vb 1
\& $is_boolean = JSON::PP::is_bool($scalar)
.Ve
.PP
Returns true if the passed scalar represents either JSON::PP::true or
JSON::PP::false, two constants that act like \f(CW1\fR and \f(CW0\fR respectively
and are also used to represent \s-1JSON\s0 \f(CW\*(C`true\*(C'\fR and \f(CW\*(C`false\*(C'\fR in Perl strings.
.SS "JSON::PP::true"
.IX Subsection "JSON::PP::true"
Returns \s-1JSON\s0 true value which is blessed object.
It \f(CW\*(C`isa\*(C'\fR JSON::PP::Boolean object.
.SS "JSON::PP::false"
.IX Subsection "JSON::PP::false"
Returns \s-1JSON\s0 false value which is blessed object.
It \f(CW\*(C`isa\*(C'\fR JSON::PP::Boolean object.
.SS "JSON::PP::null"
.IX Subsection "JSON::PP::null"
Returns \f(CW\*(C`undef\*(C'\fR.
.PP
See \s-1MAPPING\s0, below, for more information on how \s-1JSON\s0 values are mapped to
Perl.
.SH "HOW DO I DECODE A DATA FROM OUTER AND ENCODE TO OUTER"
.IX Header "HOW DO I DECODE A DATA FROM OUTER AND ENCODE TO OUTER"
This section supposes that your perl vresion is 5.8 or later.
.PP
If you know a \s-1JSON\s0 text from an outer world \- a network, a file content, and so on,
is encoded in \s-1UTF\-8\s0, you should use \f(CW\*(C`decode_json\*(C'\fR or \f(CW\*(C`JSON\*(C'\fR module object
with \f(CW\*(C`utf8\*(C'\fR enable. And the decoded result will contain \s-1UNICODE\s0 characters.
.PP
.Vb 4
\& # from network
\& my $json = JSON::PP\->new\->utf8;
\& my $json_text = CGI\->new\->param( \*(Aqjson_data\*(Aq );
\& my $perl_scalar = $json\->decode( $json_text );
\&
\& # from file content
\& local $/;
\& open( my $fh, \*(Aq<\*(Aq, \*(Aqjson.data\*(Aq );
\& $json_text = <$fh>;
\& $perl_scalar = decode_json( $json_text );
.Ve
.PP
If an outer data is not encoded in \s-1UTF\-8\s0, firstly you should \f(CW\*(C`decode\*(C'\fR it.
.PP
.Vb 5
\& use Encode;
\& local $/;
\& open( my $fh, \*(Aq<\*(Aq, \*(Aqjson.data\*(Aq );
\& my $encoding = \*(Aqcp932\*(Aq;
\& my $unicode_json_text = decode( $encoding, <$fh> ); # UNICODE
\&
\& # or you can write the below code.
\& #
\& # open( my $fh, "<:encoding($encoding)", \*(Aqjson.data\*(Aq );
\& # $unicode_json_text = <$fh>;
.Ve
.PP
In this case, \f(CW$unicode_json_text\fR is of course \s-1UNICODE\s0 string.
So you \fBcannot\fR use \f(CW\*(C`decode_json\*(C'\fR nor \f(CW\*(C`JSON\*(C'\fR module object with \f(CW\*(C`utf8\*(C'\fR enable.
Instead of them, you use \f(CW\*(C`JSON\*(C'\fR module object with \f(CW\*(C`utf8\*(C'\fR disable.
.PP
.Vb 1
\& $perl_scalar = $json\->utf8(0)\->decode( $unicode_json_text );
.Ve
.PP
Or \f(CW\*(C`encode \*(Aqutf8\*(Aq\*(C'\fR and \f(CW\*(C`decode_json\*(C'\fR:
.PP
.Vb 2
\& $perl_scalar = decode_json( encode( \*(Aqutf8\*(Aq, $unicode_json_text ) );
\& # this way is not efficient.
.Ve
.PP
And now, you want to convert your \f(CW$perl_scalar\fR into \s-1JSON\s0 data and
send it to an outer world \- a network or a file content, and so on.
.PP
Your data usually contains \s-1UNICODE\s0 strings and you want the converted data to be encoded
in \s-1UTF\-8\s0, you should use \f(CW\*(C`encode_json\*(C'\fR or \f(CW\*(C`JSON\*(C'\fR module object with \f(CW\*(C`utf8\*(C'\fR enable.
.PP
.Vb 3
\& print encode_json( $perl_scalar ); # to a network? file? or display?
\& # or
\& print $json\->utf8\->encode( $perl_scalar );
.Ve
.PP
If \f(CW$perl_scalar\fR does not contain \s-1UNICODE\s0 but \f(CW$encoding\fR\-encoded strings
for some reason, then its characters are regarded as \fBlatin1\fR for perl
(because it does not concern with your \f(CW$encoding\fR).
You \fBcannot\fR use \f(CW\*(C`encode_json\*(C'\fR nor \f(CW\*(C`JSON\*(C'\fR module object with \f(CW\*(C`utf8\*(C'\fR enable.
Instead of them, you use \f(CW\*(C`JSON\*(C'\fR module object with \f(CW\*(C`utf8\*(C'\fR disable.
Note that the resulted text is a \s-1UNICODE\s0 string but no problem to print it.
.PP
.Vb 4
\& # $perl_scalar contains $encoding encoded string values
\& $unicode_json_text = $json\->utf8(0)\->encode( $perl_scalar );
\& # $unicode_json_text consists of characters less than 0x100
\& print $unicode_json_text;
.Ve
.PP
Or \f(CW\*(C`decode $encoding\*(C'\fR all string values and \f(CW\*(C`encode_json\*(C'\fR:
.PP
.Vb 3
\& $perl_scalar\->{ foo } = decode( $encoding, $perl_scalar\->{ foo } );
\& # ... do it to each string values, then encode_json
\& $json_text = encode_json( $perl_scalar );
.Ve
.PP
This method is a proper way but probably not efficient.
.PP
See to Encode, perluniintro.
.SH "METHODS"
.IX Header "METHODS"
Basically, check to \s-1JSON\s0 or \s-1JSON::XS\s0.
.SS "new"
.IX Subsection "new"
.Vb 1
\& $json = JSON::PP\->new
.Ve
.PP
Rturns a new \s-1JSON::PP\s0 object that can be used to de/encode \s-1JSON\s0
strings.
.PP
All boolean flags described below are by default \fIdisabled\fR.
.PP
The mutators for flags all return the \s-1JSON\s0 object again and thus calls can
be chained:
.PP
.Vb 2
\& my $json = JSON::PP\->new\->utf8\->space_after\->encode({a => [1,2]})
\& => {"a": [1, 2]}
.Ve
.SS "ascii"
.IX Subsection "ascii"
.Vb 1
\& $json = $json\->ascii([$enable])
\&
\& $enabled = $json\->get_ascii
.Ve
.PP
If \f(CW$enable\fR is true (or missing), then the encode method will not generate characters outside
the code range 0..127. Any Unicode characters outside that range will be escaped using either
a single \euXXXX or a double \euHHHH\euLLLLL escape sequence, as per \s-1RFC4627\s0.
(See to \*(L"OBJECT-ORIENTED \s-1INTERFACE\s0\*(R" in \s-1JSON::XS\s0).
.PP
In Perl 5.005, there is no character having high value (more than 255).
See to \*(L"\s-1UNICODE\s0 \s-1HANDLING\s0 \s-1ON\s0 \s-1PERLS\s0\*(R".
.PP
If \f(CW$enable\fR is false, then the encode method will not escape Unicode characters unless
required by the \s-1JSON\s0 syntax or other flags. This results in a faster and more compact format.
.PP
.Vb 2
\& JSON::PP\->new\->ascii(1)\->encode([chr 0x10401])
\& => ["\eud801\eudc01"]
.Ve
.SS "latin1"
.IX Subsection "latin1"
.Vb 1
\& $json = $json\->latin1([$enable])
\&
\& $enabled = $json\->get_latin1
.Ve
.PP
If \f(CW$enable\fR is true (or missing), then the encode method will encode the resulting \s-1JSON\s0
text as latin1 (or iso\-8859\-1), escaping any characters outside the code range 0..255.
.PP
If \f(CW$enable\fR is false, then the encode method will not escape Unicode characters
unless required by the \s-1JSON\s0 syntax or other flags.
.PP
.Vb 2
\& JSON::XS\->new\->latin1\->encode (["\ex{89}\ex{abc}"]
\& => ["\ex{89}\e\eu0abc"] # (perl syntax, U+abc escaped, U+89 not)
.Ve
.PP
See to \*(L"\s-1UNICODE\s0 \s-1HANDLING\s0 \s-1ON\s0 \s-1PERLS\s0\*(R".
.SS "utf8"
.IX Subsection "utf8"
.Vb 1
\& $json = $json\->utf8([$enable])
\&
\& $enabled = $json\->get_utf8
.Ve
.PP
If \f(CW$enable\fR is true (or missing), then the encode method will encode the \s-1JSON\s0 result
into \s-1UTF\-8\s0, as required by many protocols, while the decode method expects to be handled
an UTF\-8\-encoded string. Please note that UTF\-8\-encoded strings do not contain any
characters outside the range 0..255, they are thus useful for bytewise/binary I/O.
.PP
(In Perl 5.005, any character outside the range 0..255 does not exist.
See to \*(L"\s-1UNICODE\s0 \s-1HANDLING\s0 \s-1ON\s0 \s-1PERLS\s0\*(R".)
.PP
In future versions, enabling this option might enable autodetection of the \s-1UTF\-16\s0 and \s-1UTF\-32\s0
encoding families, as described in \s-1RFC4627\s0.
.PP
If \f(CW$enable\fR is false, then the encode method will return the \s-1JSON\s0 string as a (non-encoded)
Unicode string, while decode expects thus a Unicode string. Any decoding or encoding
(e.g. to \s-1UTF\-8\s0 or \s-1UTF\-16\s0) needs to be done yourself, e.g. using the Encode module.
.PP
Example, output UTF\-16BE\-encoded \s-1JSON:\s0
.PP
.Vb 2
\& use Encode;
\& $jsontext = encode "UTF\-16BE", JSON::PP\->new\->encode ($object);
.Ve
.PP
Example, decode UTF\-32LE\-encoded \s-1JSON:\s0
.PP
.Vb 2
\& use Encode;
\& $object = JSON::PP\->new\->decode (decode "UTF\-32LE", $jsontext);
.Ve
.SS "pretty"
.IX Subsection "pretty"
.Vb 1
\& $json = $json\->pretty([$enable])
.Ve
.PP
This enables (or disables) all of the \f(CW\*(C`indent\*(C'\fR, \f(CW\*(C`space_before\*(C'\fR and
\&\f(CW\*(C`space_after\*(C'\fR flags in one call to generate the most readable
(or most compact) form possible.
.PP
Equivalent to:
.PP
.Vb 1
\& $json\->indent\->space_before\->space_after
.Ve
.SS "indent"
.IX Subsection "indent"
.Vb 1
\& $json = $json\->indent([$enable])
\&
\& $enabled = $json\->get_indent
.Ve
.PP
The default indent space length is three.
You can use \f(CW\*(C`indent_length\*(C'\fR to change the length.
.SS "space_before"
.IX Subsection "space_before"
.Vb 1
\& $json = $json\->space_before([$enable])
\&
\& $enabled = $json\->get_space_before
.Ve
.PP
If \f(CW$enable\fR is true (or missing), then the \f(CW\*(C`encode\*(C'\fR method will add an extra
optional space before the \f(CW\*(C`:\*(C'\fR separating keys from values in \s-1JSON\s0 objects.
.PP
If \f(CW$enable\fR is false, then the \f(CW\*(C`encode\*(C'\fR method will not add any extra
space at those places.
.PP
This setting has no effect when decoding \s-1JSON\s0 texts.
.PP
Example, space_before enabled, space_after and indent disabled:
.PP
.Vb 1
\& {"key" :"value"}
.Ve
.SS "space_after"
.IX Subsection "space_after"
.Vb 1
\& $json = $json\->space_after([$enable])
\&
\& $enabled = $json\->get_space_after
.Ve
.PP
If \f(CW$enable\fR is true (or missing), then the \f(CW\*(C`encode\*(C'\fR method will add an extra
optional space after the \f(CW\*(C`:\*(C'\fR separating keys from values in \s-1JSON\s0 objects
and extra whitespace after the \f(CW\*(C`,\*(C'\fR separating key-value pairs and array
members.
.PP
If \f(CW$enable\fR is false, then the \f(CW\*(C`encode\*(C'\fR method will not add any extra
space at those places.
.PP
This setting has no effect when decoding \s-1JSON\s0 texts.
.PP
Example, space_before and indent disabled, space_after enabled:
.PP
.Vb 1
\& {"key": "value"}
.Ve
.SS "relaxed"
.IX Subsection "relaxed"
.Vb 1
\& $json = $json\->relaxed([$enable])
\&
\& $enabled = $json\->get_relaxed
.Ve
.PP
If \f(CW$enable\fR is true (or missing), then \f(CW\*(C`decode\*(C'\fR will accept some
extensions to normal \s-1JSON\s0 syntax (see below). \f(CW\*(C`encode\*(C'\fR will not be
affected in anyway. \fIBe aware that this option makes you accept invalid
\&\s-1JSON\s0 texts as if they were valid!\fR. I suggest only to use this option to
parse application-specific files written by humans (configuration files,
resource files etc.)
.PP
If \f(CW$enable\fR is false (the default), then \f(CW\*(C`decode\*(C'\fR will only accept
valid \s-1JSON\s0 texts.
.PP
Currently accepted extensions are:
.IP "\(bu" 4
list items can have an end-comma
.Sp
\&\s-1JSON\s0 \fIseparates\fR array elements and key-value pairs with commas. This
can be annoying if you write \s-1JSON\s0 texts manually and want to be able to
quickly append elements, so this extension accepts comma at the end of
such items not just between them:
.Sp
.Vb 8
\& [
\& 1,
\& 2, <\- this comma not normally allowed
\& ]
\& {
\& "k1": "v1",
\& "k2": "v2", <\- this comma not normally allowed
\& }
.Ve
.IP "\(bu" 4
shell-style '#'\-comments
.Sp
Whenever \s-1JSON\s0 allows whitespace, shell-style comments are additionally
allowed. They are terminated by the first carriage-return or line-feed
character, after which more white-space and comments are allowed.
.Sp
.Vb 4
\& [
\& 1, # this comment not allowed in JSON
\& # neither this one...
\& ]
.Ve
.SS "canonical"
.IX Subsection "canonical"
.Vb 1
\& $json = $json\->canonical([$enable])
\&
\& $enabled = $json\->get_canonical
.Ve
.PP
If \f(CW$enable\fR is true (or missing), then the \f(CW\*(C`encode\*(C'\fR method will output \s-1JSON\s0 objects
by sorting their keys. This is adding a comparatively high overhead.
.PP
If \f(CW$enable\fR is false, then the \f(CW\*(C`encode\*(C'\fR method will output key-value
pairs in the order Perl stores them (which will likely change between runs
of the same script).
.PP
This option is useful if you want the same data structure to be encoded as
the same \s-1JSON\s0 text (given the same overall settings). If it is disabled,
the same hash might be encoded differently even if contains the same data,
as key-value pairs have no inherent ordering in Perl.
.PP
This setting has no effect when decoding \s-1JSON\s0 texts.
.PP
If you want your own sorting routine, you can give a code referece
or a subroutine name to \f(CW\*(C`sort_by\*(C'\fR. See to \f(CW\*(C`JSON::PP OWN METHODS\*(C'\fR.
.SS "allow_nonref"
.IX Subsection "allow_nonref"
.Vb 1
\& $json = $json\->allow_nonref([$enable])
\&
\& $enabled = $json\->get_allow_nonref
.Ve
.PP
If \f(CW$enable\fR is true (or missing), then the \f(CW\*(C`encode\*(C'\fR method can convert a
non-reference into its corresponding string, number or null \s-1JSON\s0 value,
which is an extension to \s-1RFC4627\s0. Likewise, \f(CW\*(C`decode\*(C'\fR will accept those \s-1JSON\s0
values instead of croaking.
.PP
If \f(CW$enable\fR is false, then the \f(CW\*(C`encode\*(C'\fR method will croak if it isn't
passed an arrayref or hashref, as \s-1JSON\s0 texts must either be an object
or array. Likewise, \f(CW\*(C`decode\*(C'\fR will croak if given something that is not a
\&\s-1JSON\s0 object or array.
.PP
.Vb 2
\& JSON::PP\->new\->allow_nonref\->encode ("Hello, World!")
\& => "Hello, World!"
.Ve
.SS "allow_unknown"
.IX Subsection "allow_unknown"
.Vb 1
\& $json = $json\->allow_unknown ([$enable])
\&
\& $enabled = $json\->get_allow_unknown
.Ve
.PP
If \f(CW$enable\fR is true (or missing), then \*(L"encode\*(R" will *not* throw an
exception when it encounters values it cannot represent in \s-1JSON\s0 (for
example, filehandles) but instead will encode a \s-1JSON\s0 \*(L"null\*(R" value.
Note that blessed objects are not included here and are handled
separately by c