1 | =encoding utf-8 |
|
|
2 | |
|
|
3 | =head1 NAME |
1 | =head1 NAME |
4 | |
2 | |
5 | JSON::XS - JSON serialising/deserialising, done correctly and fast |
3 | JSON::XS - JSON serialising/deserialising, done correctly and fast |
6 | |
4 | |
7 | JSON::XS - 正しくて高速な JSON シリアライザ/デシリアライザ |
5 | JSON::XS - 正しくて高速な JSON シリアライザ/デシリアライザ |
… | |
… | |
42 | |
40 | |
43 | =head2 FEATURES |
41 | =head2 FEATURES |
44 | |
42 | |
45 | =over 4 |
43 | =over 4 |
46 | |
44 | |
47 | =item * correct unicode handling |
45 | =item * correct Unicode handling |
48 | |
46 | |
49 | This module knows how to handle Unicode, and even documents how and when |
47 | This module knows how to handle Unicode, and even documents how and when |
50 | it does so. |
48 | it does so. |
51 | |
49 | |
52 | =item * round-trip integrity |
50 | =item * round-trip integrity |
… | |
… | |
72 | This module has both a simple functional interface as well as an OO |
70 | This module has both a simple functional interface as well as an OO |
73 | interface. |
71 | interface. |
74 | |
72 | |
75 | =item * reasonably versatile output formats |
73 | =item * reasonably versatile output formats |
76 | |
74 | |
77 | You can choose between the most compact guarenteed single-line format |
75 | You can choose between the most compact guaranteed single-line format |
78 | possible (nice for simple line-based protocols), a pure-ascii format |
76 | possible (nice for simple line-based protocols), a pure-ascii format |
79 | (for when your transport is not 8-bit clean, still supports the whole |
77 | (for when your transport is not 8-bit clean, still supports the whole |
80 | unicode range), or a pretty-printed format (for when you want to read that |
78 | Unicode range), or a pretty-printed format (for when you want to read that |
81 | stuff). Or you can combine those features in whatever way you like. |
79 | stuff). Or you can combine those features in whatever way you like. |
82 | |
80 | |
83 | =back |
81 | =back |
84 | |
82 | |
85 | =cut |
83 | =cut |
86 | |
84 | |
87 | package JSON::XS; |
85 | package JSON::XS; |
88 | |
86 | |
89 | use strict; |
87 | use strict; |
90 | |
88 | |
91 | our $VERSION = '1.5'; |
89 | our $VERSION = '1.53'; |
92 | our @ISA = qw(Exporter); |
90 | our @ISA = qw(Exporter); |
93 | |
91 | |
94 | our @EXPORT = qw(to_json from_json); |
92 | our @EXPORT = qw(to_json from_json); |
95 | |
93 | |
96 | use Exporter; |
94 | use Exporter; |
97 | use XSLoader; |
95 | use XSLoader; |
98 | |
96 | |
99 | =head1 FUNCTIONAL INTERFACE |
97 | =head1 FUNCTIONAL INTERFACE |
100 | |
98 | |
101 | The following convinience methods are provided by this module. They are |
99 | The following convenience methods are provided by this module. They are |
102 | exported by default: |
100 | exported by default: |
103 | |
101 | |
104 | =over 4 |
102 | =over 4 |
105 | |
103 | |
106 | =item $json_text = to_json $perl_scalar |
104 | =item $json_text = to_json $perl_scalar |
… | |
… | |
145 | |
143 | |
146 | =over 4 |
144 | =over 4 |
147 | |
145 | |
148 | =item 1. Perl strings can store characters with ordinal values > 255. |
146 | =item 1. Perl strings can store characters with ordinal values > 255. |
149 | |
147 | |
150 | This enables you to store unicode characters as single characters in a |
148 | This enables you to store Unicode characters as single characters in a |
151 | Perl string - very natural. |
149 | Perl string - very natural. |
152 | |
150 | |
153 | =item 2. Perl does I<not> associate an encoding with your strings. |
151 | =item 2. Perl does I<not> associate an encoding with your strings. |
154 | |
152 | |
155 | Unless you force it to, e.g. when matching it against a regex, or printing |
153 | Unless you force it to, e.g. when matching it against a regex, or printing |
… | |
… | |
162 | encoding of your string. |
160 | encoding of your string. |
163 | |
161 | |
164 | Just ignore that flag unless you debug a Perl bug, a module written in |
162 | Just ignore that flag unless you debug a Perl bug, a module written in |
165 | XS or want to dive into the internals of perl. Otherwise it will only |
163 | XS or want to dive into the internals of perl. Otherwise it will only |
166 | confuse you, as, despite the name, it says nothing about how your string |
164 | confuse you, as, despite the name, it says nothing about how your string |
167 | is encoded. You can have unicode strings with that flag set, with that |
165 | is encoded. You can have Unicode strings with that flag set, with that |
168 | flag clear, and you can have binary data with that flag set and that flag |
166 | flag clear, and you can have binary data with that flag set and that flag |
169 | clear. Other possibilities exist, too. |
167 | clear. Other possibilities exist, too. |
170 | |
168 | |
171 | If you didn't know about that flag, just the better, pretend it doesn't |
169 | If you didn't know about that flag, just the better, pretend it doesn't |
172 | exist. |
170 | exist. |
… | |
… | |
177 | If you have UTF-8 encoded data, it is no longer a Unicode string, but a |
175 | If you have UTF-8 encoded data, it is no longer a Unicode string, but a |
178 | Unicode string encoded in UTF-8, giving you a binary string. |
176 | Unicode string encoded in UTF-8, giving you a binary string. |
179 | |
177 | |
180 | =item 5. A string containing "high" (> 255) character values is I<not> a UTF-8 string. |
178 | =item 5. A string containing "high" (> 255) character values is I<not> a UTF-8 string. |
181 | |
179 | |
182 | Its a fact. Learn to live with it. |
180 | It's a fact. Learn to live with it. |
183 | |
181 | |
184 | =back |
182 | =back |
185 | |
183 | |
186 | I hope this helps :) |
184 | I hope this helps :) |
187 | |
185 | |
… | |
… | |
206 | |
204 | |
207 | =item $json = $json->ascii ([$enable]) |
205 | =item $json = $json->ascii ([$enable]) |
208 | |
206 | |
209 | If C<$enable> is true (or missing), then the C<encode> method will not |
207 | If C<$enable> is true (or missing), then the C<encode> method will not |
210 | generate characters outside the code range C<0..127> (which is ASCII). Any |
208 | generate characters outside the code range C<0..127> (which is ASCII). Any |
211 | unicode characters outside that range will be escaped using either a |
209 | Unicode characters outside that range will be escaped using either a |
212 | single \uXXXX (BMP characters) or a double \uHHHH\uLLLLL escape sequence, |
210 | single \uXXXX (BMP characters) or a double \uHHHH\uLLLLL escape sequence, |
213 | as per RFC4627. The resulting encoded JSON text can be treated as a native |
211 | as per RFC4627. The resulting encoded JSON text can be treated as a native |
214 | unicode string, an ascii-encoded, latin1-encoded or UTF-8 encoded string, |
212 | Unicode string, an ascii-encoded, latin1-encoded or UTF-8 encoded string, |
215 | or any other superset of ASCII. |
213 | or any other superset of ASCII. |
216 | |
214 | |
217 | If C<$enable> is false, then the C<encode> method will not escape Unicode |
215 | If C<$enable> is false, then the C<encode> method will not escape Unicode |
218 | characters unless required by the JSON syntax or other flags. This results |
216 | characters unless required by the JSON syntax or other flags. This results |
219 | in a faster and more compact format. |
217 | in a faster and more compact format. |
… | |
… | |
228 | =item $json = $json->latin1 ([$enable]) |
226 | =item $json = $json->latin1 ([$enable]) |
229 | |
227 | |
230 | If C<$enable> is true (or missing), then the C<encode> method will encode |
228 | If C<$enable> is true (or missing), then the C<encode> method will encode |
231 | the resulting JSON text as latin1 (or iso-8859-1), escaping any characters |
229 | the resulting JSON text as latin1 (or iso-8859-1), escaping any characters |
232 | outside the code range C<0..255>. The resulting string can be treated as a |
230 | outside the code range C<0..255>. The resulting string can be treated as a |
233 | latin1-encoded JSON text or a native unicode string. The C<decode> method |
231 | latin1-encoded JSON text or a native Unicode string. The C<decode> method |
234 | will not be affected in any way by this flag, as C<decode> by default |
232 | will not be affected in any way by this flag, as C<decode> by default |
235 | expects unicode, which is a strict superset of latin1. |
233 | expects Unicode, which is a strict superset of latin1. |
236 | |
234 | |
237 | If C<$enable> is false, then the C<encode> method will not escape Unicode |
235 | If C<$enable> is false, then the C<encode> method will not escape Unicode |
238 | characters unless required by the JSON syntax or other flags. |
236 | characters unless required by the JSON syntax or other flags. |
239 | |
237 | |
240 | The main use for this flag is efficiently encoding binary data as JSON |
238 | The main use for this flag is efficiently encoding binary data as JSON |
241 | text, as most octets will not be escaped, resulting in a smaller encoded |
239 | text, as most octets will not be escaped, resulting in a smaller encoded |
242 | size. The disadvantage is that the resulting JSON text is encoded |
240 | size. The disadvantage is that the resulting JSON text is encoded |
243 | in latin1 (and must correctly be treated as such when storing and |
241 | in latin1 (and must correctly be treated as such when storing and |
244 | transfering), a rare encoding for JSON. It is therefore most useful when |
242 | transferring), a rare encoding for JSON. It is therefore most useful when |
245 | you want to store data structures known to contain binary data efficiently |
243 | you want to store data structures known to contain binary data efficiently |
246 | in files or databases, not when talking to other JSON encoders/decoders. |
244 | in files or databases, not when talking to other JSON encoders/decoders. |
247 | |
245 | |
248 | JSON::XS->new->latin1->encode (["\x{89}\x{abc}"] |
246 | JSON::XS->new->latin1->encode (["\x{89}\x{abc}"] |
249 | => ["\x{89}\\u0abc"] # (perl syntax, U+abc escaped, U+89 not) |
247 | => ["\x{89}\\u0abc"] # (perl syntax, U+abc escaped, U+89 not) |
… | |
… | |
257 | range C<0..255>, they are thus useful for bytewise/binary I/O. In future |
255 | range C<0..255>, they are thus useful for bytewise/binary I/O. In future |
258 | versions, enabling this option might enable autodetection of the UTF-16 |
256 | versions, enabling this option might enable autodetection of the UTF-16 |
259 | and UTF-32 encoding families, as described in RFC4627. |
257 | and UTF-32 encoding families, as described in RFC4627. |
260 | |
258 | |
261 | If C<$enable> is false, then the C<encode> method will return the JSON |
259 | If C<$enable> is false, then the C<encode> method will return the JSON |
262 | string as a (non-encoded) unicode string, while C<decode> expects thus a |
260 | string as a (non-encoded) Unicode string, while C<decode> expects thus a |
263 | unicode string. Any decoding or encoding (e.g. to UTF-8 or UTF-16) needs |
261 | Unicode string. Any decoding or encoding (e.g. to UTF-8 or UTF-16) needs |
264 | to be done yourself, e.g. using the Encode module. |
262 | to be done yourself, e.g. using the Encode module. |
265 | |
263 | |
266 | Example, output UTF-16BE-encoded JSON: |
264 | Example, output UTF-16BE-encoded JSON: |
267 | |
265 | |
268 | use Encode; |
266 | use Encode; |
… | |
… | |
292 | |
290 | |
293 | =item $json = $json->indent ([$enable]) |
291 | =item $json = $json->indent ([$enable]) |
294 | |
292 | |
295 | If C<$enable> is true (or missing), then the C<encode> method will use a multiline |
293 | If C<$enable> is true (or missing), then the C<encode> method will use a multiline |
296 | format as output, putting every array member or object/hash key-value pair |
294 | format as output, putting every array member or object/hash key-value pair |
297 | into its own line, identing them properly. |
295 | into its own line, indenting them properly. |
298 | |
296 | |
299 | If C<$enable> is false, no newlines or indenting will be produced, and the |
297 | If C<$enable> is false, no newlines or indenting will be produced, and the |
300 | resulting JSON text is guarenteed not to contain any C<newlines>. |
298 | resulting JSON text is guaranteed not to contain any C<newlines>. |
301 | |
299 | |
302 | This setting has no effect when decoding JSON texts. |
300 | This setting has no effect when decoding JSON texts. |
303 | |
301 | |
304 | =item $json = $json->space_before ([$enable]) |
302 | =item $json = $json->space_before ([$enable]) |
305 | |
303 | |
… | |
… | |
386 | pairs in the order Perl stores them (which will likely change between runs |
384 | pairs in the order Perl stores them (which will likely change between runs |
387 | of the same script). |
385 | of the same script). |
388 | |
386 | |
389 | This option is useful if you want the same data structure to be encoded as |
387 | This option is useful if you want the same data structure to be encoded as |
390 | the same JSON text (given the same overall settings). If it is disabled, |
388 | the same JSON text (given the same overall settings). If it is disabled, |
391 | the same hash migh be encoded differently even if contains the same data, |
389 | the same hash might be encoded differently even if contains the same data, |
392 | as key-value pairs have no inherent ordering in Perl. |
390 | as key-value pairs have no inherent ordering in Perl. |
393 | |
391 | |
394 | This setting has no effect when decoding JSON texts. |
392 | This setting has no effect when decoding JSON texts. |
395 | |
393 | |
396 | =item $json = $json->allow_nonref ([$enable]) |
394 | =item $json = $json->allow_nonref ([$enable]) |
… | |
… | |
413 | |
411 | |
414 | =item $json = $json->allow_blessed ([$enable]) |
412 | =item $json = $json->allow_blessed ([$enable]) |
415 | |
413 | |
416 | If C<$enable> is true (or missing), then the C<encode> method will not |
414 | If C<$enable> is true (or missing), then the C<encode> method will not |
417 | barf when it encounters a blessed reference. Instead, the value of the |
415 | barf when it encounters a blessed reference. Instead, the value of the |
418 | B<convert_blessed> option will decide wether C<null> (C<convert_blessed> |
416 | B<convert_blessed> option will decide whether C<null> (C<convert_blessed> |
419 | disabled or no C<to_json> method found) or a representation of the |
417 | disabled or no C<to_json> method found) or a representation of the |
420 | object (C<convert_blessed> enabled and C<to_json> method found) is being |
418 | object (C<convert_blessed> enabled and C<to_json> method found) is being |
421 | encoded. Has no effect on C<decode>. |
419 | encoded. Has no effect on C<decode>. |
422 | |
420 | |
423 | If C<$enable> is false (the default), then C<encode> will throw an |
421 | If C<$enable> is false (the default), then C<encode> will throw an |
… | |
… | |
488 | |
486 | |
489 | As this callback gets called less often then the C<filter_json_object> |
487 | As this callback gets called less often then the C<filter_json_object> |
490 | one, decoding speed will not usually suffer as much. Therefore, single-key |
488 | one, decoding speed will not usually suffer as much. Therefore, single-key |
491 | objects make excellent targets to serialise Perl objects into, especially |
489 | objects make excellent targets to serialise Perl objects into, especially |
492 | as single-key JSON objects are as close to the type-tagged value concept |
490 | as single-key JSON objects are as close to the type-tagged value concept |
493 | as JSON gets (its basically an ID/VALUE tuple). Of course, JSON does not |
491 | as JSON gets (it's basically an ID/VALUE tuple). Of course, JSON does not |
494 | support this in any way, so you need to make sure your data never looks |
492 | support this in any way, so you need to make sure your data never looks |
495 | like a serialised Perl hash. |
493 | like a serialised Perl hash. |
496 | |
494 | |
497 | Typical names for the single object key are C<__class_whatever__>, or |
495 | Typical names for the single object key are C<__class_whatever__>, or |
498 | C<$__dollars_are_rarely_used__$> or C<}ugly_brace_placement>, or even |
496 | C<$__dollars_are_rarely_used__$> or C<}ugly_brace_placement>, or even |
… | |
… | |
625 | vice versa. These mappings are designed to "do the right thing" in most |
623 | vice versa. These mappings are designed to "do the right thing" in most |
626 | circumstances automatically, preserving round-tripping characteristics |
624 | circumstances automatically, preserving round-tripping characteristics |
627 | (what you put in comes out as something equivalent). |
625 | (what you put in comes out as something equivalent). |
628 | |
626 | |
629 | For the more enlightened: note that in the following descriptions, |
627 | For the more enlightened: note that in the following descriptions, |
630 | lowercase I<perl> refers to the Perl interpreter, while uppcercase I<Perl> |
628 | lowercase I<perl> refers to the Perl interpreter, while uppercase I<Perl> |
631 | refers to the abstract Perl language itself. |
629 | refers to the abstract Perl language itself. |
632 | |
630 | |
633 | |
631 | |
634 | =head2 JSON -> PERL |
632 | =head2 JSON -> PERL |
635 | |
633 | |
636 | =over 4 |
634 | =over 4 |
637 | |
635 | |
638 | =item object |
636 | =item object |
639 | |
637 | |
640 | A JSON object becomes a reference to a hash in Perl. No ordering of object |
638 | A JSON object becomes a reference to a hash in Perl. No ordering of object |
641 | keys is preserved (JSON does not preserver object key ordering itself). |
639 | keys is preserved (JSON does not preserve object key ordering itself). |
642 | |
640 | |
643 | =item array |
641 | =item array |
644 | |
642 | |
645 | A JSON array becomes a reference to an array in Perl. |
643 | A JSON array becomes a reference to an array in Perl. |
646 | |
644 | |
… | |
… | |
672 | |
670 | |
673 | =item true, false |
671 | =item true, false |
674 | |
672 | |
675 | These JSON atoms become C<JSON::XS::true> and C<JSON::XS::false>, |
673 | These JSON atoms become C<JSON::XS::true> and C<JSON::XS::false>, |
676 | respectively. They are overloaded to act almost exactly like the numbers |
674 | respectively. They are overloaded to act almost exactly like the numbers |
677 | C<1> and C<0>. You can check wether a scalar is a JSON boolean by using |
675 | C<1> and C<0>. You can check whether a scalar is a JSON boolean by using |
678 | the C<JSON::XS::is_bool> function. |
676 | the C<JSON::XS::is_bool> function. |
679 | |
677 | |
680 | =item null |
678 | =item null |
681 | |
679 | |
682 | A JSON null atom becomes C<undef> in Perl. |
680 | A JSON null atom becomes C<undef> in Perl. |
… | |
… | |
745 | to_json [$value] # yields ["5"] |
743 | to_json [$value] # yields ["5"] |
746 | |
744 | |
747 | # undef becomes null |
745 | # undef becomes null |
748 | to_json [undef] # yields [null] |
746 | to_json [undef] # yields [null] |
749 | |
747 | |
750 | You can force the type to be a string by stringifying it: |
748 | You can force the type to be a JSON string by stringifying it: |
751 | |
749 | |
752 | my $x = 3.1; # some variable containing a number |
750 | my $x = 3.1; # some variable containing a number |
753 | "$x"; # stringified |
751 | "$x"; # stringified |
754 | $x .= ""; # another, more awkward way to stringify |
752 | $x .= ""; # another, more awkward way to stringify |
755 | print $x; # perl does it for you, too, quite often |
753 | print $x; # perl does it for you, too, quite often |
756 | |
754 | |
757 | You can force the type to be a number by numifying it: |
755 | You can force the type to be a JSON number by numifying it: |
758 | |
756 | |
759 | my $x = "3"; # some variable containing a string |
757 | my $x = "3"; # some variable containing a string |
760 | $x += 0; # numify it, ensuring it will be dumped as a number |
758 | $x += 0; # numify it, ensuring it will be dumped as a number |
761 | $x *= 1; # same thing, the choise is yours. |
759 | $x *= 1; # same thing, the choice is yours. |
762 | |
760 | |
763 | You can not currently output JSON booleans or force the type in other, |
761 | You can not currently force the type in other, less obscure, ways. Tell me |
764 | less obscure, ways. Tell me if you need this capability. |
762 | if you need this capability. |
765 | |
763 | |
766 | =back |
764 | =back |
767 | |
765 | |
768 | |
766 | |
769 | =head1 COMPARISON |
767 | =head1 COMPARISON |
… | |
… | |
778 | |
776 | |
779 | =item JSON 1.07 |
777 | =item JSON 1.07 |
780 | |
778 | |
781 | Slow (but very portable, as it is written in pure Perl). |
779 | Slow (but very portable, as it is written in pure Perl). |
782 | |
780 | |
783 | Undocumented/buggy Unicode handling (how JSON handles unicode values is |
781 | Undocumented/buggy Unicode handling (how JSON handles Unicode values is |
784 | undocumented. One can get far by feeding it unicode strings and doing |
782 | undocumented. One can get far by feeding it Unicode strings and doing |
785 | en-/decoding oneself, but unicode escapes are not working properly). |
783 | en-/decoding oneself, but Unicode escapes are not working properly). |
786 | |
784 | |
787 | No roundtripping (strings get clobbered if they look like numbers, e.g. |
785 | No round-tripping (strings get clobbered if they look like numbers, e.g. |
788 | the string C<2.0> will encode to C<2.0> instead of C<"2.0">, and that will |
786 | the string C<2.0> will encode to C<2.0> instead of C<"2.0">, and that will |
789 | decode into the number 2. |
787 | decode into the number 2. |
790 | |
788 | |
791 | =item JSON::PC 0.01 |
789 | =item JSON::PC 0.01 |
792 | |
790 | |
793 | Very fast. |
791 | Very fast. |
794 | |
792 | |
795 | Undocumented/buggy Unicode handling. |
793 | Undocumented/buggy Unicode handling. |
796 | |
794 | |
797 | No roundtripping. |
795 | No round-tripping. |
798 | |
796 | |
799 | Has problems handling many Perl values (e.g. regex results and other magic |
797 | Has problems handling many Perl values (e.g. regex results and other magic |
800 | values will make it croak). |
798 | values will make it croak). |
801 | |
799 | |
802 | Does not even generate valid JSON (C<{1,2}> gets converted to C<{1:2}> |
800 | Does not even generate valid JSON (C<{1,2}> gets converted to C<{1:2}> |
… | |
… | |
812 | Very inflexible (no human-readable format supported, format pretty much |
810 | Very inflexible (no human-readable format supported, format pretty much |
813 | undocumented. I need at least a format for easy reading by humans and a |
811 | undocumented. I need at least a format for easy reading by humans and a |
814 | single-line compact format for use in a protocol, and preferably a way to |
812 | single-line compact format for use in a protocol, and preferably a way to |
815 | generate ASCII-only JSON texts). |
813 | generate ASCII-only JSON texts). |
816 | |
814 | |
817 | Completely broken (and confusingly documented) Unicode handling (unicode |
815 | Completely broken (and confusingly documented) Unicode handling (Unicode |
818 | escapes are not working properly, you need to set ImplicitUnicode to |
816 | escapes are not working properly, you need to set ImplicitUnicode to |
819 | I<different> values on en- and decoding to get symmetric behaviour). |
817 | I<different> values on en- and decoding to get symmetric behaviour). |
820 | |
818 | |
821 | No roundtripping (simple cases work, but this depends on wether the scalar |
819 | No round-tripping (simple cases work, but this depends on whether the scalar |
822 | value was used in a numeric context or not). |
820 | value was used in a numeric context or not). |
823 | |
821 | |
824 | Dumping hashes may skip hash values depending on iterator state. |
822 | Dumping hashes may skip hash values depending on iterator state. |
825 | |
823 | |
826 | Unmaintained (maintainer unresponsive for many months, bugs are not |
824 | Unmaintained (maintainer unresponsive for many months, bugs are not |
827 | getting fixed). |
825 | getting fixed). |
828 | |
826 | |
829 | Does not check input for validity (i.e. will accept non-JSON input and |
827 | Does not check input for validity (i.e. will accept non-JSON input and |
830 | return "something" instead of raising an exception. This is a security |
828 | return "something" instead of raising an exception. This is a security |
831 | issue: imagine two banks transfering money between each other using |
829 | issue: imagine two banks transferring money between each other using |
832 | JSON. One bank might parse a given non-JSON request and deduct money, |
830 | JSON. One bank might parse a given non-JSON request and deduct money, |
833 | while the other might reject the transaction with a syntax error. While a |
831 | while the other might reject the transaction with a syntax error. While a |
834 | good protocol will at least recover, that is extra unnecessary work and |
832 | good protocol will at least recover, that is extra unnecessary work and |
835 | the transaction will still not succeed). |
833 | the transaction will still not succeed). |
836 | |
834 | |
837 | =item JSON::DWIW 0.04 |
835 | =item JSON::DWIW 0.04 |
838 | |
836 | |
839 | Very fast. Very natural. Very nice. |
837 | Very fast. Very natural. Very nice. |
840 | |
838 | |
841 | Undocumented unicode handling (but the best of the pack. Unicode escapes |
839 | Undocumented Unicode handling (but the best of the pack. Unicode escapes |
842 | still don't get parsed properly). |
840 | still don't get parsed properly). |
843 | |
841 | |
844 | Very inflexible. |
842 | Very inflexible. |
845 | |
843 | |
846 | No roundtripping. |
844 | No round-tripping. |
847 | |
845 | |
848 | Does not generate valid JSON texts (key strings are often unquoted, empty keys |
846 | Does not generate valid JSON texts (key strings are often unquoted, empty keys |
849 | result in nothing being output) |
847 | result in nothing being output) |
850 | |
848 | |
851 | Does not check input for validity. |
849 | Does not check input for validity. |
… | |
… | |
866 | my $yaml = $to_yaml->encode ($ref) . "\n"; |
864 | my $yaml = $to_yaml->encode ($ref) . "\n"; |
867 | |
865 | |
868 | This will usually generate JSON texts that also parse as valid |
866 | This will usually generate JSON texts that also parse as valid |
869 | YAML. Please note that YAML has hardcoded limits on (simple) object key |
867 | YAML. Please note that YAML has hardcoded limits on (simple) object key |
870 | lengths that JSON doesn't have, so you should make sure that your hash |
868 | lengths that JSON doesn't have, so you should make sure that your hash |
871 | keys are noticably shorter than the 1024 characters YAML allows. |
869 | keys are noticeably shorter than the 1024 characters YAML allows. |
872 | |
870 | |
873 | There might be other incompatibilities that I am not aware of. In general |
871 | There might be other incompatibilities that I am not aware of. In general |
874 | you should not try to generate YAML with a JSON generator or vice versa, |
872 | you should not try to generate YAML with a JSON generator or vice versa, |
875 | or try to parse JSON with a YAML parser or vice versa: chances are high |
873 | or try to parse JSON with a YAML parser or vice versa: chances are high |
876 | that you will run into severe interoperability problems. |
874 | that you will run into severe interoperability problems. |
… | |
… | |
892 | It shows the number of encodes/decodes per second (JSON::XS uses |
890 | It shows the number of encodes/decodes per second (JSON::XS uses |
893 | the functional interface, while JSON::XS/2 uses the OO interface |
891 | the functional interface, while JSON::XS/2 uses the OO interface |
894 | with pretty-printing and hashkey sorting enabled, JSON::XS/3 enables |
892 | with pretty-printing and hashkey sorting enabled, JSON::XS/3 enables |
895 | shrink). Higher is better: |
893 | shrink). Higher is better: |
896 | |
894 | |
897 | Storable | 15779.925 | 14169.946 | |
|
|
898 | -----------+------------+------------+ |
|
|
899 | module | encode | decode | |
895 | module | encode | decode | |
900 | -----------|------------|------------| |
896 | -----------|------------|------------| |
901 | JSON | 4990.842 | 4088.813 | |
897 | JSON | 4990.842 | 4088.813 | |
902 | JSON::DWIW | 51653.990 | 71575.154 | |
898 | JSON::DWIW | 51653.990 | 71575.154 | |
903 | JSON::PC | 65948.176 | 74631.744 | |
899 | JSON::PC | 65948.176 | 74631.744 | |
… | |
… | |
908 | JSON::XS/3 | 338250.323 | 218453.333 | |
904 | JSON::XS/3 | 338250.323 | 218453.333 | |
909 | Storable | 16500.016 | 135300.129 | |
905 | Storable | 16500.016 | 135300.129 | |
910 | -----------+------------+------------+ |
906 | -----------+------------+------------+ |
911 | |
907 | |
912 | That is, JSON::XS is about five times faster than JSON::DWIW on encoding, |
908 | That is, JSON::XS is about five times faster than JSON::DWIW on encoding, |
913 | about three times faster on decoding, and over fourty times faster |
909 | about three times faster on decoding, and over forty times faster |
914 | than JSON, even with pretty-printing and key sorting. It also compares |
910 | than JSON, even with pretty-printing and key sorting. It also compares |
915 | favourably to Storable for small amounts of data. |
911 | favourably to Storable for small amounts of data. |
916 | |
912 | |
917 | Using a longer test string (roughly 18KB, generated from Yahoo! Locals |
913 | Using a longer test string (roughly 18KB, generated from Yahoo! Locals |
918 | search API (http://nanoref.com/yahooapis/mgPdGg): |
914 | search API (http://nanoref.com/yahooapis/mgPdGg): |
… | |
… | |
931 | -----------+------------+------------+ |
927 | -----------+------------+------------+ |
932 | |
928 | |
933 | Again, JSON::XS leads by far (except for Storable which non-surprisingly |
929 | Again, JSON::XS leads by far (except for Storable which non-surprisingly |
934 | decodes faster). |
930 | decodes faster). |
935 | |
931 | |
936 | On large strings containing lots of high unicode characters, some modules |
932 | On large strings containing lots of high Unicode characters, some modules |
937 | (such as JSON::PC) seem to decode faster than JSON::XS, but the result |
933 | (such as JSON::PC) seem to decode faster than JSON::XS, but the result |
938 | will be broken due to missing (or wrong) unicode handling. Others refuse |
934 | will be broken due to missing (or wrong) Unicode handling. Others refuse |
939 | to decode or encode properly, so it was impossible to prepare a fair |
935 | to decode or encode properly, so it was impossible to prepare a fair |
940 | comparison table for that case. |
936 | comparison table for that case. |
941 | |
937 | |
942 | |
938 | |
943 | =head1 SECURITY CONSIDERATIONS |
939 | =head1 SECURITY CONSIDERATIONS |
… | |
… | |
949 | any buffer overflows. Obviously, this module should ensure that and I am |
945 | any buffer overflows. Obviously, this module should ensure that and I am |
950 | trying hard on making that true, but you never know. |
946 | trying hard on making that true, but you never know. |
951 | |
947 | |
952 | Second, you need to avoid resource-starving attacks. That means you should |
948 | Second, you need to avoid resource-starving attacks. That means you should |
953 | limit the size of JSON texts you accept, or make sure then when your |
949 | limit the size of JSON texts you accept, or make sure then when your |
954 | resources run out, thats just fine (e.g. by using a separate process that |
950 | resources run out, that's just fine (e.g. by using a separate process that |
955 | can crash safely). The size of a JSON text in octets or characters is |
951 | can crash safely). The size of a JSON text in octets or characters is |
956 | usually a good indication of the size of the resources required to decode |
952 | usually a good indication of the size of the resources required to decode |
957 | it into a Perl structure. While JSON::XS can check the size of the JSON |
953 | it into a Perl structure. While JSON::XS can check the size of the JSON |
958 | text, it might be too late when you already have it in memory, so you |
954 | text, it might be too late when you already have it in memory, so you |
959 | might want to check the size before you accept the string. |
955 | might want to check the size before you accept the string. |
… | |
… | |
970 | And last but least, something else could bomb you that I forgot to think |
966 | And last but least, something else could bomb you that I forgot to think |
971 | of. In that case, you get to keep the pieces. I am always open for hints, |
967 | of. In that case, you get to keep the pieces. I am always open for hints, |
972 | though... |
968 | though... |
973 | |
969 | |
974 | If you are using JSON::XS to return packets to consumption |
970 | If you are using JSON::XS to return packets to consumption |
975 | by javascript scripts in a browser you should have a look at |
971 | by JavaScript scripts in a browser you should have a look at |
976 | L<http://jpsykes.com/47/practical-csrf-and-json-security> to see wether |
972 | L<http://jpsykes.com/47/practical-csrf-and-json-security> to see whether |
977 | you are vulnerable to some common attack vectors (which really are browser |
973 | you are vulnerable to some common attack vectors (which really are browser |
978 | design bugs, but it is still you who will have to deal with it, as major |
974 | design bugs, but it is still you who will have to deal with it, as major |
979 | browser developers care only for features, not about doing security |
975 | browser developers care only for features, not about doing security |
980 | right). |
976 | right). |
981 | |
977 | |
982 | |
978 | |
983 | =head1 THREADS |
979 | =head1 THREADS |
984 | |
980 | |
985 | This module is I<not> guarenteed to be thread safe and there are no |
981 | This module is I<not> guaranteed to be thread safe and there are no |
986 | plans to change this until Perl gets thread support (as opposed to the |
982 | plans to change this until Perl gets thread support (as opposed to the |
987 | horribly slow so-called "threads" which are simply slow and bloated |
983 | horribly slow so-called "threads" which are simply slow and bloated |
988 | process simulations - use fork, its I<much> faster, cheaper, better). |
984 | process simulations - use fork, its I<much> faster, cheaper, better). |
989 | |
985 | |
990 | (It might actually work, but you ahve ben warned). |
986 | (It might actually work, but you have been warned). |
991 | |
987 | |
992 | |
988 | |
993 | =head1 BUGS |
989 | =head1 BUGS |
994 | |
990 | |
995 | While the goal of this module is to be correct, that unfortunately does |
991 | While the goal of this module is to be correct, that unfortunately does |