… | |
… | |
49 | by JSON, the deserialised data structure is identical on the Perl level. |
49 | by JSON, the deserialised data structure is identical on the Perl level. |
50 | (e.g. the string "2.0" doesn't suddenly become "2"). |
50 | (e.g. the string "2.0" doesn't suddenly become "2"). |
51 | |
51 | |
52 | =item * strict checking of JSON correctness |
52 | =item * strict checking of JSON correctness |
53 | |
53 | |
54 | There is no guessing, no generating of illegal JSON strings by default, |
54 | There is no guessing, no generating of illegal JSON texts by default, |
55 | and only JSON is accepted as input by default (the latter is a security |
55 | and only JSON is accepted as input by default (the latter is a security |
56 | feature). |
56 | feature). |
57 | |
57 | |
58 | =item * fast |
58 | =item * fast |
59 | |
59 | |
… | |
… | |
78 | =cut |
78 | =cut |
79 | |
79 | |
80 | package JSON::XS; |
80 | package JSON::XS; |
81 | |
81 | |
82 | BEGIN { |
82 | BEGIN { |
83 | $VERSION = '0.3'; |
83 | $VERSION = '0.31'; |
84 | @ISA = qw(Exporter); |
84 | @ISA = qw(Exporter); |
85 | |
85 | |
86 | @EXPORT = qw(to_json from_json); |
86 | @EXPORT = qw(to_json from_json); |
87 | require Exporter; |
87 | require Exporter; |
88 | |
88 | |
… | |
… | |
95 | The following convinience methods are provided by this module. They are |
95 | The following convinience methods are provided by this module. They are |
96 | exported by default: |
96 | exported by default: |
97 | |
97 | |
98 | =over 4 |
98 | =over 4 |
99 | |
99 | |
100 | =item $json_string = to_json $perl_scalar |
100 | =item $json_text = to_json $perl_scalar |
101 | |
101 | |
102 | Converts the given Perl data structure (a simple scalar or a reference to |
102 | Converts the given Perl data structure (a simple scalar or a reference to |
103 | a hash or array) to a UTF-8 encoded, binary string (that is, the string contains |
103 | a hash or array) to a UTF-8 encoded, binary string (that is, the string contains |
104 | octets only). Croaks on error. |
104 | octets only). Croaks on error. |
105 | |
105 | |
106 | This function call is functionally identical to C<< JSON::XS->new->utf8->encode ($perl_scalar) >>. |
106 | This function call is functionally identical to: |
107 | |
107 | |
|
|
108 | $json_text = JSON::XS->new->utf8->encode ($perl_scalar) |
|
|
109 | |
|
|
110 | except being faster. |
|
|
111 | |
108 | =item $perl_scalar = from_json $json_string |
112 | =item $perl_scalar = from_json $json_text |
109 | |
113 | |
110 | The opposite of C<to_json>: expects an UTF-8 (binary) string and tries to |
114 | The opposite of C<to_json>: expects an UTF-8 (binary) string and tries to |
111 | parse that as an UTF-8 encoded JSON string, returning the resulting simple |
115 | parse that as an UTF-8 encoded JSON text, returning the resulting simple |
112 | scalar or reference. Croaks on error. |
116 | scalar or reference. Croaks on error. |
113 | |
117 | |
114 | This function call is functionally identical to C<< JSON::XS->new->utf8->decode ($json_string) >>. |
118 | This function call is functionally identical to: |
|
|
119 | |
|
|
120 | $perl_scalar = JSON::XS->new->utf8->decode ($json_text) |
|
|
121 | |
|
|
122 | except being faster. |
115 | |
123 | |
116 | =back |
124 | =back |
117 | |
125 | |
118 | =head1 OBJECT-ORIENTED INTERFACE |
126 | =head1 OBJECT-ORIENTED INTERFACE |
119 | |
127 | |
… | |
… | |
128 | strings. All boolean flags described below are by default I<disabled>. |
136 | strings. All boolean flags described below are by default I<disabled>. |
129 | |
137 | |
130 | The mutators for flags all return the JSON object again and thus calls can |
138 | The mutators for flags all return the JSON object again and thus calls can |
131 | be chained: |
139 | be chained: |
132 | |
140 | |
133 | my $json = JSON::XS->new->utf8(1)->space_after(1)->encode ({a => [1,2]}) |
141 | my $json = JSON::XS->new->utf8->space_after->encode ({a => [1,2]}) |
134 | => {"a": [1, 2]} |
142 | => {"a": [1, 2]} |
135 | |
143 | |
136 | =item $json = $json->ascii ([$enable]) |
144 | =item $json = $json->ascii ([$enable]) |
137 | |
145 | |
138 | If C<$enable> is true (or missing), then the C<encode> method will |
146 | If C<$enable> is true (or missing), then the C<encode> method will not |
139 | not generate characters outside the code range C<0..127>. Any unicode |
147 | generate characters outside the code range C<0..127> (which is ASCII). Any |
140 | characters outside that range will be escaped using either a single |
148 | unicode characters outside that range will be escaped using either a |
141 | \uXXXX (BMP characters) or a double \uHHHH\uLLLLL escape sequence, as per |
149 | single \uXXXX (BMP characters) or a double \uHHHH\uLLLLL escape sequence, |
142 | RFC4627. |
150 | as per RFC4627. |
143 | |
151 | |
144 | If C<$enable> is false, then the C<encode> method will not escape Unicode |
152 | If C<$enable> is false, then the C<encode> method will not escape Unicode |
145 | characters unless necessary. |
153 | characters unless required by the JSON syntax. This results in a faster |
|
|
154 | and more compact format. |
146 | |
155 | |
147 | JSON::XS->new->ascii (1)->encode (chr 0x10401) |
156 | JSON::XS->new->ascii (1)->encode ([chr 0x10401]) |
148 | => \ud801\udc01 |
157 | => ["\ud801\udc01"] |
149 | |
158 | |
150 | =item $json = $json->utf8 ([$enable]) |
159 | =item $json = $json->utf8 ([$enable]) |
151 | |
160 | |
152 | If C<$enable> is true (or missing), then the C<encode> method will encode |
161 | If C<$enable> is true (or missing), then the C<encode> method will encode |
153 | the JSON string into UTF-8, as required by many protocols, while the |
162 | the JSON result into UTF-8, as required by many protocols, while the |
154 | C<decode> method expects to be handled an UTF-8-encoded string. Please |
163 | C<decode> method expects to be handled an UTF-8-encoded string. Please |
155 | note that UTF-8-encoded strings do not contain any characters outside the |
164 | note that UTF-8-encoded strings do not contain any characters outside the |
156 | range C<0..255>, they are thus useful for bytewise/binary I/O. |
165 | range C<0..255>, they are thus useful for bytewise/binary I/O. In future |
|
|
166 | versions, enabling this option might enable autodetection of the UTF-16 |
|
|
167 | and UTF-32 encoding families, as described in RFC4627. |
157 | |
168 | |
158 | If C<$enable> is false, then the C<encode> method will return the JSON |
169 | If C<$enable> is false, then the C<encode> method will return the JSON |
159 | string as a (non-encoded) unicode string, while C<decode> expects thus a |
170 | string as a (non-encoded) unicode string, while C<decode> expects thus a |
160 | unicode string. Any decoding or encoding (e.g. to UTF-8 or UTF-16) needs |
171 | unicode string. Any decoding or encoding (e.g. to UTF-8 or UTF-16) needs |
161 | to be done yourself, e.g. using the Encode module. |
172 | to be done yourself, e.g. using the Encode module. |
162 | |
173 | |
163 | Example, output UTF-16-encoded JSON: |
174 | Example, output UTF-16BE-encoded JSON: |
|
|
175 | |
|
|
176 | use Encode; |
|
|
177 | $jsontext = encode "UTF-16BE", JSON::XS->new->encode ($object); |
|
|
178 | |
|
|
179 | Example, decode UTF-32LE-encoded JSON: |
|
|
180 | |
|
|
181 | use Encode; |
|
|
182 | $object = JSON::XS->new->decode (decode "UTF-32LE", $jsontext); |
164 | |
183 | |
165 | =item $json = $json->pretty ([$enable]) |
184 | =item $json = $json->pretty ([$enable]) |
166 | |
185 | |
167 | This enables (or disables) all of the C<indent>, C<space_before> and |
186 | This enables (or disables) all of the C<indent>, C<space_before> and |
168 | C<space_after> (and in the future possibly more) flags in one call to |
187 | C<space_after> (and in the future possibly more) flags in one call to |
… | |
… | |
184 | If C<$enable> is true (or missing), then the C<encode> method will use a multiline |
203 | If C<$enable> is true (or missing), then the C<encode> method will use a multiline |
185 | format as output, putting every array member or object/hash key-value pair |
204 | format as output, putting every array member or object/hash key-value pair |
186 | into its own line, identing them properly. |
205 | into its own line, identing them properly. |
187 | |
206 | |
188 | If C<$enable> is false, no newlines or indenting will be produced, and the |
207 | If C<$enable> is false, no newlines or indenting will be produced, and the |
189 | resulting JSON strings is guarenteed not to contain any C<newlines>. |
208 | resulting JSON text is guarenteed not to contain any C<newlines>. |
190 | |
209 | |
191 | This setting has no effect when decoding JSON strings. |
210 | This setting has no effect when decoding JSON texts. |
192 | |
211 | |
193 | =item $json = $json->space_before ([$enable]) |
212 | =item $json = $json->space_before ([$enable]) |
194 | |
213 | |
195 | If C<$enable> is true (or missing), then the C<encode> method will add an extra |
214 | If C<$enable> is true (or missing), then the C<encode> method will add an extra |
196 | optional space before the C<:> separating keys from values in JSON objects. |
215 | optional space before the C<:> separating keys from values in JSON objects. |
197 | |
216 | |
198 | If C<$enable> is false, then the C<encode> method will not add any extra |
217 | If C<$enable> is false, then the C<encode> method will not add any extra |
199 | space at those places. |
218 | space at those places. |
200 | |
219 | |
201 | This setting has no effect when decoding JSON strings. You will also most |
220 | This setting has no effect when decoding JSON texts. You will also |
202 | likely combine this setting with C<space_after>. |
221 | most likely combine this setting with C<space_after>. |
203 | |
222 | |
204 | Example, space_before enabled, space_after and indent disabled: |
223 | Example, space_before enabled, space_after and indent disabled: |
205 | |
224 | |
206 | {"key" :"value"} |
225 | {"key" :"value"} |
207 | |
226 | |
… | |
… | |
213 | members. |
232 | members. |
214 | |
233 | |
215 | If C<$enable> is false, then the C<encode> method will not add any extra |
234 | If C<$enable> is false, then the C<encode> method will not add any extra |
216 | space at those places. |
235 | space at those places. |
217 | |
236 | |
218 | This setting has no effect when decoding JSON strings. |
237 | This setting has no effect when decoding JSON texts. |
219 | |
238 | |
220 | Example, space_before and indent disabled, space_after enabled: |
239 | Example, space_before and indent disabled, space_after enabled: |
221 | |
240 | |
222 | {"key": "value"} |
241 | {"key": "value"} |
223 | |
242 | |
… | |
… | |
229 | If C<$enable> is false, then the C<encode> method will output key-value |
248 | If C<$enable> is false, then the C<encode> method will output key-value |
230 | pairs in the order Perl stores them (which will likely change between runs |
249 | pairs in the order Perl stores them (which will likely change between runs |
231 | of the same script). |
250 | of the same script). |
232 | |
251 | |
233 | This option is useful if you want the same data structure to be encoded as |
252 | This option is useful if you want the same data structure to be encoded as |
234 | the same JSON string (given the same overall settings). If it is disabled, |
253 | the same JSON text (given the same overall settings). If it is disabled, |
235 | the same hash migh be encoded differently even if contains the same data, |
254 | the same hash migh be encoded differently even if contains the same data, |
236 | as key-value pairs have no inherent ordering in Perl. |
255 | as key-value pairs have no inherent ordering in Perl. |
237 | |
256 | |
238 | This setting has no effect when decoding JSON strings. |
257 | This setting has no effect when decoding JSON texts. |
239 | |
258 | |
240 | =item $json = $json->allow_nonref ([$enable]) |
259 | =item $json = $json->allow_nonref ([$enable]) |
241 | |
260 | |
242 | If C<$enable> is true (or missing), then the C<encode> method can convert a |
261 | If C<$enable> is true (or missing), then the C<encode> method can convert a |
243 | non-reference into its corresponding string, number or null JSON value, |
262 | non-reference into its corresponding string, number or null JSON value, |
244 | which is an extension to RFC4627. Likewise, C<decode> will accept those JSON |
263 | which is an extension to RFC4627. Likewise, C<decode> will accept those JSON |
245 | values instead of croaking. |
264 | values instead of croaking. |
246 | |
265 | |
247 | If C<$enable> is false, then the C<encode> method will croak if it isn't |
266 | If C<$enable> is false, then the C<encode> method will croak if it isn't |
248 | passed an arrayref or hashref, as JSON strings must either be an object |
267 | passed an arrayref or hashref, as JSON texts must either be an object |
249 | or array. Likewise, C<decode> will croak if given something that is not a |
268 | or array. Likewise, C<decode> will croak if given something that is not a |
250 | JSON object or array. |
269 | JSON object or array. |
251 | |
270 | |
252 | Example, encode a Perl scalar as JSON value with enabled C<allow_nonref>, |
271 | Example, encode a Perl scalar as JSON value with enabled C<allow_nonref>, |
253 | resulting in an invalid JSON text: |
272 | resulting in an invalid JSON text: |
… | |
… | |
258 | =item $json = $json->shrink ([$enable]) |
277 | =item $json = $json->shrink ([$enable]) |
259 | |
278 | |
260 | Perl usually over-allocates memory a bit when allocating space for |
279 | Perl usually over-allocates memory a bit when allocating space for |
261 | strings. This flag optionally resizes strings generated by either |
280 | strings. This flag optionally resizes strings generated by either |
262 | C<encode> or C<decode> to their minimum size possible. This can save |
281 | C<encode> or C<decode> to their minimum size possible. This can save |
263 | memory when your JSON strings are either very very long or you have many |
282 | memory when your JSON texts are either very very long or you have many |
264 | short strings. It will also try to downgrade any strings to octet-form |
283 | short strings. It will also try to downgrade any strings to octet-form |
265 | if possible: perl stores strings internally either in an encoding called |
284 | if possible: perl stores strings internally either in an encoding called |
266 | UTF-X or in octet-form. The latter cannot store everything but uses less |
285 | UTF-X or in octet-form. The latter cannot store everything but uses less |
267 | space in general. |
286 | space in general. |
268 | |
287 | |
… | |
… | |
274 | |
293 | |
275 | In the future, this setting might control other things, such as converting |
294 | In the future, this setting might control other things, such as converting |
276 | strings that look like integers or floats into integers or floats |
295 | strings that look like integers or floats into integers or floats |
277 | internally (there is no difference on the Perl level), saving space. |
296 | internally (there is no difference on the Perl level), saving space. |
278 | |
297 | |
279 | =item $json_string = $json->encode ($perl_scalar) |
298 | =item $json_text = $json->encode ($perl_scalar) |
280 | |
299 | |
281 | Converts the given Perl data structure (a simple scalar or a reference |
300 | Converts the given Perl data structure (a simple scalar or a reference |
282 | to a hash or array) to its JSON representation. Simple scalars will be |
301 | to a hash or array) to its JSON representation. Simple scalars will be |
283 | converted into JSON string or number sequences, while references to arrays |
302 | converted into JSON string or number sequences, while references to arrays |
284 | become JSON arrays and references to hashes become JSON objects. Undefined |
303 | become JSON arrays and references to hashes become JSON objects. Undefined |
285 | Perl values (e.g. C<undef>) become JSON C<null> values. Neither C<true> |
304 | Perl values (e.g. C<undef>) become JSON C<null> values. Neither C<true> |
286 | nor C<false> values will be generated. |
305 | nor C<false> values will be generated. |
287 | |
306 | |
288 | =item $perl_scalar = $json->decode ($json_string) |
307 | =item $perl_scalar = $json->decode ($json_text) |
289 | |
308 | |
290 | The opposite of C<encode>: expects a JSON string and tries to parse it, |
309 | The opposite of C<encode>: expects a JSON text and tries to parse it, |
291 | returning the resulting simple scalar or reference. Croaks on error. |
310 | returning the resulting simple scalar or reference. Croaks on error. |
292 | |
311 | |
293 | JSON numbers and strings become simple Perl scalars. JSON arrays become |
312 | JSON numbers and strings become simple Perl scalars. JSON arrays become |
294 | Perl arrayrefs and JSON objects become Perl hashrefs. C<true> becomes |
313 | Perl arrayrefs and JSON objects become Perl hashrefs. C<true> becomes |
295 | C<1>, C<false> becomes C<0> and C<null> becomes C<undef>. |
314 | C<1>, C<false> becomes C<0> and C<null> becomes C<undef>. |
… | |
… | |
312 | =over 4 |
331 | =over 4 |
313 | |
332 | |
314 | =item object |
333 | =item object |
315 | |
334 | |
316 | A JSON object becomes a reference to a hash in Perl. No ordering of object |
335 | A JSON object becomes a reference to a hash in Perl. No ordering of object |
317 | keys is preserved. |
336 | keys is preserved (JSON does not preserver object key ordering itself). |
318 | |
337 | |
319 | =item array |
338 | =item array |
320 | |
339 | |
321 | A JSON array becomes a reference to an array in Perl. |
340 | A JSON array becomes a reference to an array in Perl. |
322 | |
341 | |
… | |
… | |
358 | =item hash references |
377 | =item hash references |
359 | |
378 | |
360 | Perl hash references become JSON objects. As there is no inherent ordering |
379 | Perl hash references become JSON objects. As there is no inherent ordering |
361 | in hash keys, they will usually be encoded in a pseudo-random order that |
380 | in hash keys, they will usually be encoded in a pseudo-random order that |
362 | can change between runs of the same program but stays generally the same |
381 | can change between runs of the same program but stays generally the same |
363 | within the single run of a program. JSON::XS can optionally sort the hash |
382 | within a single run of a program. JSON::XS can optionally sort the hash |
364 | keys (determined by the I<canonical> flag), so the same datastructure |
383 | keys (determined by the I<canonical> flag), so the same datastructure |
365 | will serialise to the same JSON text (given same settings and version of |
384 | will serialise to the same JSON text (given same settings and version of |
366 | JSON::XS), but this incurs a runtime overhead. |
385 | JSON::XS), but this incurs a runtime overhead. |
367 | |
386 | |
368 | =item array references |
387 | =item array references |
… | |
… | |
448 | |
467 | |
449 | Has problems handling many Perl values (e.g. regex results and other magic |
468 | Has problems handling many Perl values (e.g. regex results and other magic |
450 | values will make it croak). |
469 | values will make it croak). |
451 | |
470 | |
452 | Does not even generate valid JSON (C<{1,2}> gets converted to C<{1:2}> |
471 | Does not even generate valid JSON (C<{1,2}> gets converted to C<{1:2}> |
453 | which is not a valid JSON string. |
472 | which is not a valid JSON text. |
454 | |
473 | |
455 | Unmaintained (maintainer unresponsive for many months, bugs are not |
474 | Unmaintained (maintainer unresponsive for many months, bugs are not |
456 | getting fixed). |
475 | getting fixed). |
457 | |
476 | |
458 | =item JSON::Syck 0.21 |
477 | =item JSON::Syck 0.21 |
… | |
… | |
460 | Very buggy (often crashes). |
479 | Very buggy (often crashes). |
461 | |
480 | |
462 | Very inflexible (no human-readable format supported, format pretty much |
481 | Very inflexible (no human-readable format supported, format pretty much |
463 | undocumented. I need at least a format for easy reading by humans and a |
482 | undocumented. I need at least a format for easy reading by humans and a |
464 | single-line compact format for use in a protocol, and preferably a way to |
483 | single-line compact format for use in a protocol, and preferably a way to |
465 | generate ASCII-only JSON strings). |
484 | generate ASCII-only JSON texts). |
466 | |
485 | |
467 | Completely broken (and confusingly documented) Unicode handling (unicode |
486 | Completely broken (and confusingly documented) Unicode handling (unicode |
468 | escapes are not working properly, you need to set ImplicitUnicode to |
487 | escapes are not working properly, you need to set ImplicitUnicode to |
469 | I<different> values on en- and decoding to get symmetric behaviour). |
488 | I<different> values on en- and decoding to get symmetric behaviour). |
470 | |
489 | |
… | |
… | |
493 | |
512 | |
494 | Very inflexible. |
513 | Very inflexible. |
495 | |
514 | |
496 | No roundtripping. |
515 | No roundtripping. |
497 | |
516 | |
498 | Does not generate valid JSON (key strings are often unquoted, empty keys |
517 | Does not generate valid JSON texts (key strings are often unquoted, empty keys |
499 | result in nothing being output) |
518 | result in nothing being output) |
500 | |
519 | |
501 | Does not check input for validity. |
520 | Does not check input for validity. |
502 | |
521 | |
503 | =back |
522 | =back |
… | |
… | |
507 | It seems that JSON::XS is surprisingly fast, as shown in the following |
526 | It seems that JSON::XS is surprisingly fast, as shown in the following |
508 | tables. They have been generated with the help of the C<eg/bench> program |
527 | tables. They have been generated with the help of the C<eg/bench> program |
509 | in the JSON::XS distribution, to make it easy to compare on your own |
528 | in the JSON::XS distribution, to make it easy to compare on your own |
510 | system. |
529 | system. |
511 | |
530 | |
512 | First is a comparison between various modules using a very simple JSON |
531 | First comes a comparison between various modules using a very short JSON |
513 | string, showing the number of encodes/decodes per second (JSON::XS is |
532 | string (83 bytes), showing the number of encodes/decodes per second |
514 | the functional interface, while JSON::XS/2 is the OO interface with |
533 | (JSON::XS is the functional interface, while JSON::XS/2 is the OO |
515 | pretty-printing and hashkey sorting enabled). |
534 | interface with pretty-printing and hashkey sorting enabled). Higher is |
|
|
535 | better: |
516 | |
536 | |
517 | module | encode | decode | |
537 | module | encode | decode | |
518 | -----------|------------|------------| |
538 | -----------|------------|------------| |
519 | JSON | 14006 | 6820 | |
539 | JSON | 14006 | 6820 | |
520 | JSON::DWIW | 200937 | 120386 | |
540 | JSON::DWIW | 200937 | 120386 | |
… | |
… | |
525 | -----------+------------+------------+ |
545 | -----------+------------+------------+ |
526 | |
546 | |
527 | That is, JSON::XS is 6 times faster than than JSON::DWIW and about 80 |
547 | That is, JSON::XS is 6 times faster than than JSON::DWIW and about 80 |
528 | times faster than JSON, even with pretty-printing and key sorting. |
548 | times faster than JSON, even with pretty-printing and key sorting. |
529 | |
549 | |
530 | Using a longer test string (roughly 8KB, generated from Yahoo! Locals |
550 | Using a longer test string (roughly 18KB, generated from Yahoo! Locals |
531 | search API (http://nanoref.com/yahooapis/mgPdGg): |
551 | search API (http://nanoref.com/yahooapis/mgPdGg): |
532 | |
552 | |
533 | module | encode | decode | |
553 | module | encode | decode | |
534 | -----------|------------|------------| |
554 | -----------|------------|------------| |
535 | JSON | 673 | 38 | |
555 | JSON | 673 | 38 | |
… | |
… | |
541 | -----------+------------+------------+ |
561 | -----------+------------+------------+ |
542 | |
562 | |
543 | Again, JSON::XS leads by far in the encoding case, while still beating |
563 | Again, JSON::XS leads by far in the encoding case, while still beating |
544 | every other module in the decoding case. |
564 | every other module in the decoding case. |
545 | |
565 | |
|
|
566 | On large strings containing lots of unicode characters, some modules |
|
|
567 | (such as JSON::PC) decode faster than JSON::XS, but the result will be |
|
|
568 | broken due to missing unicode handling. Others refuse to decode or encode |
|
|
569 | properly, so it was impossible to prepare a fair comparison table for that |
|
|
570 | case. |
|
|
571 | |
546 | =head1 RESOURCE LIMITS |
572 | =head1 RESOURCE LIMITS |
547 | |
573 | |
548 | JSON::XS does not impose any limits on the size of JSON texts or Perl |
574 | JSON::XS does not impose any limits on the size of JSON texts or Perl |
549 | values they represent - if your machine can handle it, JSON::XS will |
575 | values they represent - if your machine can handle it, JSON::XS will |
550 | encode or decode it. Future versions might optionally impose structure |
576 | encode or decode it. Future versions might optionally impose structure |