… | |
… | |
111 | strings. All boolean flags described below are by default I<disabled>. |
111 | strings. All boolean flags described below are by default I<disabled>. |
112 | |
112 | |
113 | The mutators for flags all return the JSON object again and thus calls can |
113 | The mutators for flags all return the JSON object again and thus calls can |
114 | be chained: |
114 | be chained: |
115 | |
115 | |
116 | my $json = JSON::XS->new->utf8(1)->pretty(1)->encode ({a => [1,2]}) |
116 | my $json = JSON::XS->new->utf8(1)->space_after(1)->encode ({a => [1,2]}) |
117 | => {"a" : [1, 2]} |
117 | => {"a": [1, 2]} |
118 | |
118 | |
119 | =item $json = $json->ascii ($enable) |
119 | =item $json = $json->ascii ($enable) |
120 | |
120 | |
121 | If C<$enable> is true, then the C<encode> method will not generate |
121 | If C<$enable> is true, then the C<encode> method will not generate |
122 | characters outside the code range C<0..127>. Any unicode characters |
122 | characters outside the code range C<0..127>. Any unicode characters |
123 | outside that range will be escaped using either a single \uXXXX (BMP |
123 | outside that range will be escaped using either a single \uXXXX (BMP |
124 | characters) or a double \uHHHH\uLLLLL escape sequence, as per RFC4627. |
124 | characters) or a double \uHHHH\uLLLLL escape sequence, as per RFC4627. |
125 | |
125 | |
126 | If C<$enable> is false, then the C<encode> method will not escape Unicode |
126 | If C<$enable> is false, then the C<encode> method will not escape Unicode |
127 | characters unless necessary. |
127 | characters unless necessary. |
|
|
128 | |
|
|
129 | JSON::XS->new->ascii (1)->encode (chr 0x10401) |
|
|
130 | => \ud801\udc01 |
128 | |
131 | |
129 | =item $json = $json->utf8 ($enable) |
132 | =item $json = $json->utf8 ($enable) |
130 | |
133 | |
131 | If C<$enable> is true, then the C<encode> method will encode the JSON |
134 | If C<$enable> is true, then the C<encode> method will encode the JSON |
132 | string into UTF-8, as required by many protocols, while the C<decode> |
135 | string into UTF-8, as required by many protocols, while the C<decode> |
… | |
… | |
137 | If C<$enable> is false, then the C<encode> method will return the JSON |
140 | If C<$enable> is false, then the C<encode> method will return the JSON |
138 | string as a (non-encoded) unicode string, while C<decode> expects thus a |
141 | string as a (non-encoded) unicode string, while C<decode> expects thus a |
139 | unicode string. Any decoding or encoding (e.g. to UTF-8 or UTF-16) needs |
142 | unicode string. Any decoding or encoding (e.g. to UTF-8 or UTF-16) needs |
140 | to be done yourself, e.g. using the Encode module. |
143 | to be done yourself, e.g. using the Encode module. |
141 | |
144 | |
142 | =item $json = $json->pretty ($enabla) |
145 | =item $json = $json->pretty ($enable) |
143 | |
146 | |
144 | This enables (or disables) all of the C<indent>, C<space_before> and |
147 | This enables (or disables) all of the C<indent>, C<space_before> and |
145 | C<space_after> (and in the future possibly more) settings in one call to |
148 | C<space_after> (and in the future possibly more) flags in one call to |
146 | generate the most readable (or most compact) form possible. |
149 | generate the most readable (or most compact) form possible. |
|
|
150 | |
|
|
151 | my $json = JSON::XS->new->pretty(1)->encode ({a => [1,2]}) |
|
|
152 | => |
|
|
153 | { |
|
|
154 | "a" : [ |
|
|
155 | 1, |
|
|
156 | 2 |
|
|
157 | ] |
|
|
158 | } |
147 | |
159 | |
148 | =item $json = $json->indent ($enable) |
160 | =item $json = $json->indent ($enable) |
149 | |
161 | |
150 | If C<$enable> is true, then the C<encode> method will use a multiline |
162 | If C<$enable> is true, then the C<encode> method will use a multiline |
151 | format as output, putting every array member or object/hash key-value pair |
163 | format as output, putting every array member or object/hash key-value pair |
… | |
… | |
192 | the same JSON string (given the same overall settings). If it is disabled, |
204 | the same JSON string (given the same overall settings). If it is disabled, |
193 | the same hash migh be encoded differently even if contains the same data, |
205 | the same hash migh be encoded differently even if contains the same data, |
194 | as key-value pairs have no inherent ordering in Perl. |
206 | as key-value pairs have no inherent ordering in Perl. |
195 | |
207 | |
196 | This setting has no effect when decoding JSON strings. |
208 | This setting has no effect when decoding JSON strings. |
|
|
209 | |
|
|
210 | =item $json = $json->allow_nonref ($enable) |
|
|
211 | |
|
|
212 | If C<$enable> is true, then the C<encode> method can convert a |
|
|
213 | non-reference into its corresponding string, number or null JSON value, |
|
|
214 | which is an extension to RFC4627. Likewise, C<decode> will accept those JSON |
|
|
215 | values instead of croaking. |
|
|
216 | |
|
|
217 | If C<$enable> is false, then the C<encode> method will croak if it isn't |
|
|
218 | passed an arrayref or hashref, as JSON strings must either be an object |
|
|
219 | or array. Likewise, C<decode> will croak if given something that is not a |
|
|
220 | JSON object or array. |
197 | |
221 | |
198 | =item $json_string = $json->encode ($perl_scalar) |
222 | =item $json_string = $json->encode ($perl_scalar) |
199 | |
223 | |
200 | Converts the given Perl data structure (a simple scalar or a reference |
224 | Converts the given Perl data structure (a simple scalar or a reference |
201 | to a hash or array) to its JSON representation. Simple scalars will be |
225 | to a hash or array) to its JSON representation. Simple scalars will be |
… | |
… | |
213 | Perl arrayrefs and JSON objects become Perl hashrefs. C<true> becomes |
237 | Perl arrayrefs and JSON objects become Perl hashrefs. C<true> becomes |
214 | C<1>, C<false> becomes C<0> and C<null> becomes C<undef>. |
238 | C<1>, C<false> becomes C<0> and C<null> becomes C<undef>. |
215 | |
239 | |
216 | =back |
240 | =back |
217 | |
241 | |
|
|
242 | =head1 COMPARISON |
|
|
243 | |
|
|
244 | As already mentioned, this module was created because none of the existing |
|
|
245 | JSON modules could be made to work correctly. First I will describe the |
|
|
246 | problems (or pleasures) I encountered with various existing JSON modules, |
|
|
247 | followed by some benchmark values. JSON::XS was designed not to suffer |
|
|
248 | from any of these problems or limitations. |
|
|
249 | |
|
|
250 | =over 4 |
|
|
251 | |
|
|
252 | =item JSON |
|
|
253 | |
|
|
254 | Slow (but very portable, as it is written in pure Perl). |
|
|
255 | |
|
|
256 | Undocumented/buggy Unicode handling (how JSON handles unicode values is |
|
|
257 | undocumented. One can get far by feeding it unicode strings and doing |
|
|
258 | en-/decoding oneself, but unicode escapes are not working properly). |
|
|
259 | |
|
|
260 | No roundtripping (strings get clobbered if they look like numbers, e.g. |
|
|
261 | the string C<2.0> will encode to C<2.0> instead of C<"2.0">, and that will |
|
|
262 | decode into the number 2. |
|
|
263 | |
|
|
264 | =item JSON::PC |
|
|
265 | |
|
|
266 | Very fast. |
|
|
267 | |
|
|
268 | Undocumented/buggy Unicode handling. |
|
|
269 | |
|
|
270 | No roundtripping. |
|
|
271 | |
|
|
272 | Has problems handling many Perl values (e.g. regex results and other magic |
|
|
273 | values will make it croak). |
|
|
274 | |
|
|
275 | Does not even generate valid JSON (C<{1,2}> gets converted to C<{1:2}> |
|
|
276 | which is not a valid JSON string. |
|
|
277 | |
|
|
278 | Unmaintained (maintainer unresponsive for many months, bugs are not |
|
|
279 | getting fixed). |
|
|
280 | |
|
|
281 | =item JSON::Syck |
|
|
282 | |
|
|
283 | Very buggy (often crashes). |
|
|
284 | |
|
|
285 | Very inflexible (no human-readable format supported, format pretty much |
|
|
286 | undocumented. I need at least a format for easy reading by humans and a |
|
|
287 | single-line compact format for use in a protocol, and preferably a way to |
|
|
288 | generate ASCII-only JSON strings). |
|
|
289 | |
|
|
290 | Completely broken (and confusingly documented) Unicode handling (unicode |
|
|
291 | escapes are not working properly, you need to set ImplicitUnicode to |
|
|
292 | I<different> values on en- and decoding to get symmetric behaviour). |
|
|
293 | |
|
|
294 | No roundtripping (simple cases work, but this depends on wether the scalar |
|
|
295 | value was used in a numeric context or not). |
|
|
296 | |
|
|
297 | Dumping hashes may skip hash values depending on iterator state. |
|
|
298 | |
|
|
299 | Unmaintained (maintainer unresponsive for many months, bugs are not |
|
|
300 | getting fixed). |
|
|
301 | |
|
|
302 | Does not check input for validity (i.e. will accept non-JSON input and |
|
|
303 | return "something" instead of raising an exception. This is a security |
|
|
304 | issue: imagine two banks transfering money between each other using |
|
|
305 | JSON. One bank might parse a given non-JSON request and deduct money, |
|
|
306 | while the other might reject the transaction with a syntax error. While a |
|
|
307 | good protocol will at least recover, that is extra unnecessary work and |
|
|
308 | the transaction will still not succeed). |
|
|
309 | |
|
|
310 | =item JSON::DWIW |
|
|
311 | |
|
|
312 | Very fast. Very natural. Very nice. |
|
|
313 | |
|
|
314 | Undocumented unicode handling (but the best of the pack. Unicode escapes |
|
|
315 | still don't get parsed properly). |
|
|
316 | |
|
|
317 | Very inflexible. |
|
|
318 | |
|
|
319 | No roundtripping. |
|
|
320 | |
|
|
321 | Does not generate valid JSON (key strings are often unquoted, empty keys |
|
|
322 | result in nothing being output) |
|
|
323 | |
|
|
324 | Does not check input for validity. |
|
|
325 | |
|
|
326 | =back |
|
|
327 | |
|
|
328 | =head2 SPEED |
|
|
329 | |
|
|
330 | It seems that JSON::XS is surprisingly fast, as shown in the following |
|
|
331 | tables. They have been generated with the help of the C<eg/bench> program |
|
|
332 | in the JSON::XS distribution, to make it easy to compare on your own |
|
|
333 | system. |
|
|
334 | |
|
|
335 | First is a comparison between various modules using a very simple JSON |
|
|
336 | string, showing the number of encodes/decodes per second (JSON::XS is |
|
|
337 | the functional interface, while JSON::XS/2 is the OO interface with |
|
|
338 | pretty-printing and hashkey sorting enabled). |
|
|
339 | |
|
|
340 | module | encode | decode | |
|
|
341 | -----------|------------|------------| |
|
|
342 | JSON | 14006 | 6820 | |
|
|
343 | JSON::DWIW | 200937 | 120386 | |
|
|
344 | JSON::PC | 85065 | 129366 | |
|
|
345 | JSON::Syck | 59898 | 44232 | |
|
|
346 | JSON::XS | 1171478 | 342435 | |
|
|
347 | JSON::XS/2 | 730760 | 328714 | |
|
|
348 | -----------+------------+------------+ |
|
|
349 | |
|
|
350 | That is, JSON::XS is 6 times faster than than JSON::DWIW and about 80 |
|
|
351 | times faster than JSON, even with pretty-printing and key sorting. |
|
|
352 | |
|
|
353 | Using a longer test string (roughly 8KB, generated from Yahoo! Locals |
|
|
354 | search API (http://nanoref.com/yahooapis/mgPdGg): |
|
|
355 | |
|
|
356 | module | encode | decode | |
|
|
357 | -----------|------------|------------| |
|
|
358 | JSON | 673 | 38 | |
|
|
359 | JSON::DWIW | 5271 | 770 | |
|
|
360 | JSON::PC | 9901 | 2491 | |
|
|
361 | JSON::Syck | 2360 | 786 | |
|
|
362 | JSON::XS | 37398 | 3202 | |
|
|
363 | JSON::XS/2 | 13765 | 3153 | |
|
|
364 | -----------+------------+------------+ |
|
|
365 | |
|
|
366 | Again, JSON::XS leads by far in the encoding case, while still beating |
|
|
367 | every other module in the decoding case. |
|
|
368 | |
|
|
369 | Last example is an almost 8MB large hash with many large binary values |
|
|
370 | (PNG files), resulting in a lot of escaping: |
|
|
371 | |
|
|
372 | =head1 BUGS |
|
|
373 | |
|
|
374 | While the goal of this module is to be correct, that unfortunately does |
|
|
375 | not mean its bug-free, only that I think its design is bug-free. It is |
|
|
376 | still very young and not well-tested. If you keep reporting bugs they will |
|
|
377 | be fixed swiftly, though. |
|
|
378 | |
218 | =cut |
379 | =cut |
219 | |
380 | |
220 | 1; |
381 | 1; |
221 | |
382 | |
222 | =head1 AUTHOR |
383 | =head1 AUTHOR |