… | |
… | |
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 | |
… | |
… | |
312 | =over 4 |
312 | =over 4 |
313 | |
313 | |
314 | =item object |
314 | =item object |
315 | |
315 | |
316 | A JSON object becomes a reference to a hash in Perl. No ordering of object |
316 | A JSON object becomes a reference to a hash in Perl. No ordering of object |
317 | keys is preserved. |
317 | keys is preserved (JSON does not preserver object key ordering itself). |
318 | |
318 | |
319 | =item array |
319 | =item array |
320 | |
320 | |
321 | A JSON array becomes a reference to an array in Perl. |
321 | A JSON array becomes a reference to an array in Perl. |
322 | |
322 | |
… | |
… | |
358 | =item hash references |
358 | =item hash references |
359 | |
359 | |
360 | Perl hash references become JSON objects. As there is no inherent ordering |
360 | 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 |
361 | 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 |
362 | 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 |
363 | 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 |
364 | 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 |
365 | will serialise to the same JSON text (given same settings and version of |
366 | JSON::XS), but this incurs a runtime overhead. |
366 | JSON::XS), but this incurs a runtime overhead. |
367 | |
367 | |
368 | =item array references |
368 | =item array references |
… | |
… | |
507 | It seems that JSON::XS is surprisingly fast, as shown in the following |
507 | 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 |
508 | 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 |
509 | in the JSON::XS distribution, to make it easy to compare on your own |
510 | system. |
510 | system. |
511 | |
511 | |
512 | First is a comparison between various modules using a very simple JSON |
512 | 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 |
513 | string (83 bytes), showing the number of encodes/decodes per second |
514 | the functional interface, while JSON::XS/2 is the OO interface with |
514 | (JSON::XS is the functional interface, while JSON::XS/2 is the OO |
515 | pretty-printing and hashkey sorting enabled). |
515 | interface with pretty-printing and hashkey sorting enabled). Higher is |
|
|
516 | better: |
516 | |
517 | |
517 | module | encode | decode | |
518 | module | encode | decode | |
518 | -----------|------------|------------| |
519 | -----------|------------|------------| |
519 | JSON | 14006 | 6820 | |
520 | JSON | 14006 | 6820 | |
520 | JSON::DWIW | 200937 | 120386 | |
521 | JSON::DWIW | 200937 | 120386 | |
… | |
… | |
525 | -----------+------------+------------+ |
526 | -----------+------------+------------+ |
526 | |
527 | |
527 | That is, JSON::XS is 6 times faster than than JSON::DWIW and about 80 |
528 | 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. |
529 | times faster than JSON, even with pretty-printing and key sorting. |
529 | |
530 | |
530 | Using a longer test string (roughly 8KB, generated from Yahoo! Locals |
531 | Using a longer test string (roughly 18KB, generated from Yahoo! Locals |
531 | search API (http://nanoref.com/yahooapis/mgPdGg): |
532 | search API (http://nanoref.com/yahooapis/mgPdGg): |
532 | |
533 | |
533 | module | encode | decode | |
534 | module | encode | decode | |
534 | -----------|------------|------------| |
535 | -----------|------------|------------| |
535 | JSON | 673 | 38 | |
536 | JSON | 673 | 38 | |
… | |
… | |
541 | -----------+------------+------------+ |
542 | -----------+------------+------------+ |
542 | |
543 | |
543 | Again, JSON::XS leads by far in the encoding case, while still beating |
544 | Again, JSON::XS leads by far in the encoding case, while still beating |
544 | every other module in the decoding case. |
545 | every other module in the decoding case. |
545 | |
546 | |
|
|
547 | On large strings containing lots of unicode characters, some modules |
|
|
548 | (such as JSON::PC) decode faster than JSON::XS, but the result will be |
|
|
549 | broken due to missing unicode handling. Others refuse to decode or encode |
|
|
550 | properly, so it was impossible to prepare a fair comparison table for that |
|
|
551 | case. |
|
|
552 | |
546 | =head1 RESOURCE LIMITS |
553 | =head1 RESOURCE LIMITS |
547 | |
554 | |
548 | JSON::XS does not impose any limits on the size of JSON texts or Perl |
555 | 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 |
556 | values they represent - if your machine can handle it, JSON::XS will |
550 | encode or decode it. Future versions might optionally impose structure |
557 | encode or decode it. Future versions might optionally impose structure |