Filename | /home/ss5/perl5/perlbrew/perls/perl-5.22.0/lib/site_perl/5.22.0/x86_64-linux/Cpanel/JSON/XS.pm |
Statements | Executed 14 statements in 790µs |
Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine |
---|---|---|---|---|---|
1001 | 1 | 1 | 10.9ms | 10.9ms | decode (xsub) | Cpanel::JSON::XS::
1000 | 1 | 1 | 7.02ms | 7.02ms | encode (xsub) | Cpanel::JSON::XS::
1002 | 1 | 1 | 2.07ms | 2.07ms | new (xsub) | Cpanel::JSON::XS::
1001 | 2 | 2 | 1.47ms | 1.47ms | DESTROY (xsub) | Cpanel::JSON::XS::
1 | 1 | 1 | 11µs | 39µs | BEGIN@1758 | JSON::XS::Boolean::
1 | 1 | 1 | 9µs | 22µs | BEGIN@171 | Cpanel::JSON::XS::
1 | 1 | 1 | 9µs | 88µs | BEGIN@94 | DynaLoader::
1 | 1 | 1 | 3µs | 3µs | BEGIN@172 | Cpanel::JSON::XS::
1 | 1 | 1 | 3µs | 3µs | END (xsub) | Cpanel::JSON::XS::
2 | 2 | 2 | 1µs | 1µs | utf8 (xsub) | Cpanel::JSON::XS::
1 | 1 | 1 | 300ns | 300ns | canonical (xsub) | Cpanel::JSON::XS::
0 | 0 | 0 | 0s | 0s | false | Cpanel::JSON::XS::
0 | 0 | 0 | 0s | 0s | from_json | Cpanel::JSON::XS::
0 | 0 | 0 | 0s | 0s | is_bool | Cpanel::JSON::XS::
0 | 0 | 0 | 0s | 0s | to_json | Cpanel::JSON::XS::
0 | 0 | 0 | 0s | 0s | true | Cpanel::JSON::XS::
0 | 0 | 0 | 0s | 0s | __ANON__[:1758] | JSON::XS::Boolean::
0 | 0 | 0 | 0s | 0s | __ANON__[:1759] | JSON::XS::Boolean::
0 | 0 | 0 | 0s | 0s | __ANON__[:1760] | JSON::XS::Boolean::
Line | State ments |
Time on line |
Calls | Time in subs |
Code |
---|---|---|---|---|---|
0 | 1 | 88µs | Profile data that couldn't be associated with a specific line: # spent 88µs making 1 call to DynaLoader::BEGIN@94 | ||
1 | 1 | 37µs | =head1 NAME | ||
2 | |||||
3 | Cpanel::JSON::XS - JSON::XS for Cpanel, fast and correct serialising, also for 5.6.2 | ||||
4 | |||||
5 | =head1 SYNOPSIS | ||||
6 | |||||
7 | use Cpanel::JSON::XS; | ||||
8 | |||||
9 | # exported functions, they croak on error | ||||
10 | # and expect/generate UTF-8 | ||||
11 | |||||
12 | $utf8_encoded_json_text = encode_json $perl_hash_or_arrayref; | ||||
13 | $perl_hash_or_arrayref = decode_json $utf8_encoded_json_text; | ||||
14 | |||||
15 | # OO-interface | ||||
16 | |||||
17 | $coder = Cpanel::JSON::XS->new->ascii->pretty->allow_nonref; | ||||
18 | $pretty_printed_unencoded = $coder->encode ($perl_scalar); | ||||
19 | $perl_scalar = $coder->decode ($unicode_json_text); | ||||
20 | |||||
21 | # Note that 5.6 misses most smart utf8 and encoding functionalities | ||||
22 | # of newer releases. | ||||
23 | |||||
24 | # Note that L<JSON::MaybeXS> will automatically use Cpanel::JSON::XS | ||||
25 | # if available, at virtually no speed overhead either, so you should | ||||
26 | # be able to just: | ||||
27 | |||||
28 | use JSON::MaybeXS; | ||||
29 | |||||
30 | # and do the same things, except that you have a pure-perl fallback now. | ||||
31 | |||||
32 | =head1 DESCRIPTION | ||||
33 | |||||
34 | This module converts Perl data structures to JSON and vice versa. Its | ||||
35 | primary goal is to be I<correct> and its secondary goal is to be | ||||
36 | I<fast>. To reach the latter goal it was written in C. | ||||
37 | |||||
38 | As this is the n-th-something JSON module on CPAN, what was the reason | ||||
39 | to write yet another JSON module? While it seems there are many JSON | ||||
40 | modules, none of them correctly handle all corner cases, and in most cases | ||||
41 | their maintainers are unresponsive, gone missing, or not listening to bug | ||||
42 | reports for other reasons. | ||||
43 | |||||
44 | See below for the Cpanel fork. | ||||
45 | |||||
46 | See MAPPING, below, on how Cpanel::JSON::XS maps perl values to JSON | ||||
47 | values and vice versa. | ||||
48 | |||||
49 | =head2 FEATURES | ||||
50 | |||||
51 | =over 4 | ||||
52 | |||||
53 | =item * correct Unicode handling | ||||
54 | |||||
55 | This module knows how to handle Unicode with Perl version higher than 5.8.5, | ||||
56 | documents how and when it does so, and even documents what "correct" means. | ||||
57 | |||||
58 | =item * round-trip integrity | ||||
59 | |||||
60 | When you serialise a perl data structure using only data types supported | ||||
61 | by JSON and Perl, the deserialised data structure is identical on the Perl | ||||
62 | level. (e.g. the string "2.0" doesn't suddenly become "2" just because | ||||
63 | it looks like a number). There I<are> minor exceptions to this, read the | ||||
64 | MAPPING section below to learn about those. | ||||
65 | |||||
66 | =item * strict checking of JSON correctness | ||||
67 | |||||
68 | There is no guessing, no generating of illegal JSON texts by default, | ||||
69 | and only JSON is accepted as input by default (the latter is a security | ||||
70 | feature). | ||||
71 | |||||
72 | =item * fast | ||||
73 | |||||
74 | Compared to other JSON modules and other serialisers such as Storable, | ||||
75 | this module usually compares favourably in terms of speed, too. | ||||
76 | |||||
77 | =item * simple to use | ||||
78 | |||||
79 | This module has both a simple functional interface as well as an object | ||||
80 | oriented interface. | ||||
81 | |||||
82 | =item * reasonably versatile output formats | ||||
83 | |||||
84 | You can choose between the most compact guaranteed-single-line format | ||||
85 | possible (nice for simple line-based protocols), a pure-ASCII format | ||||
86 | (for when your transport is not 8-bit clean, still supports the whole | ||||
87 | Unicode range), or a pretty-printed format (for when you want to read that | ||||
88 | stuff). Or you can combine those features in whatever way you like. | ||||
89 | |||||
90 | =back | ||||
91 | |||||
92 | =head2 cPanel fork | ||||
93 | |||||
94 | # spent 88µs (9+79) within DynaLoader::BEGIN@94 which was called:
# once (9µs+79µs) by XSLoader::load at line 0 | ||||
95 | bugtracker, this cPanel fork sits now on github. | ||||
96 | |||||
97 | src repo: L<https://github.com/rurban/Cpanel-JSON-XS> | ||||
98 | original: L<http://cvs.schmorp.de/JSON-XS/> | ||||
99 | |||||
100 | RT: L<https://github.com/rurban/Cpanel-JSON-XS/issues> | ||||
101 | or L<https://rt.cpan.org/Public/Dist/Display.html?Queue=Cpanel-JSON-XS> | ||||
102 | |||||
103 | B<Changes to JSON::XS> | ||||
104 | |||||
105 | - fixed encode of numbers for dual-vars. Different string representations | ||||
106 | are preserved, but numbers with temporary strings which represent the same number | ||||
107 | are here treated as numbers, not strings. Cpanel::JSON::XS is a bit slower, but | ||||
108 | preserves numeric types better. | ||||
109 | |||||
110 | - different handling of inf/nan. Default now to null, optionally with -DSTRINGIFY_INFNAN | ||||
111 | to "inf"/"nan". | ||||
112 | |||||
113 | - added C<binary> extension, non-JSON and non JSON parsable, allows | ||||
114 | C<\xNN> and C<\NNN> sequences. | ||||
115 | |||||
116 | - 5.6.2 support; sacrificing some utf8 features (assuming bytes all-over), | ||||
117 | no multi-byte unicode characters. | ||||
118 | |||||
119 | - interop for true/false overloading. JSON::XS and JSON::PP representations | ||||
120 | are accepted and JSON::XS accepts Cpanel::JSON::XS booleans [#13] | ||||
121 | |||||
122 | - ithread support. Cpanel::JSON::XS is thread-safe, JSON::XS not | ||||
123 | |||||
124 | - performance optimizations for threaded Perls | ||||
125 | |||||
126 | - additional fixes for: | ||||
127 | |||||
128 | - [cpan #88061] AIX atof without USE_LONG_DOUBLE | ||||
129 | |||||
130 | - #10 unshare_hek crash | ||||
131 | |||||
132 | - #7 avoid re-blessing where possible (e.g. SvREADONLY restricted hashes) | ||||
133 | |||||
134 | - public maintenance and bugtracker | ||||
135 | |||||
136 | - use ppport.h, sanify XS.xs comment styles, harness C coding style | ||||
137 | |||||
138 | - common::sense is optional. When available it is not used in the published | ||||
139 | production module, just during development and testing. | ||||
140 | |||||
141 | - extended testsuite | ||||
142 | |||||
143 | |||||
144 | =cut | ||||
145 | |||||
146 | package Cpanel::JSON::XS; | ||||
147 | |||||
148 | 1 | 200ns | our $VERSION = '3.0115'; | ||
149 | 1 | 4µs | our @ISA = qw(Exporter); | ||
150 | |||||
151 | 1 | 400ns | our @EXPORT = qw(encode_json decode_json to_json from_json); | ||
152 | |||||
153 | sub to_json($@) { | ||||
154 | if ($] >= 5.008) { | ||||
155 | require Carp; | ||||
156 | Carp::croak ("Cpanel::JSON::XS::to_json has been renamed to encode_json, either downgrade to pre-2.0 versions of Cpanel::JSON::XS or rename the call"); | ||||
157 | } else { | ||||
158 | _to_json(@_); | ||||
159 | } | ||||
160 | } | ||||
161 | |||||
162 | sub from_json($@) { | ||||
163 | if ($] >= 5.008) { | ||||
164 | require Carp; | ||||
165 | Carp::croak ("Cpanel::JSON::XS::from_json has been renamed to decode_json, either downgrade to pre-2.0 versions of Cpanel::JSON::XS or rename the call"); | ||||
166 | } else { | ||||
167 | _from_json(@_); | ||||
168 | } | ||||
169 | } | ||||
170 | |||||
171 | 2 | 15µs | 2 | 35µs | # spent 22µs (9+13) within Cpanel::JSON::XS::BEGIN@171 which was called:
# once (9µs+13µs) by JSON::MaybeXS::_choose_json_module at line 171 # spent 22µs making 1 call to Cpanel::JSON::XS::BEGIN@171
# spent 13µs making 1 call to Exporter::import |
172 | 2 | 528µs | 1 | 3µs | # spent 3µs within Cpanel::JSON::XS::BEGIN@172 which was called:
# once (3µs+0s) by JSON::MaybeXS::_choose_json_module at line 172 # spent 3µs making 1 call to Cpanel::JSON::XS::BEGIN@172 |
173 | |||||
174 | =head1 FUNCTIONAL INTERFACE | ||||
175 | |||||
176 | The following convenience methods are provided by this module. They are | ||||
177 | exported by default: | ||||
178 | |||||
179 | =over 4 | ||||
180 | |||||
181 | =item $json_text = encode_json $perl_scalar | ||||
182 | |||||
183 | Converts the given Perl data structure to a UTF-8 encoded, binary string | ||||
184 | (that is, the string contains octets only). Croaks on error. | ||||
185 | |||||
186 | This function call is functionally identical to: | ||||
187 | |||||
188 | $json_text = Cpanel::JSON::XS->new->utf8->encode ($perl_scalar) | ||||
189 | |||||
190 | Except being faster. | ||||
191 | |||||
192 | =item $perl_scalar = decode_json $json_text | ||||
193 | |||||
194 | The opposite of C<encode_json>: expects an UTF-8 (binary) string and tries | ||||
195 | to parse that as an UTF-8 encoded JSON text, returning the resulting | ||||
196 | reference. Croaks on error. | ||||
197 | |||||
198 | This function call is functionally identical to: | ||||
199 | |||||
200 | $perl_scalar = Cpanel::JSON::XS->new->utf8->decode ($json_text) | ||||
201 | |||||
202 | Except being faster. | ||||
203 | |||||
204 | =item $is_boolean = Cpanel::JSON::XS::is_bool $scalar | ||||
205 | |||||
206 | Returns true if the passed scalar represents either C<JSON::XS::true> or | ||||
207 | C<JSON::XS::false>, two constants that act like C<1> and C<0>, respectively | ||||
208 | and are used to represent JSON C<true> and C<false> values in Perl. | ||||
209 | |||||
210 | See MAPPING, below, for more information on how JSON values are mapped to | ||||
211 | Perl. | ||||
212 | |||||
213 | =back | ||||
214 | |||||
215 | =head1 DEPRECATED FUNCTIONS | ||||
216 | |||||
217 | =over | ||||
218 | |||||
219 | =item from_json | ||||
220 | |||||
221 | from_json has been renamed to decode_json | ||||
222 | |||||
223 | =item to_json | ||||
224 | |||||
225 | to_json has been renamed to encode_json | ||||
226 | |||||
227 | =back | ||||
228 | |||||
229 | |||||
230 | =head1 A FEW NOTES ON UNICODE AND PERL | ||||
231 | |||||
232 | Since this often leads to confusion, here are a few very clear words on | ||||
233 | how Unicode works in Perl, modulo bugs. | ||||
234 | |||||
235 | =over 4 | ||||
236 | |||||
237 | =item 1. Perl strings can store characters with ordinal values > 255. | ||||
238 | |||||
239 | This enables you to store Unicode characters as single characters in a | ||||
240 | Perl string - very natural. | ||||
241 | |||||
242 | =item 2. Perl does I<not> associate an encoding with your strings. | ||||
243 | |||||
244 | ... until you force it to, e.g. when matching it against a regex, or | ||||
245 | printing the scalar to a file, in which case Perl either interprets your | ||||
246 | string as locale-encoded text, octets/binary, or as Unicode, depending | ||||
247 | on various settings. In no case is an encoding stored together with your | ||||
248 | data, it is I<use> that decides encoding, not any magical meta data. | ||||
249 | |||||
250 | =item 3. The internal utf-8 flag has no meaning with regards to the | ||||
251 | encoding of your string. | ||||
252 | |||||
253 | =item 4. A "Unicode String" is simply a string where each character can be | ||||
254 | validly interpreted as a Unicode code point. | ||||
255 | |||||
256 | If you have UTF-8 encoded data, it is no longer a Unicode string, but a | ||||
257 | Unicode string encoded in UTF-8, giving you a binary string. | ||||
258 | |||||
259 | =item 5. A string containing "high" (> 255) character values is I<not> a UTF-8 string. | ||||
260 | |||||
261 | =back | ||||
262 | |||||
263 | I hope this helps :) | ||||
264 | |||||
265 | |||||
266 | =head1 OBJECT-ORIENTED INTERFACE | ||||
267 | |||||
268 | The object oriented interface lets you configure your own encoding or | ||||
269 | decoding style, within the limits of supported formats. | ||||
270 | |||||
271 | =over 4 | ||||
272 | |||||
273 | =item $json = new Cpanel::JSON::XS | ||||
274 | |||||
275 | Creates a new JSON object that can be used to de/encode JSON | ||||
276 | strings. All boolean flags described below are by default I<disabled>. | ||||
277 | |||||
278 | The mutators for flags all return the JSON object again and thus calls can | ||||
279 | be chained: | ||||
280 | |||||
281 | my $json = Cpanel::JSON::XS->new->utf8->space_after->encode ({a => [1,2]}) | ||||
282 | => {"a": [1, 2]} | ||||
283 | |||||
284 | =item $json = $json->ascii ([$enable]) | ||||
285 | |||||
286 | =item $enabled = $json->get_ascii | ||||
287 | |||||
288 | If C<$enable> is true (or missing), then the C<encode> method will not | ||||
289 | generate characters outside the code range C<0..127> (which is ASCII). Any | ||||
290 | Unicode characters outside that range will be escaped using either a | ||||
291 | single C<\uXXXX> (BMP characters) or a double C<\uHHHH\uLLLLL> escape sequence, | ||||
292 | as per RFC4627. The resulting encoded JSON text can be treated as a native | ||||
293 | Unicode string, an ascii-encoded, latin1-encoded or UTF-8 encoded string, | ||||
294 | or any other superset of ASCII. | ||||
295 | |||||
296 | If C<$enable> is false, then the C<encode> method will not escape Unicode | ||||
297 | characters unless required by the JSON syntax or other flags. This results | ||||
298 | in a faster and more compact format. | ||||
299 | |||||
300 | See also the section I<ENCODING/CODESET FLAG NOTES> later in this | ||||
301 | document. | ||||
302 | |||||
303 | The main use for this flag is to produce JSON texts that can be | ||||
304 | transmitted over a 7-bit channel, as the encoded JSON texts will not | ||||
305 | contain any 8 bit characters. | ||||
306 | |||||
307 | Cpanel::JSON::XS->new->ascii (1)->encode ([chr 0x10401]) | ||||
308 | => ["\ud801\udc01"] | ||||
309 | |||||
310 | =item $json = $json->latin1 ([$enable]) | ||||
311 | |||||
312 | =item $enabled = $json->get_latin1 | ||||
313 | |||||
314 | If C<$enable> is true (or missing), then the C<encode> method will encode | ||||
315 | the resulting JSON text as latin1 (or iso-8859-1), escaping any characters | ||||
316 | outside the code range C<0..255>. The resulting string can be treated as a | ||||
317 | latin1-encoded JSON text or a native Unicode string. The C<decode> method | ||||
318 | will not be affected in any way by this flag, as C<decode> by default | ||||
319 | expects Unicode, which is a strict superset of latin1. | ||||
320 | |||||
321 | If C<$enable> is false, then the C<encode> method will not escape Unicode | ||||
322 | characters unless required by the JSON syntax or other flags. | ||||
323 | |||||
324 | See also the section I<ENCODING/CODESET FLAG NOTES> later in this | ||||
325 | document. | ||||
326 | |||||
327 | The main use for this flag is efficiently encoding binary data as JSON | ||||
328 | text, as most octets will not be escaped, resulting in a smaller encoded | ||||
329 | size. The disadvantage is that the resulting JSON text is encoded | ||||
330 | in latin1 (and must correctly be treated as such when storing and | ||||
331 | transferring), a rare encoding for JSON. It is therefore most useful when | ||||
332 | you want to store data structures known to contain binary data efficiently | ||||
333 | in files or databases, not when talking to other JSON encoders/decoders. | ||||
334 | |||||
335 | Cpanel::JSON::XS->new->latin1->encode (["\x{89}\x{abc}"] | ||||
336 | => ["\x{89}\\u0abc"] # (perl syntax, U+abc escaped, U+89 not) | ||||
337 | |||||
338 | =item $json = $json->binary ([$enable]) | ||||
339 | |||||
340 | =item $enabled = $json = $json->get_binary | ||||
341 | |||||
342 | If the C<$enable> argument is true (or missing), then the C<encode> | ||||
343 | method will not try to detect an UTF-8 encoding in any JSON string, it | ||||
344 | will strictly interpret it as byte sequence. The result might contain | ||||
345 | new C<\xNN> sequences, which is B<unparsable JSON>. The C<decode> method | ||||
346 | forbids C<\uNNNN> sequences and accepts C<\xNN> and octal C<\NNN> | ||||
347 | sequences. | ||||
348 | |||||
349 | There is also a special logic for perl 5.6 and utf8. 5.6 encodes any | ||||
350 | string to utf-8 automatically when seeing a codepoint >= C<0x80> and | ||||
351 | < C<0x100>. With the binary flag enabled decode the perl utf8 encoded | ||||
352 | string to the original byte encoding and encode this with C<\xNN> | ||||
353 | escapes. This will result to the same encodings as with newer | ||||
354 | perls. But note that binary multi-byte codepoints with 5.6 will | ||||
355 | result in C<illegal unicode character in binary string> errors, | ||||
356 | unlike with newer perls. | ||||
357 | |||||
358 | If C<$enable> is false, then the C<encode> method will smartly try to | ||||
359 | detect Unicode characters unless required by the JSON syntax or other | ||||
360 | flags and hex and octal sequences are forbidden. | ||||
361 | |||||
362 | See also the section I<ENCODING/CODESET FLAG NOTES> later in this | ||||
363 | document. | ||||
364 | |||||
365 | The main use for this flag is to avoid the smart unicode detection and | ||||
366 | possible double encoding. The disadvantage is that the resulting JSON | ||||
367 | text is encoded in new C<\xNN> and in latin1 characters and must | ||||
368 | correctly be treated as such when storing and transferring, a rare | ||||
369 | encoding for JSON. It will produce non-readable JSON strings in the | ||||
370 | browser. It is therefore most useful when you want to store data | ||||
371 | structures known to contain binary data efficiently in files or | ||||
372 | databases, not when talking to other JSON encoders/decoders. The | ||||
373 | binary decoding method can also be used when an encoder produced a | ||||
374 | non-JSON conformant hex or octal encoding C<\xNN> or C<\NNN>. | ||||
375 | |||||
376 | Cpanel::JSON::XS->new->binary->encode (["\x{89}\x{abc}"]) | ||||
377 | 5.6: Error: malformed or illegal unicode character in binary string | ||||
378 | >=5.8: ['\x89\xe0\xaa\xbc'] | ||||
379 | |||||
380 | Cpanel::JSON::XS->new->binary->encode (["\x{89}\x{bc}"]) | ||||
381 | => ["\x89\xbc"] | ||||
382 | |||||
383 | Cpanel::JSON::XS->new->binary->decode (["\x89\ua001"]) | ||||
384 | Error: malformed or illegal unicode character in binary string | ||||
385 | |||||
386 | Cpanel::JSON::XS->new->decode (["\x89"]) | ||||
387 | Error: illegal hex character in non-binary string | ||||
388 | |||||
389 | =item $json = $json->utf8 ([$enable]) | ||||
390 | |||||
391 | =item $enabled = $json->get_utf8 | ||||
392 | |||||
393 | If C<$enable> is true (or missing), then the C<encode> method will encode | ||||
394 | the JSON result into UTF-8, as required by many protocols, while the | ||||
395 | C<decode> method expects to be handled an UTF-8-encoded string. Please | ||||
396 | note that UTF-8-encoded strings do not contain any characters outside the | ||||
397 | range C<0..255>, they are thus useful for bytewise/binary I/O. In future | ||||
398 | versions, enabling this option might enable autodetection of the UTF-16 | ||||
399 | and UTF-32 encoding families, as described in RFC4627. | ||||
400 | |||||
401 | If C<$enable> is false, then the C<encode> method will return the JSON | ||||
402 | string as a (non-encoded) Unicode string, while C<decode> expects thus a | ||||
403 | Unicode string. Any decoding or encoding (e.g. to UTF-8 or UTF-16) needs | ||||
404 | to be done yourself, e.g. using the Encode module. | ||||
405 | |||||
406 | See also the section I<ENCODING/CODESET FLAG NOTES> later in this | ||||
407 | document. | ||||
408 | |||||
409 | Example, output UTF-16BE-encoded JSON: | ||||
410 | |||||
411 | use Encode; | ||||
412 | $jsontext = encode "UTF-16BE", Cpanel::JSON::XS->new->encode ($object); | ||||
413 | |||||
414 | Example, decode UTF-32LE-encoded JSON: | ||||
415 | |||||
416 | use Encode; | ||||
417 | $object = Cpanel::JSON::XS->new->decode (decode "UTF-32LE", $jsontext); | ||||
418 | |||||
419 | =item $json = $json->pretty ([$enable]) | ||||
420 | |||||
421 | This enables (or disables) all of the C<indent>, C<space_before> and | ||||
422 | C<space_after> (and in the future possibly more) flags in one call to | ||||
423 | generate the most readable (or most compact) form possible. | ||||
424 | |||||
425 | Example, pretty-print some simple structure: | ||||
426 | |||||
427 | my $json = Cpanel::JSON::XS->new->pretty(1)->encode ({a => [1,2]}) | ||||
428 | => | ||||
429 | { | ||||
430 | "a" : [ | ||||
431 | 1, | ||||
432 | 2 | ||||
433 | ] | ||||
434 | } | ||||
435 | |||||
436 | =item $json = $json->indent ([$enable]) | ||||
437 | |||||
438 | =item $enabled = $json->get_indent | ||||
439 | |||||
440 | If C<$enable> is true (or missing), then the C<encode> method will use a multiline | ||||
441 | format as output, putting every array member or object/hash key-value pair | ||||
442 | into its own line, indenting them properly. | ||||
443 | |||||
444 | If C<$enable> is false, no newlines or indenting will be produced, and the | ||||
445 | resulting JSON text is guaranteed not to contain any C<newlines>. | ||||
446 | |||||
447 | This setting has no effect when decoding JSON texts. | ||||
448 | |||||
449 | =item $json = $json->space_before ([$enable]) | ||||
450 | |||||
451 | =item $enabled = $json->get_space_before | ||||
452 | |||||
453 | If C<$enable> is true (or missing), then the C<encode> method will add an extra | ||||
454 | optional space before the C<:> separating keys from values in JSON objects. | ||||
455 | |||||
456 | If C<$enable> is false, then the C<encode> method will not add any extra | ||||
457 | space at those places. | ||||
458 | |||||
459 | This setting has no effect when decoding JSON texts. You will also | ||||
460 | most likely combine this setting with C<space_after>. | ||||
461 | |||||
462 | Example, space_before enabled, space_after and indent disabled: | ||||
463 | |||||
464 | {"key" :"value"} | ||||
465 | |||||
466 | =item $json = $json->space_after ([$enable]) | ||||
467 | |||||
468 | =item $enabled = $json->get_space_after | ||||
469 | |||||
470 | If C<$enable> is true (or missing), then the C<encode> method will add an extra | ||||
471 | optional space after the C<:> separating keys from values in JSON objects | ||||
472 | and extra whitespace after the C<,> separating key-value pairs and array | ||||
473 | members. | ||||
474 | |||||
475 | If C<$enable> is false, then the C<encode> method will not add any extra | ||||
476 | space at those places. | ||||
477 | |||||
478 | This setting has no effect when decoding JSON texts. | ||||
479 | |||||
480 | Example, space_before and indent disabled, space_after enabled: | ||||
481 | |||||
482 | {"key": "value"} | ||||
483 | |||||
484 | =item $json = $json->relaxed ([$enable]) | ||||
485 | |||||
486 | =item $enabled = $json->get_relaxed | ||||
487 | |||||
488 | If C<$enable> is true (or missing), then C<decode> will accept some | ||||
489 | extensions to normal JSON syntax (see below). C<encode> will not be | ||||
490 | affected in anyway. I<Be aware that this option makes you accept invalid | ||||
491 | JSON texts as if they were valid!>. I suggest only to use this option to | ||||
492 | parse application-specific files written by humans (configuration files, | ||||
493 | resource files etc.) | ||||
494 | |||||
495 | If C<$enable> is false (the default), then C<decode> will only accept | ||||
496 | valid JSON texts. | ||||
497 | |||||
498 | Currently accepted extensions are: | ||||
499 | |||||
500 | =over 4 | ||||
501 | |||||
502 | =item * list items can have an end-comma | ||||
503 | |||||
504 | JSON I<separates> array elements and key-value pairs with commas. This | ||||
505 | can be annoying if you write JSON texts manually and want to be able to | ||||
506 | quickly append elements, so this extension accepts comma at the end of | ||||
507 | such items not just between them: | ||||
508 | |||||
509 | [ | ||||
510 | 1, | ||||
511 | 2, <- this comma not normally allowed | ||||
512 | ] | ||||
513 | { | ||||
514 | "k1": "v1", | ||||
515 | "k2": "v2", <- this comma not normally allowed | ||||
516 | } | ||||
517 | |||||
518 | =item * shell-style '#'-comments | ||||
519 | |||||
520 | Whenever JSON allows whitespace, shell-style comments are additionally | ||||
521 | allowed. They are terminated by the first carriage-return or line-feed | ||||
522 | character, after which more white-space and comments are allowed. | ||||
523 | |||||
524 | [ | ||||
525 | 1, # this comment not allowed in JSON | ||||
526 | # neither this one... | ||||
527 | ] | ||||
528 | |||||
529 | =item * literal ASCII TAB characters in strings | ||||
530 | |||||
531 | Literal ASCII TAB characters are now allowed in strings (and treated as | ||||
532 | C<\t>) in relaxed mode. Despite JSON mandates, that TAB character is | ||||
533 | substituted for "\t" sequence. | ||||
534 | |||||
535 | [ | ||||
536 | "Hello\tWorld", | ||||
537 | "Hello<TAB>World", # literal <TAB> would not normally be allowed | ||||
538 | ] | ||||
539 | |||||
540 | =back | ||||
541 | |||||
542 | =item $json = $json->canonical ([$enable]) | ||||
543 | |||||
544 | =item $enabled = $json->get_canonical | ||||
545 | |||||
546 | If C<$enable> is true (or missing), then the C<encode> method will output JSON objects | ||||
547 | by sorting their keys. This is adding a comparatively high overhead. | ||||
548 | |||||
549 | If C<$enable> is false, then the C<encode> method will output key-value | ||||
550 | pairs in the order Perl stores them (which will likely change between runs | ||||
551 | of the same script, and can change even within the same run from 5.18 | ||||
552 | onwards). | ||||
553 | |||||
554 | This option is useful if you want the same data structure to be encoded as | ||||
555 | the same JSON text (given the same overall settings). If it is disabled, | ||||
556 | the same hash might be encoded differently even if contains the same data, | ||||
557 | as key-value pairs have no inherent ordering in Perl. | ||||
558 | |||||
559 | This setting has no effect when decoding JSON texts. | ||||
560 | |||||
561 | This setting has currently no effect on tied hashes. | ||||
562 | |||||
563 | =item $json = $json->allow_nonref ([$enable]) | ||||
564 | |||||
565 | =item $enabled = $json->get_allow_nonref | ||||
566 | |||||
567 | If C<$enable> is true (or missing), then the C<encode> method can convert a | ||||
568 | non-reference into its corresponding string, number or null JSON value, | ||||
569 | which is an extension to RFC4627. Likewise, C<decode> will accept those JSON | ||||
570 | values instead of croaking. | ||||
571 | |||||
572 | If C<$enable> is false, then the C<encode> method will croak if it isn't | ||||
573 | passed an arrayref or hashref, as JSON texts must either be an object | ||||
574 | or array. Likewise, C<decode> will croak if given something that is not a | ||||
575 | JSON object or array. | ||||
576 | |||||
577 | Example, encode a Perl scalar as JSON value with enabled C<allow_nonref>, | ||||
578 | resulting in an invalid JSON text: | ||||
579 | |||||
580 | Cpanel::JSON::XS->new->allow_nonref->encode ("Hello, World!") | ||||
581 | => "Hello, World!" | ||||
582 | |||||
583 | =item $json = $json->allow_unknown ([$enable]) | ||||
584 | |||||
585 | =item $enabled = $json->get_allow_unknown | ||||
586 | |||||
587 | If C<$enable> is true (or missing), then C<encode> will I<not> throw an | ||||
588 | exception when it encounters values it cannot represent in JSON (for | ||||
589 | example, filehandles) but instead will encode a JSON C<null> value. Note | ||||
590 | that blessed objects are not included here and are handled separately by | ||||
591 | c<allow_nonref>. | ||||
592 | |||||
593 | If C<$enable> is false (the default), then C<encode> will throw an | ||||
594 | exception when it encounters anything it cannot encode as JSON. | ||||
595 | |||||
596 | This option does not affect C<decode> in any way, and it is recommended to | ||||
597 | leave it off unless you know your communications partner. | ||||
598 | |||||
599 | =item $json = $json->allow_blessed ([$enable]) | ||||
600 | |||||
601 | =item $enabled = $json->get_allow_blessed | ||||
602 | |||||
603 | If C<$enable> is true (or missing), then the C<encode> method will not | ||||
604 | barf when it encounters a blessed reference. Instead, the value of the | ||||
605 | B<convert_blessed> option will decide whether C<null> (C<convert_blessed> | ||||
606 | disabled or no C<TO_JSON> method found) or a representation of the | ||||
607 | object (C<convert_blessed> enabled and C<TO_JSON> method found) is being | ||||
608 | encoded. Has no effect on C<decode>. | ||||
609 | |||||
610 | If C<$enable> is false (the default), then C<encode> will throw an | ||||
611 | exception when it encounters a blessed object. | ||||
612 | |||||
613 | This setting has no effect on C<decode>. | ||||
614 | |||||
615 | =item $json = $json->convert_blessed ([$enable]) | ||||
616 | |||||
617 | =item $enabled = $json->get_convert_blessed | ||||
618 | |||||
619 | If C<$enable> is true (or missing), then C<encode>, upon encountering a | ||||
620 | blessed object, will check for the availability of the C<TO_JSON> method | ||||
621 | on the object's class. If found, it will be called in scalar context | ||||
622 | and the resulting scalar will be encoded instead of the object. If no | ||||
623 | C<TO_JSON> method is found, the value of C<allow_blessed> will decide what | ||||
624 | to do. | ||||
625 | |||||
626 | The C<TO_JSON> method may safely call die if it wants. If C<TO_JSON> | ||||
627 | returns other blessed objects, those will be handled in the same | ||||
628 | way. C<TO_JSON> must take care of not causing an endless recursion cycle | ||||
629 | (== crash) in this case. The name of C<TO_JSON> was chosen because other | ||||
630 | methods called by the Perl core (== not by the user of the object) are | ||||
631 | usually in upper case letters and to avoid collisions with any C<to_json> | ||||
632 | function or method. | ||||
633 | |||||
634 | If C<$enable> is false (the default), then C<encode> will not consider | ||||
635 | this type of conversion. | ||||
636 | |||||
637 | This setting has no effect on C<decode>. | ||||
638 | |||||
639 | =item $json = $json->allow_tags ([$enable]) | ||||
640 | |||||
641 | =item $enabled = $json->get_allow_tags | ||||
642 | |||||
643 | See L<OBJECT SERIALISATION> for details. | ||||
644 | |||||
645 | If C<$enable> is true (or missing), then C<encode>, upon encountering a | ||||
646 | blessed object, will check for the availability of the C<FREEZE> method on | ||||
647 | the object's class. If found, it will be used to serialise the object into | ||||
648 | a nonstandard tagged JSON value (that JSON decoders cannot decode). | ||||
649 | |||||
650 | It also causes C<decode> to parse such tagged JSON values and deserialise | ||||
651 | them via a call to the C<THAW> method. | ||||
652 | |||||
653 | If C<$enable> is false (the default), then C<encode> will not consider | ||||
654 | this type of conversion, and tagged JSON values will cause a parse error | ||||
655 | in C<decode>, as if tags were not part of the grammar. | ||||
656 | |||||
657 | =item $json = $json->filter_json_object ([$coderef->($hashref)]) | ||||
658 | |||||
659 | When C<$coderef> is specified, it will be called from C<decode> each | ||||
660 | time it decodes a JSON object. The only argument is a reference to the | ||||
661 | newly-created hash. If the code references returns a single scalar (which | ||||
662 | need not be a reference), this value (i.e. a copy of that scalar to avoid | ||||
663 | aliasing) is inserted into the deserialised data structure. If it returns | ||||
664 | an empty list (NOTE: I<not> C<undef>, which is a valid scalar), the | ||||
665 | original deserialised hash will be inserted. This setting can slow down | ||||
666 | decoding considerably. | ||||
667 | |||||
668 | When C<$coderef> is omitted or undefined, any existing callback will | ||||
669 | be removed and C<decode> will not change the deserialised hash in any | ||||
670 | way. | ||||
671 | |||||
672 | Example, convert all JSON objects into the integer 5: | ||||
673 | |||||
674 | my $js = Cpanel::JSON::XS->new->filter_json_object (sub { 5 }); | ||||
675 | # returns [5] | ||||
676 | $js->decode ('[{}]') | ||||
677 | # throw an exception because allow_nonref is not enabled | ||||
678 | # so a lone 5 is not allowed. | ||||
679 | $js->decode ('{"a":1, "b":2}'); | ||||
680 | |||||
681 | =item $json = $json->filter_json_single_key_object ($key [=> $coderef->($value)]) | ||||
682 | |||||
683 | Works remotely similar to C<filter_json_object>, but is only called for | ||||
684 | JSON objects having a single key named C<$key>. | ||||
685 | |||||
686 | This C<$coderef> is called before the one specified via | ||||
687 | C<filter_json_object>, if any. It gets passed the single value in the JSON | ||||
688 | object. If it returns a single value, it will be inserted into the data | ||||
689 | structure. If it returns nothing (not even C<undef> but the empty list), | ||||
690 | the callback from C<filter_json_object> will be called next, as if no | ||||
691 | single-key callback were specified. | ||||
692 | |||||
693 | If C<$coderef> is omitted or undefined, the corresponding callback will be | ||||
694 | disabled. There can only ever be one callback for a given key. | ||||
695 | |||||
696 | As this callback gets called less often then the C<filter_json_object> | ||||
697 | one, decoding speed will not usually suffer as much. Therefore, single-key | ||||
698 | objects make excellent targets to serialise Perl objects into, especially | ||||
699 | as single-key JSON objects are as close to the type-tagged value concept | ||||
700 | as JSON gets (it's basically an ID/VALUE tuple). Of course, JSON does not | ||||
701 | support this in any way, so you need to make sure your data never looks | ||||
702 | like a serialised Perl hash. | ||||
703 | |||||
704 | Typical names for the single object key are C<__class_whatever__>, or | ||||
705 | C<$__dollars_are_rarely_used__$> or C<}ugly_brace_placement>, or even | ||||
706 | things like C<__class_md5sum(classname)__>, to reduce the risk of clashing | ||||
707 | with real hashes. | ||||
708 | |||||
709 | Example, decode JSON objects of the form C<< { "__widget__" => <id> } >> | ||||
710 | into the corresponding C<< $WIDGET{<id>} >> object: | ||||
711 | |||||
712 | # return whatever is in $WIDGET{5}: | ||||
713 | Cpanel::JSON::XS | ||||
714 | ->new | ||||
715 | ->filter_json_single_key_object (__widget__ => sub { | ||||
716 | $WIDGET{ $_[0] } | ||||
717 | }) | ||||
718 | ->decode ('{"__widget__": 5') | ||||
719 | |||||
720 | # this can be used with a TO_JSON method in some "widget" class | ||||
721 | # for serialisation to json: | ||||
722 | sub WidgetBase::TO_JSON { | ||||
723 | my ($self) = @_; | ||||
724 | |||||
725 | unless ($self->{id}) { | ||||
726 | $self->{id} = ..get..some..id..; | ||||
727 | $WIDGET{$self->{id}} = $self; | ||||
728 | } | ||||
729 | |||||
730 | { __widget__ => $self->{id} } | ||||
731 | } | ||||
732 | |||||
733 | =item $json = $json->shrink ([$enable]) | ||||
734 | |||||
735 | =item $enabled = $json->get_shrink | ||||
736 | |||||
737 | Perl usually over-allocates memory a bit when allocating space for | ||||
738 | strings. This flag optionally resizes strings generated by either | ||||
739 | C<encode> or C<decode> to their minimum size possible. This can save | ||||
740 | memory when your JSON texts are either very very long or you have many | ||||
741 | short strings. It will also try to downgrade any strings to octet-form | ||||
742 | if possible: perl stores strings internally either in an encoding called | ||||
743 | UTF-X or in octet-form. The latter cannot store everything but uses less | ||||
744 | space in general (and some buggy Perl or C code might even rely on that | ||||
745 | internal representation being used). | ||||
746 | |||||
747 | The actual definition of what shrink does might change in future versions, | ||||
748 | but it will always try to save space at the expense of time. | ||||
749 | |||||
750 | If C<$enable> is true (or missing), the string returned by C<encode> will | ||||
751 | be shrunk-to-fit, while all strings generated by C<decode> will also be | ||||
752 | shrunk-to-fit. | ||||
753 | |||||
754 | If C<$enable> is false, then the normal perl allocation algorithms are used. | ||||
755 | If you work with your data, then this is likely to be faster. | ||||
756 | |||||
757 | In the future, this setting might control other things, such as converting | ||||
758 | strings that look like integers or floats into integers or floats | ||||
759 | internally (there is no difference on the Perl level), saving space. | ||||
760 | |||||
761 | =item $json = $json->max_depth ([$maximum_nesting_depth]) | ||||
762 | |||||
763 | =item $max_depth = $json->get_max_depth | ||||
764 | |||||
765 | Sets the maximum nesting level (default C<512>) accepted while encoding | ||||
766 | or decoding. If a higher nesting level is detected in JSON text or a Perl | ||||
767 | data structure, then the encoder and decoder will stop and croak at that | ||||
768 | point. | ||||
769 | |||||
770 | Nesting level is defined by number of hash- or arrayrefs that the encoder | ||||
771 | needs to traverse to reach a given point or the number of C<{> or C<[> | ||||
772 | characters without their matching closing parenthesis crossed to reach a | ||||
773 | given character in a string. | ||||
774 | |||||
775 | Setting the maximum depth to one disallows any nesting, so that ensures | ||||
776 | that the object is only a single hash/object or array. | ||||
777 | |||||
778 | If no argument is given, the highest possible setting will be used, which | ||||
779 | is rarely useful. | ||||
780 | |||||
781 | Note that nesting is implemented by recursion in C. The default value has | ||||
782 | been chosen to be as large as typical operating systems allow without | ||||
783 | crashing. | ||||
784 | |||||
785 | See SECURITY CONSIDERATIONS, below, for more info on why this is useful. | ||||
786 | |||||
787 | =item $json = $json->max_size ([$maximum_string_size]) | ||||
788 | |||||
789 | =item $max_size = $json->get_max_size | ||||
790 | |||||
791 | Set the maximum length a JSON text may have (in bytes) where decoding is | ||||
792 | being attempted. The default is C<0>, meaning no limit. When C<decode> | ||||
793 | is called on a string that is longer then this many bytes, it will not | ||||
794 | attempt to decode the string but throw an exception. This setting has no | ||||
795 | effect on C<encode> (yet). | ||||
796 | |||||
797 | If no argument is given, the limit check will be deactivated (same as when | ||||
798 | C<0> is specified). | ||||
799 | |||||
800 | See SECURITY CONSIDERATIONS, below, for more info on why this is useful. | ||||
801 | |||||
802 | =item $json->stringify_infnan ([$infnan_mode = 1]) | ||||
803 | |||||
804 | =item $infnan_mode = $json->get_stringify_infnan | ||||
805 | |||||
806 | Get or set how Cpanel::JSON::XS encodes C<inf> or C<nan> for numeric | ||||
807 | values. | ||||
808 | |||||
809 | C<null>: infnan_mode = 0. Similar to most JSON modules in other languages. | ||||
810 | |||||
811 | stringified: infnan_mode = 1. As in Mojo::JSON. | ||||
812 | |||||
813 | inf/nan: infnan_mode = 2. As in JSON::XS, and older releases. Produces invalid JSON. | ||||
814 | |||||
815 | =item $json_text = $json->encode ($perl_scalar) | ||||
816 | |||||
817 | Converts the given Perl data structure (a simple scalar or a reference | ||||
818 | to a hash or array) to its JSON representation. Simple scalars will be | ||||
819 | converted into JSON string or number sequences, while references to arrays | ||||
820 | become JSON arrays and references to hashes become JSON objects. Undefined | ||||
821 | Perl values (e.g. C<undef>) become JSON C<null> values. Neither C<true> | ||||
822 | nor C<false> values will be generated. | ||||
823 | |||||
824 | =item $perl_scalar = $json->decode ($json_text) | ||||
825 | |||||
826 | The opposite of C<encode>: expects a JSON text and tries to parse it, | ||||
827 | returning the resulting simple scalar or reference. Croaks on error. | ||||
828 | |||||
829 | JSON numbers and strings become simple Perl scalars. JSON arrays become | ||||
830 | Perl arrayrefs and JSON objects become Perl hashrefs. C<true> becomes | ||||
831 | C<1>, C<false> becomes C<0> and C<null> becomes C<undef>. | ||||
832 | |||||
833 | =item ($perl_scalar, $characters) = $json->decode_prefix ($json_text) | ||||
834 | |||||
835 | This works like the C<decode> method, but instead of raising an exception | ||||
836 | when there is trailing garbage after the first JSON object, it will | ||||
837 | silently stop parsing there and return the number of characters consumed | ||||
838 | so far. | ||||
839 | |||||
840 | This is useful if your JSON texts are not delimited by an outer protocol | ||||
841 | and you need to know where the JSON text ends. | ||||
842 | |||||
843 | Cpanel::JSON::XS->new->decode_prefix ("[1] the tail") | ||||
844 | => ([], 3) | ||||
845 | |||||
846 | =item $json->to_json ($perl_hash_or_arrayref) | ||||
847 | |||||
848 | Deprecated method for perl 5.8 and newer. Use L<encode_json> instead. | ||||
849 | |||||
850 | =item $json->from_json ($utf8_encoded_json_text) | ||||
851 | |||||
852 | Deprecated method for perl 5.8 and newer. Use L<decode_json> instead. | ||||
853 | |||||
854 | =back | ||||
855 | |||||
856 | |||||
857 | =head1 INCREMENTAL PARSING | ||||
858 | |||||
859 | In some cases, there is the need for incremental parsing of JSON | ||||
860 | texts. While this module always has to keep both JSON text and resulting | ||||
861 | Perl data structure in memory at one time, it does allow you to parse a | ||||
862 | JSON stream incrementally. It does so by accumulating text until it has | ||||
863 | a full JSON object, which it then can decode. This process is similar to | ||||
864 | using C<decode_prefix> to see if a full JSON object is available, but | ||||
865 | is much more efficient (and can be implemented with a minimum of method | ||||
866 | calls). | ||||
867 | |||||
868 | Cpanel::JSON::XS will only attempt to parse the JSON text once it is sure it | ||||
869 | has enough text to get a decisive result, using a very simple but | ||||
870 | truly incremental parser. This means that it sometimes won't stop as | ||||
871 | early as the full parser, for example, it doesn't detect mismatched | ||||
872 | parentheses. The only thing it guarantees is that it starts decoding as | ||||
873 | soon as a syntactically valid JSON text has been seen. This means you need | ||||
874 | to set resource limits (e.g. C<max_size>) to ensure the parser will stop | ||||
875 | parsing in the presence if syntax errors. | ||||
876 | |||||
877 | The following methods implement this incremental parser. | ||||
878 | |||||
879 | =over 4 | ||||
880 | |||||
881 | =item [void, scalar or list context] = $json->incr_parse ([$string]) | ||||
882 | |||||
883 | This is the central parsing function. It can both append new text and | ||||
884 | extract objects from the stream accumulated so far (both of these | ||||
885 | functions are optional). | ||||
886 | |||||
887 | If C<$string> is given, then this string is appended to the already | ||||
888 | existing JSON fragment stored in the C<$json> object. | ||||
889 | |||||
890 | After that, if the function is called in void context, it will simply | ||||
891 | return without doing anything further. This can be used to add more text | ||||
892 | in as many chunks as you want. | ||||
893 | |||||
894 | If the method is called in scalar context, then it will try to extract | ||||
895 | exactly I<one> JSON object. If that is successful, it will return this | ||||
896 | object, otherwise it will return C<undef>. If there is a parse error, | ||||
897 | this method will croak just as C<decode> would do (one can then use | ||||
898 | C<incr_skip> to skip the erroneous part). This is the most common way of | ||||
899 | using the method. | ||||
900 | |||||
901 | And finally, in list context, it will try to extract as many objects | ||||
902 | from the stream as it can find and return them, or the empty list | ||||
903 | otherwise. For this to work, there must be no separators between the JSON | ||||
904 | objects or arrays, instead they must be concatenated back-to-back. If | ||||
905 | an error occurs, an exception will be raised as in the scalar context | ||||
906 | case. Note that in this case, any previously-parsed JSON texts will be | ||||
907 | lost. | ||||
908 | |||||
909 | Example: Parse some JSON arrays/objects in a given string and return | ||||
910 | them. | ||||
911 | |||||
912 | my @objs = Cpanel::JSON::XS->new->incr_parse ("[5][7][1,2]"); | ||||
913 | |||||
914 | =item $lvalue_string = $json->incr_text (>5.8 only) | ||||
915 | |||||
916 | This method returns the currently stored JSON fragment as an lvalue, that | ||||
917 | is, you can manipulate it. This I<only> works when a preceding call to | ||||
918 | C<incr_parse> in I<scalar context> successfully returned an object, and | ||||
919 | 2. only with Perl >= 5.8 | ||||
920 | |||||
921 | Under all other circumstances you must not call this function (I mean | ||||
922 | it. although in simple tests it might actually work, it I<will> fail | ||||
923 | under real world conditions). As a special exception, you can also | ||||
924 | call this method before having parsed anything. | ||||
925 | |||||
926 | This function is useful in two cases: a) finding the trailing text after a | ||||
927 | JSON object or b) parsing multiple JSON objects separated by non-JSON text | ||||
928 | (such as commas). | ||||
929 | |||||
930 | =item $json->incr_skip | ||||
931 | |||||
932 | This will reset the state of the incremental parser and will remove | ||||
933 | the parsed text from the input buffer so far. This is useful after | ||||
934 | C<incr_parse> died, in which case the input buffer and incremental parser | ||||
935 | state is left unchanged, to skip the text parsed so far and to reset the | ||||
936 | parse state. | ||||
937 | |||||
938 | The difference to C<incr_reset> is that only text until the parse error | ||||
939 | occurred is removed. | ||||
940 | |||||
941 | =item $json->incr_reset | ||||
942 | |||||
943 | This completely resets the incremental parser, that is, after this call, | ||||
944 | it will be as if the parser had never parsed anything. | ||||
945 | |||||
946 | This is useful if you want to repeatedly parse JSON objects and want to | ||||
947 | ignore any trailing data, which means you have to reset the parser after | ||||
948 | each successful decode. | ||||
949 | |||||
950 | =back | ||||
951 | |||||
952 | =head2 LIMITATIONS | ||||
953 | |||||
954 | All options that affect decoding are supported, except | ||||
955 | C<allow_nonref>. The reason for this is that it cannot be made to | ||||
956 | work sensibly: JSON objects and arrays are self-delimited, i.e. you can concatenate | ||||
957 | them back to back and still decode them perfectly. This does not hold true | ||||
958 | for JSON numbers, however. | ||||
959 | |||||
960 | For example, is the string C<1> a single JSON number, or is it simply the | ||||
961 | start of C<12>? Or is C<12> a single JSON number, or the concatenation | ||||
962 | of C<1> and C<2>? In neither case you can tell, and this is why Cpanel::JSON::XS | ||||
963 | takes the conservative route and disallows this case. | ||||
964 | |||||
965 | =head2 EXAMPLES | ||||
966 | |||||
967 | Some examples will make all this clearer. First, a simple example that | ||||
968 | works similarly to C<decode_prefix>: We want to decode the JSON object at | ||||
969 | the start of a string and identify the portion after the JSON object: | ||||
970 | |||||
971 | my $text = "[1,2,3] hello"; | ||||
972 | |||||
973 | my $json = new Cpanel::JSON::XS; | ||||
974 | |||||
975 | my $obj = $json->incr_parse ($text) | ||||
976 | or die "expected JSON object or array at beginning of string"; | ||||
977 | |||||
978 | my $tail = $json->incr_text; | ||||
979 | # $tail now contains " hello" | ||||
980 | |||||
981 | Easy, isn't it? | ||||
982 | |||||
983 | Now for a more complicated example: Imagine a hypothetical protocol where | ||||
984 | you read some requests from a TCP stream, and each request is a JSON | ||||
985 | array, without any separation between them (in fact, it is often useful to | ||||
986 | use newlines as "separators", as these get interpreted as whitespace at | ||||
987 | the start of the JSON text, which makes it possible to test said protocol | ||||
988 | with C<telnet>...). | ||||
989 | |||||
990 | Here is how you'd do it (it is trivial to write this in an event-based | ||||
991 | manner): | ||||
992 | |||||
993 | my $json = new Cpanel::JSON::XS; | ||||
994 | |||||
995 | # read some data from the socket | ||||
996 | while (sysread $socket, my $buf, 4096) { | ||||
997 | |||||
998 | # split and decode as many requests as possible | ||||
999 | for my $request ($json->incr_parse ($buf)) { | ||||
1000 | # act on the $request | ||||
1001 | } | ||||
1002 | } | ||||
1003 | |||||
1004 | Another complicated example: Assume you have a string with JSON objects | ||||
1005 | or arrays, all separated by (optional) comma characters (e.g. C<[1],[2], | ||||
1006 | [3]>). To parse them, we have to skip the commas between the JSON texts, | ||||
1007 | and here is where the lvalue-ness of C<incr_text> comes in useful: | ||||
1008 | |||||
1009 | my $text = "[1],[2], [3]"; | ||||
1010 | my $json = new Cpanel::JSON::XS; | ||||
1011 | |||||
1012 | # void context, so no parsing done | ||||
1013 | $json->incr_parse ($text); | ||||
1014 | |||||
1015 | # now extract as many objects as possible. note the | ||||
1016 | # use of scalar context so incr_text can be called. | ||||
1017 | while (my $obj = $json->incr_parse) { | ||||
1018 | # do something with $obj | ||||
1019 | |||||
1020 | # now skip the optional comma | ||||
1021 | $json->incr_text =~ s/^ \s* , //x; | ||||
1022 | } | ||||
1023 | |||||
1024 | Now lets go for a very complex example: Assume that you have a gigantic | ||||
1025 | JSON array-of-objects, many gigabytes in size, and you want to parse it, | ||||
1026 | but you cannot load it into memory fully (this has actually happened in | ||||
1027 | the real world :). | ||||
1028 | |||||
1029 | Well, you lost, you have to implement your own JSON parser. But Cpanel::JSON::XS | ||||
1030 | can still help you: You implement a (very simple) array parser and let | ||||
1031 | JSON decode the array elements, which are all full JSON objects on their | ||||
1032 | own (this wouldn't work if the array elements could be JSON numbers, for | ||||
1033 | example): | ||||
1034 | |||||
1035 | my $json = new Cpanel::JSON::XS; | ||||
1036 | |||||
1037 | # open the monster | ||||
1038 | open my $fh, "<bigfile.json" | ||||
1039 | or die "bigfile: $!"; | ||||
1040 | |||||
1041 | # first parse the initial "[" | ||||
1042 | for (;;) { | ||||
1043 | sysread $fh, my $buf, 65536 | ||||
1044 | or die "read error: $!"; | ||||
1045 | $json->incr_parse ($buf); # void context, so no parsing | ||||
1046 | |||||
1047 | # Exit the loop once we found and removed(!) the initial "[". | ||||
1048 | # In essence, we are (ab-)using the $json object as a simple scalar | ||||
1049 | # we append data to. | ||||
1050 | last if $json->incr_text =~ s/^ \s* \[ //x; | ||||
1051 | } | ||||
1052 | |||||
1053 | # now we have the skipped the initial "[", so continue | ||||
1054 | # parsing all the elements. | ||||
1055 | for (;;) { | ||||
1056 | # in this loop we read data until we got a single JSON object | ||||
1057 | for (;;) { | ||||
1058 | if (my $obj = $json->incr_parse) { | ||||
1059 | # do something with $obj | ||||
1060 | last; | ||||
1061 | } | ||||
1062 | |||||
1063 | # add more data | ||||
1064 | sysread $fh, my $buf, 65536 | ||||
1065 | or die "read error: $!"; | ||||
1066 | $json->incr_parse ($buf); # void context, so no parsing | ||||
1067 | } | ||||
1068 | |||||
1069 | # in this loop we read data until we either found and parsed the | ||||
1070 | # separating "," between elements, or the final "]" | ||||
1071 | for (;;) { | ||||
1072 | # first skip whitespace | ||||
1073 | $json->incr_text =~ s/^\s*//; | ||||
1074 | |||||
1075 | # if we find "]", we are done | ||||
1076 | if ($json->incr_text =~ s/^\]//) { | ||||
1077 | print "finished.\n"; | ||||
1078 | exit; | ||||
1079 | } | ||||
1080 | |||||
1081 | # if we find ",", we can continue with the next element | ||||
1082 | if ($json->incr_text =~ s/^,//) { | ||||
1083 | last; | ||||
1084 | } | ||||
1085 | |||||
1086 | # if we find anything else, we have a parse error! | ||||
1087 | if (length $json->incr_text) { | ||||
1088 | die "parse error near ", $json->incr_text; | ||||
1089 | } | ||||
1090 | |||||
1091 | # else add more data | ||||
1092 | sysread $fh, my $buf, 65536 | ||||
1093 | or die "read error: $!"; | ||||
1094 | $json->incr_parse ($buf); # void context, so no parsing | ||||
1095 | } | ||||
1096 | |||||
1097 | This is a complex example, but most of the complexity comes from the fact | ||||
1098 | that we are trying to be correct (bear with me if I am wrong, I never ran | ||||
1099 | the above example :). | ||||
1100 | |||||
- - | |||||
1103 | =head1 MAPPING | ||||
1104 | |||||
1105 | This section describes how Cpanel::JSON::XS maps Perl values to JSON values and | ||||
1106 | vice versa. These mappings are designed to "do the right thing" in most | ||||
1107 | circumstances automatically, preserving round-tripping characteristics | ||||
1108 | (what you put in comes out as something equivalent). | ||||
1109 | |||||
1110 | For the more enlightened: note that in the following descriptions, | ||||
1111 | lowercase I<perl> refers to the Perl interpreter, while uppercase I<Perl> | ||||
1112 | refers to the abstract Perl language itself. | ||||
1113 | |||||
1114 | |||||
1115 | =head2 JSON -> PERL | ||||
1116 | |||||
1117 | =over 4 | ||||
1118 | |||||
1119 | =item object | ||||
1120 | |||||
1121 | A JSON object becomes a reference to a hash in Perl. No ordering of object | ||||
1122 | keys is preserved (JSON does not preserve object key ordering itself). | ||||
1123 | |||||
1124 | =item array | ||||
1125 | |||||
1126 | A JSON array becomes a reference to an array in Perl. | ||||
1127 | |||||
1128 | =item string | ||||
1129 | |||||
1130 | A JSON string becomes a string scalar in Perl - Unicode codepoints in JSON | ||||
1131 | are represented by the same codepoints in the Perl string, so no manual | ||||
1132 | decoding is necessary. | ||||
1133 | |||||
1134 | =item number | ||||
1135 | |||||
1136 | A JSON number becomes either an integer, numeric (floating point) or | ||||
1137 | string scalar in perl, depending on its range and any fractional parts. On | ||||
1138 | the Perl level, there is no difference between those as Perl handles all | ||||
1139 | the conversion details, but an integer may take slightly less memory and | ||||
1140 | might represent more values exactly than floating point numbers. | ||||
1141 | |||||
1142 | If the number consists of digits only, Cpanel::JSON::XS will try to represent | ||||
1143 | it as an integer value. If that fails, it will try to represent it as | ||||
1144 | a numeric (floating point) value if that is possible without loss of | ||||
1145 | precision. Otherwise it will preserve the number as a string value (in | ||||
1146 | which case you lose roundtripping ability, as the JSON number will be | ||||
1147 | re-encoded to a JSON string). | ||||
1148 | |||||
1149 | Numbers containing a fractional or exponential part will always be | ||||
1150 | represented as numeric (floating point) values, possibly at a loss of | ||||
1151 | precision (in which case you might lose perfect roundtripping ability, but | ||||
1152 | the JSON number will still be re-encoded as a JSON number). | ||||
1153 | |||||
1154 | Note that precision is not accuracy - binary floating point values | ||||
1155 | cannot represent most decimal fractions exactly, and when converting | ||||
1156 | from and to floating point, C<Cpanel::JSON::XS> only guarantees precision | ||||
1157 | up to but not including the least significant bit. | ||||
1158 | |||||
1159 | =item true, false | ||||
1160 | |||||
1161 | These JSON atoms become C<Cpanel::JSON::XS::true> and C<Cpanel::JSON::XS::false>, | ||||
1162 | respectively. They are overloaded to act almost exactly like the numbers | ||||
1163 | C<1> and C<0>. You can check whether a scalar is a JSON boolean by using | ||||
1164 | the C<Cpanel::JSON::XS::is_bool> function. | ||||
1165 | |||||
1166 | =item null | ||||
1167 | |||||
1168 | A JSON null atom becomes C<undef> in Perl. | ||||
1169 | |||||
1170 | =item shell-style comments (C<< # I<text> >>) | ||||
1171 | |||||
1172 | As a nonstandard extension to the JSON syntax that is enabled by the | ||||
1173 | C<relaxed> setting, shell-style comments are allowed. They can start | ||||
1174 | anywhere outside strings and go till the end of the line. | ||||
1175 | |||||
1176 | =item tagged values (C<< (I<tag>)I<value> >>). | ||||
1177 | |||||
1178 | Another nonstandard extension to the JSON syntax, enabled with the | ||||
1179 | C<allow_tags> setting, are tagged values. In this implementation, the | ||||
1180 | I<tag> must be a perl package/class name encoded as a JSON string, and the | ||||
1181 | I<value> must be a JSON array encoding optional constructor arguments. | ||||
1182 | |||||
1183 | See L<OBJECT SERIALISATION>, below, for details. | ||||
1184 | |||||
1185 | =back | ||||
1186 | |||||
1187 | |||||
1188 | =head2 PERL -> JSON | ||||
1189 | |||||
1190 | The mapping from Perl to JSON is slightly more difficult, as Perl is a | ||||
1191 | truly typeless language, so we can only guess which JSON type is meant by | ||||
1192 | a Perl value. | ||||
1193 | |||||
1194 | =over 4 | ||||
1195 | |||||
1196 | =item hash references | ||||
1197 | |||||
1198 | Perl hash references become JSON objects. As there is no inherent ordering | ||||
1199 | in hash keys (or JSON objects), they will usually be encoded in a | ||||
1200 | pseudo-random order that can change between runs of the same program but | ||||
1201 | stays generally the same within a single run of a program. Cpanel::JSON::XS can | ||||
1202 | optionally sort the hash keys (determined by the I<canonical> flag), so | ||||
1203 | the same datastructure will serialise to the same JSON text (given same | ||||
1204 | settings and version of Cpanel::JSON::XS), but this incurs a runtime overhead | ||||
1205 | and is only rarely useful, e.g. when you want to compare some JSON text | ||||
1206 | against another for equality. | ||||
1207 | |||||
1208 | =item array references | ||||
1209 | |||||
1210 | Perl array references become JSON arrays. | ||||
1211 | |||||
1212 | =item other references | ||||
1213 | |||||
1214 | Other unblessed references are generally not allowed and will cause an | ||||
1215 | exception to be thrown, except for references to the integers C<0> and | ||||
1216 | C<1>, which get turned into C<false> and C<true> atoms in JSON. You can | ||||
1217 | also use C<Cpanel::JSON::XS::false> and C<Cpanel::JSON::XS::true> to improve | ||||
1218 | readability. | ||||
1219 | |||||
1220 | encode_json [\0, Cpanel::JSON::XS::true] # yields [false,true] | ||||
1221 | |||||
1222 | =item Cpanel::JSON::XS::true, Cpanel::JSON::XS::false | ||||
1223 | |||||
1224 | These special values become JSON true and JSON false values, | ||||
1225 | respectively. You can also use C<\1> and C<\0> directly if you want. | ||||
1226 | |||||
1227 | =item blessed objects | ||||
1228 | |||||
1229 | Blessed objects are not directly representable in JSON, but | ||||
1230 | C<Cpanel::JSON::XS> allows various ways of handling objects. See | ||||
1231 | L<OBJECT SERIALISATION>, below, for details. | ||||
1232 | |||||
1233 | See the C<allow_blessed> and C<convert_blessed> methods on various options on | ||||
1234 | how to deal with this: basically, you can choose between throwing an | ||||
1235 | exception, encoding the reference as if it weren't blessed, or provide | ||||
1236 | your own serialiser method. | ||||
1237 | |||||
1238 | =item simple scalars | ||||
1239 | |||||
1240 | Simple Perl scalars (any scalar that is not a reference) are the most | ||||
1241 | difficult objects to encode: Cpanel::JSON::XS will encode undefined scalars or inf/nan | ||||
1242 | as JSON C<null> values, scalars that have last been used in a string context | ||||
1243 | before encoding as JSON strings, and anything else as number value: | ||||
1244 | |||||
1245 | # dump as number | ||||
1246 | encode_json [2] # yields [2] | ||||
1247 | encode_json [-3.0e17] # yields [-3e+17] | ||||
1248 | my $value = 5; encode_json [$value] # yields [5] | ||||
1249 | |||||
1250 | # used as string, but the two representations are for the same number | ||||
1251 | print $value; | ||||
1252 | encode_json [$value] # yields [5] | ||||
1253 | |||||
1254 | # used as different string (non-matching dual-var) | ||||
1255 | my $str = '0 but true'; | ||||
1256 | my $num = 1 + $str; | ||||
1257 | encode_json [$num, $str] # yields [1,"0 but true"] | ||||
1258 | |||||
1259 | # undef becomes null | ||||
1260 | encode_json [undef] # yields [null] | ||||
1261 | |||||
1262 | # inf or nan becomes null, unless you answered | ||||
1263 | # "Do you want to handle inf/nan as strings" with yes | ||||
1264 | encode_json [9**9**9] # yields [null] | ||||
1265 | |||||
1266 | You can force the type to be a JSON string by stringifying it: | ||||
1267 | |||||
1268 | my $x = 3.1; # some variable containing a number | ||||
1269 | "$x"; # stringified | ||||
1270 | $x .= ""; # another, more awkward way to stringify | ||||
1271 | print $x; # perl does it for you, too, quite often | ||||
1272 | |||||
1273 | You can force the type to be a JSON number by numifying it: | ||||
1274 | |||||
1275 | my $x = "3"; # some variable containing a string | ||||
1276 | $x += 0; # numify it, ensuring it will be dumped as a number | ||||
1277 | $x *= 1; # same thing, the choice is yours. | ||||
1278 | |||||
1279 | Note that numerical precision has the same meaning as under Perl (so | ||||
1280 | binary to decimal conversion follows the same rules as in Perl, which | ||||
1281 | can differ to other languages). Also, your perl interpreter might expose | ||||
1282 | extensions to the floating point numbers of your platform, such as | ||||
1283 | infinities or NaN's - these cannot be represented in JSON, and thus | ||||
1284 | null is returned instead. Optionally you can configure it to stringify | ||||
1285 | inf and nan values. | ||||
1286 | |||||
1287 | =back | ||||
1288 | |||||
1289 | =head2 OBJECT SERIALISATION | ||||
1290 | |||||
1291 | As JSON cannot directly represent Perl objects, you have to choose between | ||||
1292 | a pure JSON representation (without the ability to deserialise the object | ||||
1293 | automatically again), and a nonstandard extension to the JSON syntax, | ||||
1294 | tagged values. | ||||
1295 | |||||
1296 | =head3 SERIALISATION | ||||
1297 | |||||
1298 | What happens when C<Cpanel::JSON::XS> encounters a Perl object depends on the | ||||
1299 | C<allow_blessed>, C<convert_blessed> and C<allow_tags> settings, which are | ||||
1300 | used in this order: | ||||
1301 | |||||
1302 | =over 4 | ||||
1303 | |||||
1304 | =item 1. C<allow_tags> is enabled and the object has a C<FREEZE> method. | ||||
1305 | |||||
1306 | In this case, C<Cpanel::JSON::XS> uses the L<Types::Serialiser> object | ||||
1307 | serialisation protocol to create a tagged JSON value, using a nonstandard | ||||
1308 | extension to the JSON syntax. | ||||
1309 | |||||
1310 | This works by invoking the C<FREEZE> method on the object, with the first | ||||
1311 | argument being the object to serialise, and the second argument being the | ||||
1312 | constant string C<JSON> to distinguish it from other serialisers. | ||||
1313 | |||||
1314 | The C<FREEZE> method can return any number of values (i.e. zero or | ||||
1315 | more). These values and the paclkage/classname of the object will then be | ||||
1316 | encoded as a tagged JSON value in the following format: | ||||
1317 | |||||
1318 | ("classname")[FREEZE return values...] | ||||
1319 | |||||
1320 | e.g.: | ||||
1321 | |||||
1322 | ("URI")["http://www.google.com/"] | ||||
1323 | ("MyDate")[2013,10,29] | ||||
1324 | ("ImageData::JPEG")["Z3...VlCg=="] | ||||
1325 | |||||
1326 | For example, the hypothetical C<My::Object> C<FREEZE> method might use the | ||||
1327 | objects C<type> and C<id> members to encode the object: | ||||
1328 | |||||
1329 | sub My::Object::FREEZE { | ||||
1330 | my ($self, $serialiser) = @_; | ||||
1331 | |||||
1332 | ($self->{type}, $self->{id}) | ||||
1333 | } | ||||
1334 | |||||
1335 | =item 2. C<convert_blessed> is enabled and the object has a C<TO_JSON> method. | ||||
1336 | |||||
1337 | In this case, the C<TO_JSON> method of the object is invoked in scalar | ||||
1338 | context. It must return a single scalar that can be directly encoded into | ||||
1339 | JSON. This scalar replaces the object in the JSON text. | ||||
1340 | |||||
1341 | For example, the following C<TO_JSON> method will convert all L<URI> | ||||
1342 | objects to JSON strings when serialised. The fatc that these values | ||||
1343 | originally were L<URI> objects is lost. | ||||
1344 | |||||
1345 | sub URI::TO_JSON { | ||||
1346 | my ($uri) = @_; | ||||
1347 | $uri->as_string | ||||
1348 | } | ||||
1349 | |||||
1350 | =item 3. C<allow_blessed> is enabled. | ||||
1351 | |||||
1352 | The object will be serialised as a JSON null value. | ||||
1353 | |||||
1354 | =item 4. none of the above | ||||
1355 | |||||
1356 | If none of the settings are enabled or the respective methods are missing, | ||||
1357 | C<Cpanel::JSON::XS> throws an exception. | ||||
1358 | |||||
1359 | =back | ||||
1360 | |||||
1361 | =head3 DESERIALISATION | ||||
1362 | |||||
1363 | For deserialisation there are only two cases to consider: either | ||||
1364 | nonstandard tagging was used, in which case C<allow_tags> decides, | ||||
1365 | or objects cannot be automatically be deserialised, in which | ||||
1366 | case you can use postprocessing or the C<filter_json_object> or | ||||
1367 | C<filter_json_single_key_object> callbacks to get some real objects our of | ||||
1368 | your JSON. | ||||
1369 | |||||
1370 | This section only considers the tagged value case: I a tagged JSON object | ||||
1371 | is encountered during decoding and C<allow_tags> is disabled, a parse | ||||
1372 | error will result (as if tagged values were not part of the grammar). | ||||
1373 | |||||
1374 | If C<allow_tags> is enabled, C<Cpanel::JSON::XS> will look up the C<THAW> method | ||||
1375 | of the package/classname used during serialisation (it will not attempt | ||||
1376 | to load the package as a Perl module). If there is no such method, the | ||||
1377 | decoding will fail with an error. | ||||
1378 | |||||
1379 | Otherwise, the C<THAW> method is invoked with the classname as first | ||||
1380 | argument, the constant string C<JSON> as second argument, and all the | ||||
1381 | values from the JSON array (the values originally returned by the | ||||
1382 | C<FREEZE> method) as remaining arguments. | ||||
1383 | |||||
1384 | The method must then return the object. While technically you can return | ||||
1385 | any Perl scalar, you might have to enable the C<enable_nonref> setting to | ||||
1386 | make that work in all cases, so better return an actual blessed reference. | ||||
1387 | |||||
1388 | As an example, let's implement a C<THAW> function that regenerates the | ||||
1389 | C<My::Object> from the C<FREEZE> example earlier: | ||||
1390 | |||||
1391 | sub My::Object::THAW { | ||||
1392 | my ($class, $serialiser, $type, $id) = @_; | ||||
1393 | |||||
1394 | $class->new (type => $type, id => $id) | ||||
1395 | } | ||||
1396 | |||||
1397 | |||||
1398 | =head1 ENCODING/CODESET FLAG NOTES | ||||
1399 | |||||
1400 | The interested reader might have seen a number of flags that signify | ||||
1401 | encodings or codesets - C<utf8>, C<latin1>, C<binary> and | ||||
1402 | C<ascii>. There seems to be some confusion on what these do, so here | ||||
1403 | is a short comparison: | ||||
1404 | |||||
1405 | C<utf8> controls whether the JSON text created by C<encode> (and expected | ||||
1406 | by C<decode>) is UTF-8 encoded or not, while C<latin1> and C<ascii> only | ||||
1407 | control whether C<encode> escapes character values outside their respective | ||||
1408 | codeset range. Neither of these flags conflict with each other, although | ||||
1409 | some combinations make less sense than others. | ||||
1410 | |||||
1411 | Care has been taken to make all flags symmetrical with respect to | ||||
1412 | C<encode> and C<decode>, that is, texts encoded with any combination of | ||||
1413 | these flag values will be correctly decoded when the same flags are used | ||||
1414 | - in general, if you use different flag settings while encoding vs. when | ||||
1415 | decoding you likely have a bug somewhere. | ||||
1416 | |||||
1417 | Below comes a verbose discussion of these flags. Note that a "codeset" is | ||||
1418 | simply an abstract set of character-codepoint pairs, while an encoding | ||||
1419 | takes those codepoint numbers and I<encodes> them, in our case into | ||||
1420 | octets. Unicode is (among other things) a codeset, UTF-8 is an encoding, | ||||
1421 | and ISO-8859-1 (= latin 1) and ASCII are both codesets I<and> encodings at | ||||
1422 | the same time, which can be confusing. | ||||
1423 | |||||
1424 | =over 4 | ||||
1425 | |||||
1426 | =item C<utf8> flag disabled | ||||
1427 | |||||
1428 | When C<utf8> is disabled (the default), then C<encode>/C<decode> generate | ||||
1429 | and expect Unicode strings, that is, characters with high ordinal Unicode | ||||
1430 | values (> 255) will be encoded as such characters, and likewise such | ||||
1431 | characters are decoded as-is, no changes to them will be done, except | ||||
1432 | "(re-)interpreting" them as Unicode codepoints or Unicode characters, | ||||
1433 | respectively (to Perl, these are the same thing in strings unless you do | ||||
1434 | funny/weird/dumb stuff). | ||||
1435 | |||||
1436 | This is useful when you want to do the encoding yourself (e.g. when you | ||||
1437 | want to have UTF-16 encoded JSON texts) or when some other layer does | ||||
1438 | the encoding for you (for example, when printing to a terminal using a | ||||
1439 | filehandle that transparently encodes to UTF-8 you certainly do NOT want | ||||
1440 | to UTF-8 encode your data first and have Perl encode it another time). | ||||
1441 | |||||
1442 | =item C<utf8> flag enabled | ||||
1443 | |||||
1444 | If the C<utf8>-flag is enabled, C<encode>/C<decode> will encode all | ||||
1445 | characters using the corresponding UTF-8 multi-byte sequence, and will | ||||
1446 | expect your input strings to be encoded as UTF-8, that is, no "character" | ||||
1447 | of the input string must have any value > 255, as UTF-8 does not allow | ||||
1448 | that. | ||||
1449 | |||||
1450 | The C<utf8> flag therefore switches between two modes: disabled means you | ||||
1451 | will get a Unicode string in Perl, enabled means you get an UTF-8 encoded | ||||
1452 | octet/binary string in Perl. | ||||
1453 | |||||
1454 | =item C<latin1>, C<binary> or C<ascii> flags enabled | ||||
1455 | |||||
1456 | With C<latin1> (or C<ascii>) enabled, C<encode> will escape | ||||
1457 | characters with ordinal values > 255 (> 127 with C<ascii>) and encode | ||||
1458 | the remaining characters as specified by the C<utf8> flag. | ||||
1459 | With C<binary> enabled, ordinal values > 255 are illegal. | ||||
1460 | |||||
1461 | If C<utf8> is disabled, then the result is also correctly encoded in those | ||||
1462 | character sets (as both are proper subsets of Unicode, meaning that a | ||||
1463 | Unicode string with all character values < 256 is the same thing as a | ||||
1464 | ISO-8859-1 string, and a Unicode string with all character values < 128 is | ||||
1465 | the same thing as an ASCII string in Perl). | ||||
1466 | |||||
1467 | If C<utf8> is enabled, you still get a correct UTF-8-encoded string, | ||||
1468 | regardless of these flags, just some more characters will be escaped using | ||||
1469 | C<\uXXXX> then before. | ||||
1470 | |||||
1471 | Note that ISO-8859-1-I<encoded> strings are not compatible with UTF-8 | ||||
1472 | encoding, while ASCII-encoded strings are. That is because the ISO-8859-1 | ||||
1473 | encoding is NOT a subset of UTF-8 (despite the ISO-8859-1 I<codeset> being | ||||
1474 | a subset of Unicode), while ASCII is. | ||||
1475 | |||||
1476 | Surprisingly, C<decode> will ignore these flags and so treat all input | ||||
1477 | values as governed by the C<utf8> flag. If it is disabled, this allows you | ||||
1478 | to decode ISO-8859-1- and ASCII-encoded strings, as both strict subsets of | ||||
1479 | Unicode. If it is enabled, you can correctly decode UTF-8 encoded strings. | ||||
1480 | |||||
1481 | So neither C<latin1>, C<binary> nor C<ascii> are incompatible with the | ||||
1482 | C<utf8> flag - they only govern when the JSON output engine escapes a | ||||
1483 | character or not. | ||||
1484 | |||||
1485 | The main use for C<latin1> or C<binary> is to relatively efficiently | ||||
1486 | store binary data as JSON, at the expense of breaking compatibility | ||||
1487 | with most JSON decoders. | ||||
1488 | |||||
1489 | The main use for C<ascii> is to force the output to not contain characters | ||||
1490 | with values > 127, which means you can interpret the resulting string | ||||
1491 | as UTF-8, ISO-8859-1, ASCII, KOI8-R or most about any character set and | ||||
1492 | 8-bit-encoding, and still get the same data structure back. This is useful | ||||
1493 | when your channel for JSON transfer is not 8-bit clean or the encoding | ||||
1494 | might be mangled in between (e.g. in mail), and works because ASCII is a | ||||
1495 | proper subset of most 8-bit and multibyte encodings in use in the world. | ||||
1496 | |||||
1497 | =back | ||||
1498 | |||||
1499 | |||||
1500 | =head2 JSON and ECMAscript | ||||
1501 | |||||
1502 | JSON syntax is based on how literals are represented in javascript (the | ||||
1503 | not-standardised predecessor of ECMAscript) which is presumably why it is | ||||
1504 | called "JavaScript Object Notation". | ||||
1505 | |||||
1506 | However, JSON is not a subset (and also not a superset of course) of | ||||
1507 | ECMAscript (the standard) or javascript (whatever browsers actually | ||||
1508 | implement). | ||||
1509 | |||||
1510 | If you want to use javascript's C<eval> function to "parse" JSON, you | ||||
1511 | might run into parse errors for valid JSON texts, or the resulting data | ||||
1512 | structure might not be queryable: | ||||
1513 | |||||
1514 | One of the problems is that U+2028 and U+2029 are valid characters inside | ||||
1515 | JSON strings, but are not allowed in ECMAscript string literals, so the | ||||
1516 | following Perl fragment will not output something that can be guaranteed | ||||
1517 | to be parsable by javascript's C<eval>: | ||||
1518 | |||||
1519 | use Cpanel::JSON::XS; | ||||
1520 | |||||
1521 | print encode_json [chr 0x2028]; | ||||
1522 | |||||
1523 | The right fix for this is to use a proper JSON parser in your javascript | ||||
1524 | programs, and not rely on C<eval> (see for example Douglas Crockford's | ||||
1525 | F<json2.js> parser). | ||||
1526 | |||||
1527 | If this is not an option, you can, as a stop-gap measure, simply encode to | ||||
1528 | ASCII-only JSON: | ||||
1529 | |||||
1530 | use Cpanel::JSON::XS; | ||||
1531 | |||||
1532 | print Cpanel::JSON::XS->new->ascii->encode ([chr 0x2028]); | ||||
1533 | |||||
1534 | Note that this will enlarge the resulting JSON text quite a bit if you | ||||
1535 | have many non-ASCII characters. You might be tempted to run some regexes | ||||
1536 | to only escape U+2028 and U+2029, e.g.: | ||||
1537 | |||||
1538 | # DO NOT USE THIS! | ||||
1539 | my $json = Cpanel::JSON::XS->new->utf8->encode ([chr 0x2028]); | ||||
1540 | $json =~ s/\xe2\x80\xa8/\\u2028/g; # escape U+2028 | ||||
1541 | $json =~ s/\xe2\x80\xa9/\\u2029/g; # escape U+2029 | ||||
1542 | print $json; | ||||
1543 | |||||
1544 | Note that I<this is a bad idea>: the above only works for U+2028 and | ||||
1545 | U+2029 and thus only for fully ECMAscript-compliant parsers. Many existing | ||||
1546 | javascript implementations, however, have issues with other characters as | ||||
1547 | well - using C<eval> naively simply I<will> cause problems. | ||||
1548 | |||||
1549 | Another problem is that some javascript implementations reserve | ||||
1550 | some property names for their own purposes (which probably makes | ||||
1551 | them non-ECMAscript-compliant). For example, Iceweasel reserves the | ||||
1552 | C<__proto__> property name for its own purposes. | ||||
1553 | |||||
1554 | If that is a problem, you could parse try to filter the resulting JSON | ||||
1555 | output for these property strings, e.g.: | ||||
1556 | |||||
1557 | $json =~ s/"__proto__"\s*:/"__proto__renamed":/g; | ||||
1558 | |||||
1559 | This works because C<__proto__> is not valid outside of strings, so every | ||||
1560 | occurrence of C<"__proto__"\s*:> must be a string used as property name. | ||||
1561 | |||||
1562 | If you know of other incompatibilities, please let me know. | ||||
1563 | |||||
1564 | |||||
1565 | =head2 JSON and YAML | ||||
1566 | |||||
1567 | You often hear that JSON is a subset of YAML. I<in general, there is | ||||
1568 | no way to configure JSON::XS to output a data structure as valid YAML> | ||||
1569 | that works in all cases. If you really must use Cpanel::JSON::XS to | ||||
1570 | generate YAML, you should use this algorithm (subject to change in | ||||
1571 | future versions): | ||||
1572 | |||||
1573 | my $to_yaml = Cpanel::JSON::XS->new->utf8->space_after (1); | ||||
1574 | my $yaml = $to_yaml->encode ($ref) . "\n"; | ||||
1575 | |||||
1576 | This will I<usually> generate JSON texts that also parse as valid | ||||
1577 | YAML. | ||||
1578 | |||||
1579 | |||||
1580 | =head2 SPEED | ||||
1581 | |||||
1582 | It seems that JSON::XS is surprisingly fast, as shown in the following | ||||
1583 | tables. They have been generated with the help of the C<eg/bench> program | ||||
1584 | in the JSON::XS distribution, to make it easy to compare on your own | ||||
1585 | system. | ||||
1586 | |||||
1587 | JSON::XS is with L<Data::MessagePack> and L<Sereal> one of the fastest | ||||
1588 | serializers, because JSON and JSON::XS do not support backrefs (no | ||||
1589 | graph structures), only trees. Storable supports backrefs, | ||||
1590 | i.e. graphs. Data::MessagePack encodes its data binary (as Storable) | ||||
1591 | and supports only very simple subset of JSON. | ||||
1592 | |||||
1593 | First comes a comparison between various modules using | ||||
1594 | a very short single-line JSON string (also available at | ||||
1595 | L<http://dist.schmorp.de/misc/json/short.json>). | ||||
1596 | |||||
1597 | {"method": "handleMessage", "params": ["user1", | ||||
1598 | "we were just talking"], "id": null, "array":[1,11,234,-5,1e5,1e7, | ||||
1599 | 1, 0]} | ||||
1600 | |||||
1601 | It shows the number of encodes/decodes per second (JSON::XS uses | ||||
1602 | the functional interface, while Cpanel::JSON::XS/2 uses the OO interface | ||||
1603 | with pretty-printing and hashkey sorting enabled, Cpanel::JSON::XS/3 enables | ||||
1604 | shrink. JSON::DWIW/DS uses the deserialise function, while JSON::DWIW::FJ | ||||
1605 | uses the from_json method). Higher is better: | ||||
1606 | |||||
1607 | module | encode | decode | | ||||
1608 | --------------|------------|------------| | ||||
1609 | JSON::DWIW/DS | 86302.551 | 102300.098 | | ||||
1610 | JSON::DWIW/FJ | 86302.551 | 75983.768 | | ||||
1611 | JSON::PP | 15827.562 | 6638.658 | | ||||
1612 | JSON::Syck | 63358.066 | 47662.545 | | ||||
1613 | JSON::XS | 511500.488 | 511500.488 | | ||||
1614 | JSON::XS/2 | 291271.111 | 388361.481 | | ||||
1615 | JSON::XS/3 | 361577.931 | 361577.931 | | ||||
1616 | Storable | 66788.280 | 265462.278 | | ||||
1617 | --------------+------------+------------+ | ||||
1618 | |||||
1619 | That is, JSON::XS is almost six times faster than JSON::DWIW on encoding, | ||||
1620 | about five times faster on decoding, and over thirty to seventy times | ||||
1621 | faster than JSON's pure perl implementation. It also compares favourably | ||||
1622 | to Storable for small amounts of data. | ||||
1623 | |||||
1624 | Using a longer test string (roughly 18KB, generated from Yahoo! Locals | ||||
1625 | search API (L<http://dist.schmorp.de/misc/json/long.json>). | ||||
1626 | |||||
1627 | module | encode | decode | | ||||
1628 | --------------|------------|------------| | ||||
1629 | JSON::DWIW/DS | 1647.927 | 2673.916 | | ||||
1630 | JSON::DWIW/FJ | 1630.249 | 2596.128 | | ||||
1631 | JSON::PP | 400.640 | 62.311 | | ||||
1632 | JSON::Syck | 1481.040 | 1524.869 | | ||||
1633 | JSON::XS | 20661.596 | 9541.183 | | ||||
1634 | JSON::XS/2 | 10683.403 | 9416.938 | | ||||
1635 | JSON::XS/3 | 20661.596 | 9400.054 | | ||||
1636 | Storable | 19765.806 | 10000.725 | | ||||
1637 | --------------+------------+------------+ | ||||
1638 | |||||
1639 | Again, JSON::XS leads by far (except for Storable which non-surprisingly | ||||
1640 | decodes a bit faster). | ||||
1641 | |||||
1642 | On large strings containing lots of high Unicode characters, some modules | ||||
1643 | (such as JSON::PC) seem to decode faster than JSON::XS, but the result | ||||
1644 | will be broken due to missing (or wrong) Unicode handling. Others refuse | ||||
1645 | to decode or encode properly, so it was impossible to prepare a fair | ||||
1646 | comparison table for that case. | ||||
1647 | |||||
1648 | For updated graphs see L<https://github.com/Sereal/Sereal/wiki/Sereal-Comparison-Graphs> | ||||
1649 | |||||
1650 | |||||
1651 | =head1 INTEROP with JSON and JSON::XS | ||||
1652 | |||||
1653 | JSON-XS-3.01 broke interoperability with JSON-2.90 with booleans. See L<JSON>. | ||||
1654 | |||||
1655 | Cpanel::JSON::XS needs to know the JSON and JSON::XS versions to be able work | ||||
1656 | with those objects, especially when encoding a booleans like C<{"is_true":true}>. | ||||
1657 | So you need to load these modules before. | ||||
1658 | |||||
1659 | true/false overloading is supported. | ||||
1660 | |||||
1661 | JSON::XS and JSON::PP representations are accepted and older JSON::XS accepts | ||||
1662 | Cpanel::JSON::XS booleans. | ||||
1663 | |||||
1664 | I cannot think of any reason to still use JSON::XS anymore. | ||||
1665 | |||||
1666 | =head1 SECURITY CONSIDERATIONS | ||||
1667 | |||||
1668 | When you are using JSON in a protocol, talking to untrusted potentially | ||||
1669 | hostile creatures requires relatively few measures. | ||||
1670 | |||||
1671 | First of all, your JSON decoder should be secure, that is, should not have | ||||
1672 | any buffer overflows. Obviously, this module should ensure that. | ||||
1673 | |||||
1674 | Second, you need to avoid resource-starving attacks. That means you should | ||||
1675 | limit the size of JSON texts you accept, or make sure then when your | ||||
1676 | resources run out, that's just fine (e.g. by using a separate process that | ||||
1677 | can crash safely). The size of a JSON text in octets or characters is | ||||
1678 | usually a good indication of the size of the resources required to decode | ||||
1679 | it into a Perl structure. While JSON::XS can check the size of the JSON | ||||
1680 | text, it might be too late when you already have it in memory, so you | ||||
1681 | might want to check the size before you accept the string. | ||||
1682 | |||||
1683 | Third, Cpanel::JSON::XS recurses using the C stack when decoding objects and | ||||
1684 | arrays. The C stack is a limited resource: for instance, on my amd64 | ||||
1685 | machine with 8MB of stack size I can decode around 180k nested arrays but | ||||
1686 | only 14k nested JSON objects (due to perl itself recursing deeply on croak | ||||
1687 | to free the temporary). If that is exceeded, the program crashes. To be | ||||
1688 | conservative, the default nesting limit is set to 512. If your process | ||||
1689 | has a smaller stack, you should adjust this setting accordingly with the | ||||
1690 | C<max_depth> method. | ||||
1691 | |||||
1692 | Also keep in mind that Cpanel::JSON::XS might leak contents of your Perl data | ||||
1693 | structures in its error messages, so when you serialise sensitive | ||||
1694 | information you might want to make sure that exceptions thrown by JSON::XS | ||||
1695 | will not end up in front of untrusted eyes. | ||||
1696 | |||||
1697 | If you are using Cpanel::JSON::XS to return packets to consumption | ||||
1698 | by JavaScript scripts in a browser you should have a look at | ||||
1699 | L<http://blog.archive.jpsykes.com/47/practical-csrf-and-json-security/> to | ||||
1700 | see whether you are vulnerable to some common attack vectors (which really | ||||
1701 | are browser design bugs, but it is still you who will have to deal with | ||||
1702 | it, as major browser developers care only for features, not about getting | ||||
1703 | security right). You might also want to also look at L<Mojo::JSON> | ||||
1704 | special escape rules to prevent from XSS attacks. | ||||
1705 | |||||
1706 | =head1 THREADS | ||||
1707 | |||||
1708 | Cpanel::JSON::XS has proper ithreads support, unlike JSON::XS. If you | ||||
1709 | encounter any bugs with thread support please report them. | ||||
1710 | |||||
1711 | =head1 BUGS | ||||
1712 | |||||
1713 | While the goal of the Cpanel::JSON::XS module is to be correct, that | ||||
1714 | unfortunately does not mean it's bug-free, only that the author thinks | ||||
1715 | its design is bug-free. If you keep reporting bugs they will be fixed | ||||
1716 | swiftly, though. | ||||
1717 | |||||
1718 | Since the JSON::XS author refuses to use a public bugtracker and | ||||
1719 | prefers private emails, we've setup a tracker at RT, so you might want | ||||
1720 | to report any issues twice. Once in private to MLEHMANN to be fixed in | ||||
1721 | JSON::XS for the masses and one to our the public tracker. Issues | ||||
1722 | fixed by JSON::XS with a new release will also be backported to | ||||
1723 | Cpanel::JSON::XS and 5.6.2, as long as Cpanel relies on 5.6.2 and | ||||
1724 | Cpanel::JSON::XS as our serializer of choice. | ||||
1725 | |||||
1726 | L<https://rt.cpan.org/Public/Dist/Display.html?Queue=Cpanel-JSON-XS> | ||||
1727 | |||||
1728 | =head1 LICENSE | ||||
1729 | |||||
1730 | This module is available under the same licences as perl, the Artistic | ||||
1731 | license and the GPL. | ||||
1732 | |||||
1733 | =cut | ||||
1734 | |||||
1735 | our ($true, $false); | ||||
1736 | 1 | 400ns | if ($INC{'JSON/XS.pm'} and $JSON::XS::VERSION ge "3.00") { | ||
1737 | $true = $Types::Serialiser::true; # readonly if loaded by JSON::XS | ||||
1738 | $false = $Types::Serialiser::false; | ||||
1739 | } else { | ||||
1740 | 1 | 700ns | $true = do { bless \(my $dummy = 1), "JSON::XS::Boolean" }; | ||
1741 | 1 | 400ns | $false = do { bless \(my $dummy = 0), "JSON::XS::Boolean" }; | ||
1742 | } | ||||
1743 | |||||
1744 | sub true() { $true } | ||||
1745 | sub false() { $false } | ||||
1746 | |||||
1747 | sub is_bool($) { | ||||
1748 | UNIVERSAL::isa($_[0], "JSON::XS::Boolean") | ||||
1749 | or UNIVERSAL::isa($_[0], "JSON::PP::Boolean"); | ||||
1750 | } | ||||
1751 | |||||
1752 | 1 | 162µs | 1 | 296µs | XSLoader::load 'Cpanel::JSON::XS', $VERSION; # spent 296µs making 1 call to XSLoader::load |
1753 | |||||
1754 | package | ||||
1755 | JSON::XS::Boolean; | ||||
1756 | |||||
1757 | use overload | ||||
1758 | # spent 39µs (11+28) within JSON::XS::Boolean::BEGIN@1758 which was called:
# once (11µs+28µs) by JSON::MaybeXS::_choose_json_module at line 1771 | ||||
1759 | "++" => sub { $_[0] = ${$_[0]} + 1 }, | ||||
1760 | "--" => sub { $_[0] = ${$_[0]} - 1 }, | ||||
1761 | # '""' => sub { ${$_[0]} == 1 ? 'true' : 'false' }, | ||||
1762 | #'eq' => sub { | ||||
1763 | # my ($obj, $op) = ref ($_[0]) ? ($_[0], $_[1]) : ($_[1], $_[0]); | ||||
1764 | # if ($op eq 'true' or $op eq 'false') { | ||||
1765 | # return "$obj" eq 'true' ? 'true' eq $op : 'false' eq $op; | ||||
1766 | # } | ||||
1767 | # else { | ||||
1768 | # return $obj ? 1 == $op : 0 == $op; | ||||
1769 | # } | ||||
1770 | # }, | ||||
1771 | 2 | 28µs | 2 | 67µs | fallback => 1; # spent 39µs making 1 call to JSON::XS::Boolean::BEGIN@1758
# spent 28µs making 1 call to overload::import |
1772 | |||||
1773 | 1 | 16µs | 1; | ||
1774 | |||||
1775 | =head1 SEE ALSO | ||||
1776 | |||||
1777 | The F<cpanel_json_xs> command line utility for quick experiments. | ||||
1778 | |||||
1779 | L<JSON>, L<JSON::XS>, L<JSON::MaybeXS>, L<Mojo::JSON>, L<Mojo::JSON::MaybeXS>, | ||||
1780 | L<JSON::SL>, L<JSON::DWIW>, L<JSON::YAJL>, L<https://metacpan.org/search?q=JSON> | ||||
1781 | |||||
1782 | =head1 AUTHOR | ||||
1783 | |||||
1784 | Marc Lehmann <schmorp@schmorp.de>, http://home.schmorp.de/ | ||||
1785 | |||||
1786 | cPanel Inc. <cpan@cpanel.net> | ||||
1787 | |||||
1788 | =head1 MAINTAINER | ||||
1789 | |||||
1790 | cPanel Inc. <cpan@cpanel.net> | ||||
1791 | |||||
1792 | =cut | ||||
1793 | |||||
# spent 1.47ms within Cpanel::JSON::XS::DESTROY which was called 1001 times, avg 1µs/call:
# 1000 times (1.46ms+0s) by BenchmarkAnything::Storage::Backend::SQL::add_single_benchmark at line 363 of BenchmarkAnything/Storage/Backend/SQL.pm, avg 1µs/call
# once (2µs+0s) by main::_gc at line 434 of /home/ss5/perl5/perlbrew/perls/perl-5.22.0/bin/benchmarkanything-storage | |||||
# spent 3µs within Cpanel::JSON::XS::END which was called:
# once (3µs+0s) by main::RUNTIME at line 0 of /home/ss5/perl5/perlbrew/perls/perl-5.22.0/bin/benchmarkanything-storage | |||||
# spent 300ns within Cpanel::JSON::XS::canonical which was called:
# once (300ns+0s) by JSON::MaybeXS::new at line 47 of JSON/MaybeXS.pm | |||||
# spent 10.9ms within Cpanel::JSON::XS::decode which was called 1001 times, avg 11µs/call:
# 1001 times (10.9ms+0s) by Search::Elasticsearch::Role::Serializer::JSON::try {...} at line 81 of Search/Elasticsearch/Role/Serializer/JSON.pm, avg 11µs/call | |||||
# spent 7.02ms within Cpanel::JSON::XS::encode which was called 1000 times, avg 7µs/call:
# 1000 times (7.02ms+0s) by Search::Elasticsearch::Role::Serializer::JSON::try {...} at line 24 of Search/Elasticsearch/Role/Serializer/JSON.pm, avg 7µs/call | |||||
# spent 2.07ms within Cpanel::JSON::XS::new which was called 1002 times, avg 2µs/call:
# 1002 times (2.07ms+0s) by JSON::MaybeXS::new at line 46 of JSON/MaybeXS.pm, avg 2µs/call | |||||
# spent 1µs within Cpanel::JSON::XS::utf8 which was called 2 times, avg 500ns/call:
# once (600ns+0s) by Search::Elasticsearch::Serializer::JSON::__ANON__[/home/ss5/perl5/perlbrew/perls/perl-5.22.0/lib/site_perl/5.22.0/Search/Elasticsearch/Serializer/JSON.pm:6] at line 6 of Search/Elasticsearch/Serializer/JSON.pm
# once (400ns+0s) by JSON::MaybeXS::new at line 47 of JSON/MaybeXS.pm |