… | |
… | |
4 | |
4 | |
5 | =head1 SYNOPSIS |
5 | =head1 SYNOPSIS |
6 | |
6 | |
7 | use JSON::XS; |
7 | use JSON::XS; |
8 | |
8 | |
9 | # exported functions, croak on error |
9 | # exported functions, they croak on error |
|
|
10 | # and expect/generate UTF-8 |
10 | |
11 | |
11 | $utf8_encoded_json_text = to_json $perl_hash_or_arrayref; |
12 | $utf8_encoded_json_text = to_json $perl_hash_or_arrayref; |
12 | $perl_hash_or_arrayref = from_json $utf8_encoded_json_text; |
13 | $perl_hash_or_arrayref = from_json $utf8_encoded_json_text; |
13 | |
14 | |
|
|
15 | # objToJson and jsonToObj aliases to to_json and from_json |
|
|
16 | # are exported for compatibility to the JSON module, |
|
|
17 | # but should not be used in new code. |
|
|
18 | |
14 | # oo-interface |
19 | # OO-interface |
15 | |
20 | |
16 | $coder = JSON::XS->new->ascii->pretty->allow_nonref; |
21 | $coder = JSON::XS->new->ascii->pretty->allow_nonref; |
17 | $pretty_printed_unencoded = $coder->encode ($perl_scalar); |
22 | $pretty_printed_unencoded = $coder->encode ($perl_scalar); |
18 | $perl_scalar = $coder->decode ($unicode_json_text); |
23 | $perl_scalar = $coder->decode ($unicode_json_text); |
19 | |
24 | |
… | |
… | |
36 | |
41 | |
37 | =head2 FEATURES |
42 | =head2 FEATURES |
38 | |
43 | |
39 | =over 4 |
44 | =over 4 |
40 | |
45 | |
41 | =item * correct handling of unicode issues |
46 | =item * correct unicode handling |
42 | |
47 | |
43 | This module knows how to handle Unicode, and even documents how and when |
48 | This module knows how to handle Unicode, and even documents how and when |
44 | it does so. |
49 | it does so. |
45 | |
50 | |
46 | =item * round-trip integrity |
51 | =item * round-trip integrity |
47 | |
52 | |
48 | When you serialise a perl data structure using only datatypes supported |
53 | When you serialise a perl data structure using only datatypes supported |
49 | by JSON, the deserialised data structure is identical on the Perl level. |
54 | by JSON, the deserialised data structure is identical on the Perl level. |
50 | (e.g. the string "2.0" doesn't suddenly become "2"). |
55 | (e.g. the string "2.0" doesn't suddenly become "2" just because it looks |
|
|
56 | like a number). |
51 | |
57 | |
52 | =item * strict checking of JSON correctness |
58 | =item * strict checking of JSON correctness |
53 | |
59 | |
54 | There is no guessing, no generating of illegal JSON strings by default, |
60 | 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 |
61 | and only JSON is accepted as input by default (the latter is a security |
56 | feature). |
62 | feature). |
57 | |
63 | |
58 | =item * fast |
64 | =item * fast |
59 | |
65 | |
… | |
… | |
66 | interface. |
72 | interface. |
67 | |
73 | |
68 | =item * reasonably versatile output formats |
74 | =item * reasonably versatile output formats |
69 | |
75 | |
70 | You can choose between the most compact guarenteed single-line format |
76 | You can choose between the most compact guarenteed single-line format |
71 | possible (nice for simple line-based protocols), a pure-ascii format (for |
77 | possible (nice for simple line-based protocols), a pure-ascii format |
72 | when your transport is not 8-bit clean), or a pretty-printed format (for |
78 | (for when your transport is not 8-bit clean, still supports the whole |
73 | when you want to read that stuff). Or you can combine those features in |
79 | unicode range), or a pretty-printed format (for when you want to read that |
74 | whatever way you like. |
80 | stuff). Or you can combine those features in whatever way you like. |
75 | |
81 | |
76 | =back |
82 | =back |
77 | |
83 | |
78 | =cut |
84 | =cut |
79 | |
85 | |
80 | package JSON::XS; |
86 | package JSON::XS; |
81 | |
87 | |
|
|
88 | use strict; |
|
|
89 | |
82 | BEGIN { |
90 | BEGIN { |
83 | $VERSION = '0.31'; |
91 | our $VERSION = '0.8'; |
84 | @ISA = qw(Exporter); |
92 | our @ISA = qw(Exporter); |
85 | |
93 | |
86 | @EXPORT = qw(to_json from_json); |
94 | our @EXPORT = qw(to_json from_json objToJson jsonToObj); |
87 | require Exporter; |
95 | require Exporter; |
88 | |
96 | |
89 | require XSLoader; |
97 | require XSLoader; |
90 | XSLoader::load JSON::XS::, $VERSION; |
98 | XSLoader::load JSON::XS::, $VERSION; |
91 | } |
99 | } |
… | |
… | |
95 | The following convinience methods are provided by this module. They are |
103 | The following convinience methods are provided by this module. They are |
96 | exported by default: |
104 | exported by default: |
97 | |
105 | |
98 | =over 4 |
106 | =over 4 |
99 | |
107 | |
100 | =item $json_string = to_json $perl_scalar |
108 | =item $json_text = to_json $perl_scalar |
101 | |
109 | |
102 | Converts the given Perl data structure (a simple scalar or a reference to |
110 | 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 |
111 | a hash or array) to a UTF-8 encoded, binary string (that is, the string contains |
104 | octets only). Croaks on error. |
112 | octets only). Croaks on error. |
105 | |
113 | |
106 | This function call is functionally identical to C<< JSON::XS->new->utf8->encode ($perl_scalar) >>. |
114 | This function call is functionally identical to: |
107 | |
115 | |
|
|
116 | $json_text = JSON::XS->new->utf8->encode ($perl_scalar) |
|
|
117 | |
|
|
118 | except being faster. |
|
|
119 | |
108 | =item $perl_scalar = from_json $json_string |
120 | =item $perl_scalar = from_json $json_text |
109 | |
121 | |
110 | The opposite of C<to_json>: expects an UTF-8 (binary) string and tries to |
122 | 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 |
123 | parse that as an UTF-8 encoded JSON text, returning the resulting simple |
112 | scalar or reference. Croaks on error. |
124 | scalar or reference. Croaks on error. |
113 | |
125 | |
114 | This function call is functionally identical to C<< JSON::XS->new->utf8->decode ($json_string) >>. |
126 | This function call is functionally identical to: |
|
|
127 | |
|
|
128 | $perl_scalar = JSON::XS->new->utf8->decode ($json_text) |
|
|
129 | |
|
|
130 | except being faster. |
115 | |
131 | |
116 | =back |
132 | =back |
|
|
133 | |
117 | |
134 | |
118 | =head1 OBJECT-ORIENTED INTERFACE |
135 | =head1 OBJECT-ORIENTED INTERFACE |
119 | |
136 | |
120 | The object oriented interface lets you configure your own encoding or |
137 | The object oriented interface lets you configure your own encoding or |
121 | decoding style, within the limits of supported formats. |
138 | decoding style, within the limits of supported formats. |
… | |
… | |
128 | strings. All boolean flags described below are by default I<disabled>. |
145 | strings. All boolean flags described below are by default I<disabled>. |
129 | |
146 | |
130 | The mutators for flags all return the JSON object again and thus calls can |
147 | The mutators for flags all return the JSON object again and thus calls can |
131 | be chained: |
148 | be chained: |
132 | |
149 | |
133 | my $json = JSON::XS->new->utf8(1)->space_after(1)->encode ({a => [1,2]}) |
150 | my $json = JSON::XS->new->utf8->space_after->encode ({a => [1,2]}) |
134 | => {"a": [1, 2]} |
151 | => {"a": [1, 2]} |
135 | |
152 | |
136 | =item $json = $json->ascii ([$enable]) |
153 | =item $json = $json->ascii ([$enable]) |
137 | |
154 | |
138 | If C<$enable> is true (or missing), then the C<encode> method will |
155 | 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 |
156 | 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 |
157 | unicode characters outside that range will be escaped using either a |
141 | \uXXXX (BMP characters) or a double \uHHHH\uLLLLL escape sequence, as per |
158 | single \uXXXX (BMP characters) or a double \uHHHH\uLLLLL escape sequence, |
142 | RFC4627. |
159 | as per RFC4627. |
143 | |
160 | |
144 | If C<$enable> is false, then the C<encode> method will not escape Unicode |
161 | If C<$enable> is false, then the C<encode> method will not escape Unicode |
145 | characters unless necessary. |
162 | characters unless required by the JSON syntax. This results in a faster |
|
|
163 | and more compact format. |
146 | |
164 | |
147 | JSON::XS->new->ascii (1)->encode (chr 0x10401) |
165 | JSON::XS->new->ascii (1)->encode ([chr 0x10401]) |
148 | => \ud801\udc01 |
166 | => ["\ud801\udc01"] |
149 | |
167 | |
150 | =item $json = $json->utf8 ([$enable]) |
168 | =item $json = $json->utf8 ([$enable]) |
151 | |
169 | |
152 | If C<$enable> is true (or missing), then the C<encode> method will encode |
170 | 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 |
171 | 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 |
172 | 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 |
173 | 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. |
174 | range C<0..255>, they are thus useful for bytewise/binary I/O. In future |
|
|
175 | versions, enabling this option might enable autodetection of the UTF-16 |
|
|
176 | and UTF-32 encoding families, as described in RFC4627. |
157 | |
177 | |
158 | If C<$enable> is false, then the C<encode> method will return the JSON |
178 | 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 |
179 | 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 |
180 | 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. |
181 | to be done yourself, e.g. using the Encode module. |
162 | |
182 | |
163 | Example, output UTF-16-encoded JSON: |
183 | Example, output UTF-16BE-encoded JSON: |
|
|
184 | |
|
|
185 | use Encode; |
|
|
186 | $jsontext = encode "UTF-16BE", JSON::XS->new->encode ($object); |
|
|
187 | |
|
|
188 | Example, decode UTF-32LE-encoded JSON: |
|
|
189 | |
|
|
190 | use Encode; |
|
|
191 | $object = JSON::XS->new->decode (decode "UTF-32LE", $jsontext); |
164 | |
192 | |
165 | =item $json = $json->pretty ([$enable]) |
193 | =item $json = $json->pretty ([$enable]) |
166 | |
194 | |
167 | This enables (or disables) all of the C<indent>, C<space_before> and |
195 | 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 |
196 | 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 |
212 | 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 |
213 | format as output, putting every array member or object/hash key-value pair |
186 | into its own line, identing them properly. |
214 | into its own line, identing them properly. |
187 | |
215 | |
188 | If C<$enable> is false, no newlines or indenting will be produced, and the |
216 | 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>. |
217 | resulting JSON text is guarenteed not to contain any C<newlines>. |
190 | |
218 | |
191 | This setting has no effect when decoding JSON strings. |
219 | This setting has no effect when decoding JSON texts. |
192 | |
220 | |
193 | =item $json = $json->space_before ([$enable]) |
221 | =item $json = $json->space_before ([$enable]) |
194 | |
222 | |
195 | If C<$enable> is true (or missing), then the C<encode> method will add an extra |
223 | 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. |
224 | optional space before the C<:> separating keys from values in JSON objects. |
197 | |
225 | |
198 | If C<$enable> is false, then the C<encode> method will not add any extra |
226 | If C<$enable> is false, then the C<encode> method will not add any extra |
199 | space at those places. |
227 | space at those places. |
200 | |
228 | |
201 | This setting has no effect when decoding JSON strings. You will also most |
229 | This setting has no effect when decoding JSON texts. You will also |
202 | likely combine this setting with C<space_after>. |
230 | most likely combine this setting with C<space_after>. |
203 | |
231 | |
204 | Example, space_before enabled, space_after and indent disabled: |
232 | Example, space_before enabled, space_after and indent disabled: |
205 | |
233 | |
206 | {"key" :"value"} |
234 | {"key" :"value"} |
207 | |
235 | |
… | |
… | |
213 | members. |
241 | members. |
214 | |
242 | |
215 | If C<$enable> is false, then the C<encode> method will not add any extra |
243 | If C<$enable> is false, then the C<encode> method will not add any extra |
216 | space at those places. |
244 | space at those places. |
217 | |
245 | |
218 | This setting has no effect when decoding JSON strings. |
246 | This setting has no effect when decoding JSON texts. |
219 | |
247 | |
220 | Example, space_before and indent disabled, space_after enabled: |
248 | Example, space_before and indent disabled, space_after enabled: |
221 | |
249 | |
222 | {"key": "value"} |
250 | {"key": "value"} |
223 | |
251 | |
… | |
… | |
229 | If C<$enable> is false, then the C<encode> method will output key-value |
257 | 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 |
258 | pairs in the order Perl stores them (which will likely change between runs |
231 | of the same script). |
259 | of the same script). |
232 | |
260 | |
233 | This option is useful if you want the same data structure to be encoded as |
261 | 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, |
262 | 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, |
263 | the same hash migh be encoded differently even if contains the same data, |
236 | as key-value pairs have no inherent ordering in Perl. |
264 | as key-value pairs have no inherent ordering in Perl. |
237 | |
265 | |
238 | This setting has no effect when decoding JSON strings. |
266 | This setting has no effect when decoding JSON texts. |
239 | |
267 | |
240 | =item $json = $json->allow_nonref ([$enable]) |
268 | =item $json = $json->allow_nonref ([$enable]) |
241 | |
269 | |
242 | If C<$enable> is true (or missing), then the C<encode> method can convert a |
270 | 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, |
271 | 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 |
272 | which is an extension to RFC4627. Likewise, C<decode> will accept those JSON |
245 | values instead of croaking. |
273 | values instead of croaking. |
246 | |
274 | |
247 | If C<$enable> is false, then the C<encode> method will croak if it isn't |
275 | 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 |
276 | 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 |
277 | or array. Likewise, C<decode> will croak if given something that is not a |
250 | JSON object or array. |
278 | JSON object or array. |
251 | |
279 | |
252 | Example, encode a Perl scalar as JSON value with enabled C<allow_nonref>, |
280 | Example, encode a Perl scalar as JSON value with enabled C<allow_nonref>, |
253 | resulting in an invalid JSON text: |
281 | resulting in an invalid JSON text: |
… | |
… | |
258 | =item $json = $json->shrink ([$enable]) |
286 | =item $json = $json->shrink ([$enable]) |
259 | |
287 | |
260 | Perl usually over-allocates memory a bit when allocating space for |
288 | Perl usually over-allocates memory a bit when allocating space for |
261 | strings. This flag optionally resizes strings generated by either |
289 | strings. This flag optionally resizes strings generated by either |
262 | C<encode> or C<decode> to their minimum size possible. This can save |
290 | 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 |
291 | 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 |
292 | 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 |
293 | 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 |
294 | UTF-X or in octet-form. The latter cannot store everything but uses less |
267 | space in general. |
295 | space in general. |
268 | |
296 | |
… | |
… | |
274 | |
302 | |
275 | In the future, this setting might control other things, such as converting |
303 | In the future, this setting might control other things, such as converting |
276 | strings that look like integers or floats into integers or floats |
304 | strings that look like integers or floats into integers or floats |
277 | internally (there is no difference on the Perl level), saving space. |
305 | internally (there is no difference on the Perl level), saving space. |
278 | |
306 | |
|
|
307 | =item $json = $json->max_depth ([$maximum_nesting_depth]) |
|
|
308 | |
|
|
309 | Sets the maximum nesting level (default C<8192>) accepted while encoding |
|
|
310 | or decoding. If the JSON text or Perl data structure has an equal or |
|
|
311 | higher nesting level then this limit, then the encoder and decoder will |
|
|
312 | stop and croak at that point. |
|
|
313 | |
|
|
314 | Nesting level is defined by number of hash- or arrayrefs that the encoder |
|
|
315 | needs to traverse to reach a given point or the number of C<{> or C<[> |
|
|
316 | characters without their matching closing parenthesis crossed to reach a |
|
|
317 | given character in a string. |
|
|
318 | |
|
|
319 | Setting the maximum depth to one disallows any nesting, so that ensures |
|
|
320 | that the object is only a single hash/object or array. |
|
|
321 | |
|
|
322 | The argument to C<max_depth> will be rounded up to the next nearest power |
|
|
323 | of two. |
|
|
324 | |
|
|
325 | See SECURITY CONSIDERATIONS, below, for more info on why this is useful. |
|
|
326 | |
279 | =item $json_string = $json->encode ($perl_scalar) |
327 | =item $json_text = $json->encode ($perl_scalar) |
280 | |
328 | |
281 | Converts the given Perl data structure (a simple scalar or a reference |
329 | 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 |
330 | 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 |
331 | converted into JSON string or number sequences, while references to arrays |
284 | become JSON arrays and references to hashes become JSON objects. Undefined |
332 | 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> |
333 | Perl values (e.g. C<undef>) become JSON C<null> values. Neither C<true> |
286 | nor C<false> values will be generated. |
334 | nor C<false> values will be generated. |
287 | |
335 | |
288 | =item $perl_scalar = $json->decode ($json_string) |
336 | =item $perl_scalar = $json->decode ($json_text) |
289 | |
337 | |
290 | The opposite of C<encode>: expects a JSON string and tries to parse it, |
338 | 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. |
339 | returning the resulting simple scalar or reference. Croaks on error. |
292 | |
340 | |
293 | JSON numbers and strings become simple Perl scalars. JSON arrays become |
341 | JSON numbers and strings become simple Perl scalars. JSON arrays become |
294 | Perl arrayrefs and JSON objects become Perl hashrefs. C<true> becomes |
342 | 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>. |
343 | C<1>, C<false> becomes C<0> and C<null> becomes C<undef>. |
296 | |
344 | |
297 | =back |
345 | =back |
|
|
346 | |
298 | |
347 | |
299 | =head1 MAPPING |
348 | =head1 MAPPING |
300 | |
349 | |
301 | This section describes how JSON::XS maps Perl values to JSON values and |
350 | This section describes how JSON::XS maps Perl values to JSON values and |
302 | vice versa. These mappings are designed to "do the right thing" in most |
351 | vice versa. These mappings are designed to "do the right thing" in most |
… | |
… | |
414 | |
463 | |
415 | Those will be encoded until memory or stackspace runs out. |
464 | Those will be encoded until memory or stackspace runs out. |
416 | |
465 | |
417 | =back |
466 | =back |
418 | |
467 | |
|
|
468 | |
419 | =head1 COMPARISON |
469 | =head1 COMPARISON |
420 | |
470 | |
421 | As already mentioned, this module was created because none of the existing |
471 | As already mentioned, this module was created because none of the existing |
422 | JSON modules could be made to work correctly. First I will describe the |
472 | JSON modules could be made to work correctly. First I will describe the |
423 | problems (or pleasures) I encountered with various existing JSON modules, |
473 | problems (or pleasures) I encountered with various existing JSON modules, |
… | |
… | |
448 | |
498 | |
449 | Has problems handling many Perl values (e.g. regex results and other magic |
499 | Has problems handling many Perl values (e.g. regex results and other magic |
450 | values will make it croak). |
500 | values will make it croak). |
451 | |
501 | |
452 | Does not even generate valid JSON (C<{1,2}> gets converted to C<{1:2}> |
502 | Does not even generate valid JSON (C<{1,2}> gets converted to C<{1:2}> |
453 | which is not a valid JSON string. |
503 | which is not a valid JSON text. |
454 | |
504 | |
455 | Unmaintained (maintainer unresponsive for many months, bugs are not |
505 | Unmaintained (maintainer unresponsive for many months, bugs are not |
456 | getting fixed). |
506 | getting fixed). |
457 | |
507 | |
458 | =item JSON::Syck 0.21 |
508 | =item JSON::Syck 0.21 |
… | |
… | |
460 | Very buggy (often crashes). |
510 | Very buggy (often crashes). |
461 | |
511 | |
462 | Very inflexible (no human-readable format supported, format pretty much |
512 | 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 |
513 | 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 |
514 | single-line compact format for use in a protocol, and preferably a way to |
465 | generate ASCII-only JSON strings). |
515 | generate ASCII-only JSON texts). |
466 | |
516 | |
467 | Completely broken (and confusingly documented) Unicode handling (unicode |
517 | Completely broken (and confusingly documented) Unicode handling (unicode |
468 | escapes are not working properly, you need to set ImplicitUnicode to |
518 | escapes are not working properly, you need to set ImplicitUnicode to |
469 | I<different> values on en- and decoding to get symmetric behaviour). |
519 | I<different> values on en- and decoding to get symmetric behaviour). |
470 | |
520 | |
… | |
… | |
493 | |
543 | |
494 | Very inflexible. |
544 | Very inflexible. |
495 | |
545 | |
496 | No roundtripping. |
546 | No roundtripping. |
497 | |
547 | |
498 | Does not generate valid JSON (key strings are often unquoted, empty keys |
548 | Does not generate valid JSON texts (key strings are often unquoted, empty keys |
499 | result in nothing being output) |
549 | result in nothing being output) |
500 | |
550 | |
501 | Does not check input for validity. |
551 | Does not check input for validity. |
502 | |
552 | |
503 | =back |
553 | =back |
… | |
… | |
508 | tables. They have been generated with the help of the C<eg/bench> program |
558 | 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 |
559 | in the JSON::XS distribution, to make it easy to compare on your own |
510 | system. |
560 | system. |
511 | |
561 | |
512 | First comes a comparison between various modules using a very short JSON |
562 | First comes a comparison between various modules using a very short JSON |
513 | string (83 bytes), showing the number of encodes/decodes per second |
563 | string: |
514 | (JSON::XS is the functional interface, while JSON::XS/2 is the OO |
564 | |
|
|
565 | {"method": "handleMessage", "params": ["user1", "we were just talking"], "id": null} |
|
|
566 | |
|
|
567 | It shows the number of encodes/decodes per second (JSON::XS uses the |
|
|
568 | functional interface, while JSON::XS/2 uses the OO interface with |
515 | interface with pretty-printing and hashkey sorting enabled). Higher is |
569 | pretty-printing and hashkey sorting enabled). Higher is better: |
516 | better: |
|
|
517 | |
570 | |
518 | module | encode | decode | |
571 | module | encode | decode | |
519 | -----------|------------|------------| |
572 | -----------|------------|------------| |
520 | JSON | 14006 | 6820 | |
573 | JSON | 11488.516 | 7823.035 | |
521 | JSON::DWIW | 200937 | 120386 | |
574 | JSON::DWIW | 94708.054 | 129094.260 | |
522 | JSON::PC | 85065 | 129366 | |
575 | JSON::PC | 63884.157 | 128528.212 | |
523 | JSON::Syck | 59898 | 44232 | |
576 | JSON::Syck | 34898.677 | 42096.911 | |
524 | JSON::XS | 1171478 | 342435 | |
577 | JSON::XS | 654027.064 | 396423.669 | |
525 | JSON::XS/2 | 730760 | 328714 | |
578 | JSON::XS/2 | 371564.190 | 371725.613 | |
526 | -----------+------------+------------+ |
579 | -----------+------------+------------+ |
527 | |
580 | |
528 | That is, JSON::XS is 6 times faster than than JSON::DWIW and about 80 |
581 | That is, JSON::XS is more than six times faster than JSON::DWIW on |
|
|
582 | encoding, more than three times faster on decoding, and about thirty times |
529 | times faster than JSON, even with pretty-printing and key sorting. |
583 | faster than JSON, even with pretty-printing and key sorting. |
530 | |
584 | |
531 | Using a longer test string (roughly 18KB, generated from Yahoo! Locals |
585 | Using a longer test string (roughly 18KB, generated from Yahoo! Locals |
532 | search API (http://nanoref.com/yahooapis/mgPdGg): |
586 | search API (http://nanoref.com/yahooapis/mgPdGg): |
533 | |
587 | |
534 | module | encode | decode | |
588 | module | encode | decode | |
535 | -----------|------------|------------| |
589 | -----------|------------|------------| |
536 | JSON | 673 | 38 | |
590 | JSON | 273.023 | 44.674 | |
537 | JSON::DWIW | 5271 | 770 | |
591 | JSON::DWIW | 1089.383 | 1145.704 | |
538 | JSON::PC | 9901 | 2491 | |
592 | JSON::PC | 3097.419 | 2393.921 | |
539 | JSON::Syck | 2360 | 786 | |
593 | JSON::Syck | 514.060 | 843.053 | |
540 | JSON::XS | 37398 | 3202 | |
594 | JSON::XS | 6479.668 | 3636.364 | |
541 | JSON::XS/2 | 13765 | 3153 | |
595 | JSON::XS/2 | 3774.221 | 3599.124 | |
542 | -----------+------------+------------+ |
596 | -----------+------------+------------+ |
543 | |
597 | |
544 | Again, JSON::XS leads by far in the encoding case, while still beating |
598 | Again, JSON::XS leads by far. |
545 | every other module in the decoding case. |
|
|
546 | |
599 | |
547 | On large strings containing lots of unicode characters, some modules |
600 | On large strings containing lots of high unicode characters, some modules |
548 | (such as JSON::PC) decode faster than JSON::XS, but the result will be |
601 | (such as JSON::PC) seem to decode faster than JSON::XS, but the result |
549 | broken due to missing unicode handling. Others refuse to decode or encode |
602 | will be broken due to missing (or wrong) unicode handling. Others refuse |
550 | properly, so it was impossible to prepare a fair comparison table for that |
603 | to decode or encode properly, so it was impossible to prepare a fair |
551 | case. |
604 | comparison table for that case. |
552 | |
605 | |
553 | =head1 RESOURCE LIMITS |
|
|
554 | |
606 | |
555 | JSON::XS does not impose any limits on the size of JSON texts or Perl |
607 | =head1 SECURITY CONSIDERATIONS |
556 | values they represent - if your machine can handle it, JSON::XS will |
608 | |
557 | encode or decode it. Future versions might optionally impose structure |
609 | When you are using JSON in a protocol, talking to untrusted potentially |
558 | depth and memory use resource limits. |
610 | hostile creatures requires relatively few measures. |
|
|
611 | |
|
|
612 | First of all, your JSON decoder should be secure, that is, should not have |
|
|
613 | any buffer overflows. Obviously, this module should ensure that and I am |
|
|
614 | trying hard on making that true, but you never know. |
|
|
615 | |
|
|
616 | Second, you need to avoid resource-starving attacks. That means you should |
|
|
617 | limit the size of JSON texts you accept, or make sure then when your |
|
|
618 | resources run out, thats just fine (e.g. by using a separate process that |
|
|
619 | can crash safely). The size of a JSON text in octets or characters is |
|
|
620 | usually a good indication of the size of the resources required to decode |
|
|
621 | it into a Perl structure. |
|
|
622 | |
|
|
623 | Third, JSON::XS recurses using the C stack when decoding objects and |
|
|
624 | arrays. The C stack is a limited resource: for instance, on my amd64 |
|
|
625 | machine with 8MB of stack size I can decode around 180k nested arrays |
|
|
626 | but only 14k nested JSON objects. If that is exceeded, the program |
|
|
627 | crashes. Thats why the default nesting limit is set to 8192. If your |
|
|
628 | process has a smaller stack, you should adjust this setting accordingly |
|
|
629 | with the C<max_depth> method. |
|
|
630 | |
|
|
631 | And last but least, something else could bomb you that I forgot to think |
|
|
632 | of. In that case, you get to keep the pieces. I am alway sopen for hints, |
|
|
633 | though... |
|
|
634 | |
559 | |
635 | |
560 | =head1 BUGS |
636 | =head1 BUGS |
561 | |
637 | |
562 | While the goal of this module is to be correct, that unfortunately does |
638 | While the goal of this module is to be correct, that unfortunately does |
563 | not mean its bug-free, only that I think its design is bug-free. It is |
639 | not mean its bug-free, only that I think its design is bug-free. It is |
564 | still very young and not well-tested. If you keep reporting bugs they will |
640 | still relatively early in its development. If you keep reporting bugs they |
565 | be fixed swiftly, though. |
641 | will be fixed swiftly, though. |
566 | |
642 | |
567 | =cut |
643 | =cut |
568 | |
644 | |
569 | 1; |
645 | 1; |
570 | |
646 | |