ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/JSON-XS/XS.pm
(Generate patch)

Comparing JSON-XS/XS.pm (file contents):
Revision 1.33 by root, Wed May 9 16:10:37 2007 UTC vs.
Revision 1.38 by root, Wed Jun 6 18:16:52 2007 UTC

86package JSON::XS; 86package JSON::XS;
87 87
88use strict; 88use strict;
89 89
90BEGIN { 90BEGIN {
91 our $VERSION = '1.12'; 91 our $VERSION = '1.23';
92 our @ISA = qw(Exporter); 92 our @ISA = qw(Exporter);
93 93
94 our @EXPORT = qw(to_json from_json objToJson jsonToObj); 94 our @EXPORT = qw(to_json from_json objToJson jsonToObj);
95 require Exporter; 95 require Exporter;
96 96
374 374
375JSON numbers and strings become simple Perl scalars. JSON arrays become 375JSON numbers and strings become simple Perl scalars. JSON arrays become
376Perl arrayrefs and JSON objects become Perl hashrefs. C<true> becomes 376Perl arrayrefs and JSON objects become Perl hashrefs. C<true> becomes
377C<1>, C<false> becomes C<0> and C<null> becomes C<undef>. 377C<1>, C<false> becomes C<0> and C<null> becomes C<undef>.
378 378
379=item ($perl_scalar, $characters) = $json->decode_prefix ($json_text)
380
381This works like the C<decode> method, but instead of raising an exception
382when there is trailing garbage after the first JSON object, it will
383silently stop parsing there and return the number of characters consumed
384so far.
385
386This is useful if your JSON texts are not delimited by an outer protocol
387(which is not the brightest thing to do in the first place) and you need
388to know where the JSON text ends.
389
390 JSON::XS->new->decode_prefix ("[1] the tail")
391 => ([], 3)
392
379=back 393=back
380 394
381 395
382=head1 MAPPING 396=head1 MAPPING
383 397
598It seems that JSON::XS is surprisingly fast, as shown in the following 612It seems that JSON::XS is surprisingly fast, as shown in the following
599tables. They have been generated with the help of the C<eg/bench> program 613tables. They have been generated with the help of the C<eg/bench> program
600in the JSON::XS distribution, to make it easy to compare on your own 614in the JSON::XS distribution, to make it easy to compare on your own
601system. 615system.
602 616
603First comes a comparison between various modules using a very short JSON 617First comes a comparison between various modules using a very short
604string: 618single-line JSON string:
605 619
606 {"method": "handleMessage", "params": ["user1", "we were just talking"], "id": null} 620 {"method": "handleMessage", "params": ["user1", "we were just talking"], \
621 "id": null, "array":[1,11,234,-5,1e5,1e7, true, false]}
607 622
608It shows the number of encodes/decodes per second (JSON::XS uses the 623It shows the number of encodes/decodes per second (JSON::XS uses the
609functional interface, while JSON::XS/2 uses the OO interface with 624functional interface, while JSON::XS/2 uses the OO interface with
610pretty-printing and hashkey sorting enabled). Higher is better: 625pretty-printing and hashkey sorting enabled). Higher is better:
611 626
612 module | encode | decode | 627 module | encode | decode |
613 -----------|------------|------------| 628 -----------|------------|------------|
614 JSON | 11488.516 | 7823.035 | 629 JSON | 7645.468 | 4208.613 |
615 JSON::DWIW | 94708.054 | 129094.260 | 630 JSON::DWIW | 68534.379 | 79437.576 |
616 JSON::PC | 63884.157 | 128528.212 | 631 JSON::PC | 65948.176 | 78251.940 |
617 JSON::Syck | 34898.677 | 42096.911 | 632 JSON::Syck | 23379.621 | 28416.694 |
618 JSON::XS | 654027.064 | 396423.669 | 633 JSON::XS | 388361.481 | 199728.762 |
619 JSON::XS/2 | 371564.190 | 371725.613 | 634 JSON::XS/2 | 218453.333 | 192399.266 |
635 JSON::XS/3 | 338250.323 | 192399.266 |
636 Storable | 15732.573 | 28571.553 |
620 -----------+------------+------------+ 637 -----------+------------+------------+
621 638
622That is, JSON::XS is more than six times faster than JSON::DWIW on 639That is, JSON::XS is about five times faster than JSON::DWIW on encoding,
623encoding, more than three times faster on decoding, and about thirty times 640about three times faster on decoding, and over fourty times faster
624faster than JSON, even with pretty-printing and key sorting. 641than JSON, even with pretty-printing and key sorting. It also compares
642favourably to Storable for small amounts of data.
625 643
626Using a longer test string (roughly 18KB, generated from Yahoo! Locals 644Using a longer test string (roughly 18KB, generated from Yahoo! Locals
627search API (http://nanoref.com/yahooapis/mgPdGg): 645search API (http://nanoref.com/yahooapis/mgPdGg):
628 646
629 module | encode | decode | 647 module | encode | decode |
630 -----------|------------|------------| 648 -----------|------------|------------|
631 JSON | 273.023 | 44.674 | 649 JSON | 254.685 | 37.665 |
632 JSON::DWIW | 1089.383 | 1145.704 | 650 JSON::DWIW | 1014.244 | 1087.678 |
633 JSON::PC | 3097.419 | 2393.921 | 651 JSON::PC | 3602.116 | 2307.352 |
634 JSON::Syck | 514.060 | 843.053 | 652 JSON::Syck | 558.035 | 776.263 |
635 JSON::XS | 6479.668 | 3636.364 | 653 JSON::XS | 5747.196 | 3543.684 |
636 JSON::XS/2 | 3774.221 | 3599.124 | 654 JSON::XS/2 | 3968.121 | 3589.170 |
655 JSON::XS/3 | 6105.246 | 3561.134 |
656 Storable | 4456.337 | 5320.020 |
637 -----------+------------+------------+ 657 -----------+------------+------------+
638 658
639Again, JSON::XS leads by far. 659Again, JSON::XS leads by far.
640 660
641On large strings containing lots of high unicode characters, some modules 661On large strings containing lots of high unicode characters, some modules

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines