… | |
… | |
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 |
… | |
… | |
427 | JSON::XS->new->allow_nonref->encode ("Hello, World!") |
443 | JSON::XS->new->allow_nonref->encode ("Hello, World!") |
428 | => "Hello, World!" |
444 | => "Hello, World!" |
429 | |
445 | |
430 | =item $json = $json->allow_blessed ([$enable]) |
446 | =item $json = $json->allow_blessed ([$enable]) |
431 | |
447 | |
432 | =item $enabled = $json->get_allow_bless |
448 | =item $enabled = $json->get_allow_blessed |
433 | |
449 | |
434 | 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 |
435 | 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 |
436 | 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> |
437 | 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 |
438 | 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 |
439 | encoded. Has no effect on C<decode>. |
455 | encoded. Has no effect on C<decode>. |
440 | |
456 | |
441 | 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 |
442 | exception when it encounters a blessed object. |
458 | exception when it encounters a blessed object. |
443 | |
459 | |