… | |
… | |
19 | |
19 | |
20 | $coder = JSON::XS->new->ascii->pretty->allow_nonref; |
20 | $coder = JSON::XS->new->ascii->pretty->allow_nonref; |
21 | $pretty_printed_unencoded = $coder->encode ($perl_scalar); |
21 | $pretty_printed_unencoded = $coder->encode ($perl_scalar); |
22 | $perl_scalar = $coder->decode ($unicode_json_text); |
22 | $perl_scalar = $coder->decode ($unicode_json_text); |
23 | |
23 | |
|
|
24 | # Note that JSON version 2.0 and above will automatically use JSON::XS |
|
|
25 | # if available, at virtually no speed overhead either, so you should |
|
|
26 | # be able to just: |
|
|
27 | |
|
|
28 | use JSON; |
|
|
29 | |
|
|
30 | # and do the same things, except that you have a pure-perl fallback now. |
|
|
31 | |
24 | =head1 DESCRIPTION |
32 | =head1 DESCRIPTION |
25 | |
33 | |
26 | This module converts Perl data structures to JSON and vice versa. Its |
34 | This module converts Perl data structures to JSON and vice versa. Its |
27 | primary goal is to be I<correct> and its secondary goal is to be |
35 | primary goal is to be I<correct> and its secondary goal is to be |
28 | I<fast>. To reach the latter goal it was written in C. |
36 | I<fast>. To reach the latter goal it was written in C. |
|
|
37 | |
|
|
38 | Beginning with version 2.0 of the JSON module, when both JSON and |
|
|
39 | JSON::XS are installed, then JSON will fall back on JSON::XS (this can be |
|
|
40 | overriden) with no overhead due to emulation (by inheritign constructor |
|
|
41 | and methods). If JSON::XS is not available, it will fall back to the |
|
|
42 | compatible JSON::PP module as backend, so using JSON instead of JSON::XS |
|
|
43 | gives you a portable JSON API that can be fast when you need and doesn't |
|
|
44 | require a C compiler when that is a problem. |
29 | |
45 | |
30 | As this is the n-th-something JSON module on CPAN, what was the reason |
46 | As this is the n-th-something JSON module on CPAN, what was the reason |
31 | to write yet another JSON module? While it seems there are many JSON |
47 | to write yet another JSON module? While it seems there are many JSON |
32 | modules, none of them correctly handle all corner cases, and in most cases |
48 | modules, none of them correctly handle all corner cases, and in most cases |
33 | their maintainers are unresponsive, gone missing, or not listening to bug |
49 | their maintainers are unresponsive, gone missing, or not listening to bug |
… | |
… | |
84 | |
100 | |
85 | package JSON::XS; |
101 | package JSON::XS; |
86 | |
102 | |
87 | use strict; |
103 | use strict; |
88 | |
104 | |
89 | our $VERSION = '1.53'; |
105 | our $VERSION = '2.0'; |
90 | our @ISA = qw(Exporter); |
106 | our @ISA = qw(Exporter); |
91 | |
107 | |
92 | our @EXPORT = qw(to_json from_json); |
108 | our @EXPORT = qw(to_json from_json); |
93 | |
109 | |
94 | use Exporter; |
110 | use Exporter; |
… | |
… | |
276 | |
292 | |
277 | use Encode; |
293 | use Encode; |
278 | $object = JSON::XS->new->decode (decode "UTF-32LE", $jsontext); |
294 | $object = JSON::XS->new->decode (decode "UTF-32LE", $jsontext); |
279 | |
295 | |
280 | =item $json = $json->pretty ([$enable]) |
296 | =item $json = $json->pretty ([$enable]) |
281 | |
|
|
282 | =item $enabled = $json->get_pretty |
|
|
283 | |
297 | |
284 | This enables (or disables) all of the C<indent>, C<space_before> and |
298 | This enables (or disables) all of the C<indent>, C<space_before> and |
285 | C<space_after> (and in the future possibly more) flags in one call to |
299 | C<space_after> (and in the future possibly more) flags in one call to |
286 | generate the most readable (or most compact) form possible. |
300 | generate the most readable (or most compact) form possible. |
287 | |
301 | |
… | |
… | |
429 | JSON::XS->new->allow_nonref->encode ("Hello, World!") |
443 | JSON::XS->new->allow_nonref->encode ("Hello, World!") |
430 | => "Hello, World!" |
444 | => "Hello, World!" |
431 | |
445 | |
432 | =item $json = $json->allow_blessed ([$enable]) |
446 | =item $json = $json->allow_blessed ([$enable]) |
433 | |
447 | |
434 | =item $enabled = $json->get_allow_bless |
448 | =item $enabled = $json->get_allow_blessed |
435 | |
449 | |
436 | If C<$enable> is true (or missing), then the C<encode> method will not |
450 | If C<$enable> is true (or missing), then the C<encode> method will not |
437 | barf when it encounters a blessed reference. Instead, the value of the |
451 | barf when it encounters a blessed reference. Instead, the value of the |
438 | B<convert_blessed> option will decide whether C<null> (C<convert_blessed> |
452 | B<convert_blessed> option will decide whether C<null> (C<convert_blessed> |
439 | disabled or no C<to_json> method found) or a representation of the |
453 | disabled or no C<TO_JSON> method found) or a representation of the |
440 | object (C<convert_blessed> enabled and C<to_json> method found) is being |
454 | object (C<convert_blessed> enabled and C<TO_JSON> method found) is being |
441 | encoded. Has no effect on C<decode>. |
455 | encoded. Has no effect on C<decode>. |
442 | |
456 | |
443 | If C<$enable> is false (the default), then C<encode> will throw an |
457 | If C<$enable> is false (the default), then C<encode> will throw an |
444 | exception when it encounters a blessed object. |
458 | exception when it encounters a blessed object. |
445 | |
459 | |