… | |
… | |
17 | # OO-interface |
17 | # OO-interface |
18 | |
18 | |
19 | $coder = JSON::XS->new->ascii->pretty->allow_nonref; |
19 | $coder = JSON::XS->new->ascii->pretty->allow_nonref; |
20 | $pretty_printed_unencoded = $coder->encode ($perl_scalar); |
20 | $pretty_printed_unencoded = $coder->encode ($perl_scalar); |
21 | $perl_scalar = $coder->decode ($unicode_json_text); |
21 | $perl_scalar = $coder->decode ($unicode_json_text); |
|
|
22 | |
|
|
23 | # Note that JSON version 2.0 and above will automatically use JSON::XS |
|
|
24 | # if available, at virtually no speed overhead either, so you should |
|
|
25 | # be able to just: |
|
|
26 | |
|
|
27 | use JSON; |
|
|
28 | |
|
|
29 | # and do the same things, except that you have a pure-perl fallback now. |
22 | |
30 | |
23 | DESCRIPTION |
31 | DESCRIPTION |
24 | This module converts Perl data structures to JSON and vice versa. Its |
32 | This module converts Perl data structures to JSON and vice versa. Its |
25 | primary goal is to be *correct* and its secondary goal is to be *fast*. |
33 | primary goal is to be *correct* and its secondary goal is to be *fast*. |
26 | To reach the latter goal it was written in C. |
34 | To reach the latter goal it was written in C. |
|
|
35 | |
|
|
36 | Beginning with version 2.0 of the JSON module, when both JSON and |
|
|
37 | JSON::XS are installed, then JSON will fall back on JSON::XS (this can |
|
|
38 | be overriden) with no overhead due to emulation (by inheritign |
|
|
39 | constructor and methods). If JSON::XS is not available, it will fall |
|
|
40 | back to the compatible JSON::PP module as backend, so using JSON instead |
|
|
41 | of JSON::XS gives you a portable JSON API that can be fast when you need |
|
|
42 | and doesn't require a C compiler when that is a problem. |
27 | |
43 | |
28 | As this is the n-th-something JSON module on CPAN, what was the reason |
44 | As this is the n-th-something JSON module on CPAN, what was the reason |
29 | to write yet another JSON module? While it seems there are many JSON |
45 | to write yet another JSON module? While it seems there are many JSON |
30 | modules, none of them correctly handle all corner cases, and in most |
46 | modules, none of them correctly handle all corner cases, and in most |
31 | cases their maintainers are unresponsive, gone missing, or not listening |
47 | cases their maintainers are unresponsive, gone missing, or not listening |
… | |
… | |
155 | |
171 | |
156 | my $json = JSON::XS->new->utf8->space_after->encode ({a => [1,2]}) |
172 | my $json = JSON::XS->new->utf8->space_after->encode ({a => [1,2]}) |
157 | => {"a": [1, 2]} |
173 | => {"a": [1, 2]} |
158 | |
174 | |
159 | $json = $json->ascii ([$enable]) |
175 | $json = $json->ascii ([$enable]) |
|
|
176 | $enabled = $json->get_ascii |
160 | If $enable is true (or missing), then the "encode" method will not |
177 | If $enable is true (or missing), then the "encode" method will not |
161 | generate characters outside the code range 0..127 (which is ASCII). |
178 | generate characters outside the code range 0..127 (which is ASCII). |
162 | Any Unicode characters outside that range will be escaped using |
179 | Any Unicode characters outside that range will be escaped using |
163 | either a single \uXXXX (BMP characters) or a double \uHHHH\uLLLLL |
180 | either a single \uXXXX (BMP characters) or a double \uHHHH\uLLLLL |
164 | escape sequence, as per RFC4627. The resulting encoded JSON text can |
181 | escape sequence, as per RFC4627. The resulting encoded JSON text can |
… | |
… | |
176 | |
193 | |
177 | JSON::XS->new->ascii (1)->encode ([chr 0x10401]) |
194 | JSON::XS->new->ascii (1)->encode ([chr 0x10401]) |
178 | => ["\ud801\udc01"] |
195 | => ["\ud801\udc01"] |
179 | |
196 | |
180 | $json = $json->latin1 ([$enable]) |
197 | $json = $json->latin1 ([$enable]) |
|
|
198 | $enabled = $json->get_latin1 |
181 | If $enable is true (or missing), then the "encode" method will |
199 | If $enable is true (or missing), then the "encode" method will |
182 | encode the resulting JSON text as latin1 (or iso-8859-1), escaping |
200 | encode the resulting JSON text as latin1 (or iso-8859-1), escaping |
183 | any characters outside the code range 0..255. The resulting string |
201 | any characters outside the code range 0..255. The resulting string |
184 | can be treated as a latin1-encoded JSON text or a native Unicode |
202 | can be treated as a latin1-encoded JSON text or a native Unicode |
185 | string. The "decode" method will not be affected in any way by this |
203 | string. The "decode" method will not be affected in any way by this |
… | |
… | |
201 | |
219 | |
202 | JSON::XS->new->latin1->encode (["\x{89}\x{abc}"] |
220 | JSON::XS->new->latin1->encode (["\x{89}\x{abc}"] |
203 | => ["\x{89}\\u0abc"] # (perl syntax, U+abc escaped, U+89 not) |
221 | => ["\x{89}\\u0abc"] # (perl syntax, U+abc escaped, U+89 not) |
204 | |
222 | |
205 | $json = $json->utf8 ([$enable]) |
223 | $json = $json->utf8 ([$enable]) |
|
|
224 | $enabled = $json->get_utf8 |
206 | If $enable is true (or missing), then the "encode" method will |
225 | If $enable is true (or missing), then the "encode" method will |
207 | encode the JSON result into UTF-8, as required by many protocols, |
226 | encode the JSON result into UTF-8, as required by many protocols, |
208 | while the "decode" method expects to be handled an UTF-8-encoded |
227 | while the "decode" method expects to be handled an UTF-8-encoded |
209 | string. Please note that UTF-8-encoded strings do not contain any |
228 | string. Please note that UTF-8-encoded strings do not contain any |
210 | characters outside the range 0..255, they are thus useful for |
229 | characters outside the range 0..255, they are thus useful for |
… | |
… | |
242 | 2 |
261 | 2 |
243 | ] |
262 | ] |
244 | } |
263 | } |
245 | |
264 | |
246 | $json = $json->indent ([$enable]) |
265 | $json = $json->indent ([$enable]) |
|
|
266 | $enabled = $json->get_indent |
247 | If $enable is true (or missing), then the "encode" method will use a |
267 | If $enable is true (or missing), then the "encode" method will use a |
248 | multiline format as output, putting every array member or |
268 | multiline format as output, putting every array member or |
249 | object/hash key-value pair into its own line, indenting them |
269 | object/hash key-value pair into its own line, indenting them |
250 | properly. |
270 | properly. |
251 | |
271 | |
… | |
… | |
253 | the resulting JSON text is guaranteed not to contain any "newlines". |
273 | the resulting JSON text is guaranteed not to contain any "newlines". |
254 | |
274 | |
255 | This setting has no effect when decoding JSON texts. |
275 | This setting has no effect when decoding JSON texts. |
256 | |
276 | |
257 | $json = $json->space_before ([$enable]) |
277 | $json = $json->space_before ([$enable]) |
|
|
278 | $enabled = $json->get_space_before |
258 | If $enable is true (or missing), then the "encode" method will add |
279 | If $enable is true (or missing), then the "encode" method will add |
259 | an extra optional space before the ":" separating keys from values |
280 | an extra optional space before the ":" separating keys from values |
260 | in JSON objects. |
281 | in JSON objects. |
261 | |
282 | |
262 | If $enable is false, then the "encode" method will not add any extra |
283 | If $enable is false, then the "encode" method will not add any extra |
… | |
… | |
268 | Example, space_before enabled, space_after and indent disabled: |
289 | Example, space_before enabled, space_after and indent disabled: |
269 | |
290 | |
270 | {"key" :"value"} |
291 | {"key" :"value"} |
271 | |
292 | |
272 | $json = $json->space_after ([$enable]) |
293 | $json = $json->space_after ([$enable]) |
|
|
294 | $enabled = $json->get_space_after |
273 | If $enable is true (or missing), then the "encode" method will add |
295 | If $enable is true (or missing), then the "encode" method will add |
274 | an extra optional space after the ":" separating keys from values in |
296 | an extra optional space after the ":" separating keys from values in |
275 | JSON objects and extra whitespace after the "," separating key-value |
297 | JSON objects and extra whitespace after the "," separating key-value |
276 | pairs and array members. |
298 | pairs and array members. |
277 | |
299 | |
… | |
… | |
283 | Example, space_before and indent disabled, space_after enabled: |
305 | Example, space_before and indent disabled, space_after enabled: |
284 | |
306 | |
285 | {"key": "value"} |
307 | {"key": "value"} |
286 | |
308 | |
287 | $json = $json->relaxed ([$enable]) |
309 | $json = $json->relaxed ([$enable]) |
|
|
310 | $enabled = $json->get_relaxed |
288 | If $enable is true (or missing), then "decode" will accept some |
311 | If $enable is true (or missing), then "decode" will accept some |
289 | extensions to normal JSON syntax (see below). "encode" will not be |
312 | extensions to normal JSON syntax (see below). "encode" will not be |
290 | affected in anyway. *Be aware that this option makes you accept |
313 | affected in anyway. *Be aware that this option makes you accept |
291 | invalid JSON texts as if they were valid!*. I suggest only to use |
314 | invalid JSON texts as if they were valid!*. I suggest only to use |
292 | this option to parse application-specific files written by humans |
315 | this option to parse application-specific files written by humans |
… | |
… | |
322 | 1, # this comment not allowed in JSON |
345 | 1, # this comment not allowed in JSON |
323 | # neither this one... |
346 | # neither this one... |
324 | ] |
347 | ] |
325 | |
348 | |
326 | $json = $json->canonical ([$enable]) |
349 | $json = $json->canonical ([$enable]) |
|
|
350 | $enabled = $json->get_canonical |
327 | If $enable is true (or missing), then the "encode" method will |
351 | If $enable is true (or missing), then the "encode" method will |
328 | output JSON objects by sorting their keys. This is adding a |
352 | output JSON objects by sorting their keys. This is adding a |
329 | comparatively high overhead. |
353 | comparatively high overhead. |
330 | |
354 | |
331 | If $enable is false, then the "encode" method will output key-value |
355 | If $enable is false, then the "encode" method will output key-value |
… | |
… | |
339 | in Perl. |
363 | in Perl. |
340 | |
364 | |
341 | This setting has no effect when decoding JSON texts. |
365 | This setting has no effect when decoding JSON texts. |
342 | |
366 | |
343 | $json = $json->allow_nonref ([$enable]) |
367 | $json = $json->allow_nonref ([$enable]) |
|
|
368 | $enabled = $json->get_allow_nonref |
344 | If $enable is true (or missing), then the "encode" method can |
369 | If $enable is true (or missing), then the "encode" method can |
345 | convert a non-reference into its corresponding string, number or |
370 | convert a non-reference into its corresponding string, number or |
346 | null JSON value, which is an extension to RFC4627. Likewise, |
371 | null JSON value, which is an extension to RFC4627. Likewise, |
347 | "decode" will accept those JSON values instead of croaking. |
372 | "decode" will accept those JSON values instead of croaking. |
348 | |
373 | |
… | |
… | |
356 | |
381 | |
357 | JSON::XS->new->allow_nonref->encode ("Hello, World!") |
382 | JSON::XS->new->allow_nonref->encode ("Hello, World!") |
358 | => "Hello, World!" |
383 | => "Hello, World!" |
359 | |
384 | |
360 | $json = $json->allow_blessed ([$enable]) |
385 | $json = $json->allow_blessed ([$enable]) |
|
|
386 | $enabled = $json->get_allow_blessed |
361 | If $enable is true (or missing), then the "encode" method will not |
387 | If $enable is true (or missing), then the "encode" method will not |
362 | barf when it encounters a blessed reference. Instead, the value of |
388 | barf when it encounters a blessed reference. Instead, the value of |
363 | the convert_blessed option will decide whether "null" |
389 | the convert_blessed option will decide whether "null" |
364 | ("convert_blessed" disabled or no "to_json" method found) or a |
390 | ("convert_blessed" disabled or no "TO_JSON" method found) or a |
365 | representation of the object ("convert_blessed" enabled and |
391 | representation of the object ("convert_blessed" enabled and |
366 | "to_json" method found) is being encoded. Has no effect on "decode". |
392 | "TO_JSON" method found) is being encoded. Has no effect on "decode". |
367 | |
393 | |
368 | If $enable is false (the default), then "encode" will throw an |
394 | If $enable is false (the default), then "encode" will throw an |
369 | exception when it encounters a blessed object. |
395 | exception when it encounters a blessed object. |
370 | |
396 | |
371 | $json = $json->convert_blessed ([$enable]) |
397 | $json = $json->convert_blessed ([$enable]) |
|
|
398 | $enabled = $json->get_convert_blessed |
372 | If $enable is true (or missing), then "encode", upon encountering a |
399 | If $enable is true (or missing), then "encode", upon encountering a |
373 | blessed object, will check for the availability of the "TO_JSON" |
400 | blessed object, will check for the availability of the "TO_JSON" |
374 | method on the object's class. If found, it will be called in scalar |
401 | method on the object's class. If found, it will be called in scalar |
375 | context and the resulting scalar will be encoded instead of the |
402 | context and the resulting scalar will be encoded instead of the |
376 | object. If no "TO_JSON" method is found, the value of |
403 | object. If no "TO_JSON" method is found, the value of |
… | |
… | |
466 | |
493 | |
467 | { __widget__ => $self->{id} } |
494 | { __widget__ => $self->{id} } |
468 | } |
495 | } |
469 | |
496 | |
470 | $json = $json->shrink ([$enable]) |
497 | $json = $json->shrink ([$enable]) |
|
|
498 | $enabled = $json->get_shrink |
471 | Perl usually over-allocates memory a bit when allocating space for |
499 | Perl usually over-allocates memory a bit when allocating space for |
472 | strings. This flag optionally resizes strings generated by either |
500 | strings. This flag optionally resizes strings generated by either |
473 | "encode" or "decode" to their minimum size possible. This can save |
501 | "encode" or "decode" to their minimum size possible. This can save |
474 | memory when your JSON texts are either very very long or you have |
502 | memory when your JSON texts are either very very long or you have |
475 | many short strings. It will also try to downgrade any strings to |
503 | many short strings. It will also try to downgrade any strings to |
… | |
… | |
493 | converting strings that look like integers or floats into integers |
521 | converting strings that look like integers or floats into integers |
494 | or floats internally (there is no difference on the Perl level), |
522 | or floats internally (there is no difference on the Perl level), |
495 | saving space. |
523 | saving space. |
496 | |
524 | |
497 | $json = $json->max_depth ([$maximum_nesting_depth]) |
525 | $json = $json->max_depth ([$maximum_nesting_depth]) |
|
|
526 | $max_depth = $json->get_max_depth |
498 | Sets the maximum nesting level (default 512) accepted while encoding |
527 | Sets the maximum nesting level (default 512) accepted while encoding |
499 | or decoding. If the JSON text or Perl data structure has an equal or |
528 | or decoding. If the JSON text or Perl data structure has an equal or |
500 | higher nesting level then this limit, then the encoder and decoder |
529 | higher nesting level then this limit, then the encoder and decoder |
501 | will stop and croak at that point. |
530 | will stop and croak at that point. |
502 | |
531 | |
… | |
… | |
514 | |
543 | |
515 | See SECURITY CONSIDERATIONS, below, for more info on why this is |
544 | See SECURITY CONSIDERATIONS, below, for more info on why this is |
516 | useful. |
545 | useful. |
517 | |
546 | |
518 | $json = $json->max_size ([$maximum_string_size]) |
547 | $json = $json->max_size ([$maximum_string_size]) |
|
|
548 | $max_size = $json->get_max_size |
519 | Set the maximum length a JSON text may have (in bytes) where |
549 | Set the maximum length a JSON text may have (in bytes) where |
520 | decoding is being attempted. The default is 0, meaning no limit. |
550 | decoding is being attempted. The default is 0, meaning no limit. |
521 | When "decode" is called on a string longer then this number of |
551 | When "decode" is called on a string longer then this number of |
522 | characters it will not attempt to decode the string but throw an |
552 | characters it will not attempt to decode the string but throw an |
523 | exception. This setting has no effect on "encode" (yet). |
553 | exception. This setting has no effect on "encode" (yet). |
… | |
… | |
804 | It shows the number of encodes/decodes per second (JSON::XS uses the |
834 | It shows the number of encodes/decodes per second (JSON::XS uses the |
805 | functional interface, while JSON::XS/2 uses the OO interface with |
835 | functional interface, while JSON::XS/2 uses the OO interface with |
806 | pretty-printing and hashkey sorting enabled, JSON::XS/3 enables shrink). |
836 | pretty-printing and hashkey sorting enabled, JSON::XS/3 enables shrink). |
807 | Higher is better: |
837 | Higher is better: |
808 | |
838 | |
809 | Storable | 15779.925 | 14169.946 | |
|
|
810 | -----------+------------+------------+ |
|
|
811 | module | encode | decode | |
839 | module | encode | decode | |
812 | -----------|------------|------------| |
840 | -----------|------------|------------| |
813 | JSON | 4990.842 | 4088.813 | |
841 | JSON 1.x | 4990.842 | 4088.813 | |
814 | JSON::DWIW | 51653.990 | 71575.154 | |
842 | JSON::DWIW | 51653.990 | 71575.154 | |
815 | JSON::PC | 65948.176 | 74631.744 | |
843 | JSON::PC | 65948.176 | 74631.744 | |
816 | JSON::PP | 8931.652 | 3817.168 | |
844 | JSON::PP | 8931.652 | 3817.168 | |
817 | JSON::Syck | 24877.248 | 27776.848 | |
845 | JSON::Syck | 24877.248 | 27776.848 | |
818 | JSON::XS | 388361.481 | 227951.304 | |
846 | JSON::XS | 388361.481 | 227951.304 | |
… | |
… | |
829 | Using a longer test string (roughly 18KB, generated from Yahoo! Locals |
857 | Using a longer test string (roughly 18KB, generated from Yahoo! Locals |
830 | search API (http://nanoref.com/yahooapis/mgPdGg): |
858 | search API (http://nanoref.com/yahooapis/mgPdGg): |
831 | |
859 | |
832 | module | encode | decode | |
860 | module | encode | decode | |
833 | -----------|------------|------------| |
861 | -----------|------------|------------| |
834 | JSON | 55.260 | 34.971 | |
862 | JSON 1.x | 55.260 | 34.971 | |
835 | JSON::DWIW | 825.228 | 1082.513 | |
863 | JSON::DWIW | 825.228 | 1082.513 | |
836 | JSON::PC | 3571.444 | 2394.829 | |
864 | JSON::PC | 3571.444 | 2394.829 | |
837 | JSON::PP | 210.987 | 32.574 | |
865 | JSON::PP | 210.987 | 32.574 | |
838 | JSON::Syck | 552.551 | 787.544 | |
866 | JSON::Syck | 552.551 | 787.544 | |
839 | JSON::XS | 5780.463 | 4854.519 | |
867 | JSON::XS | 5780.463 | 4854.519 | |