… | |
… | |
535 | Does not generate valid JSON texts (key strings are often unquoted, |
535 | Does not generate valid JSON texts (key strings are often unquoted, |
536 | empty keys result in nothing being output) |
536 | empty keys result in nothing being output) |
537 | |
537 | |
538 | Does not check input for validity. |
538 | Does not check input for validity. |
539 | |
539 | |
|
|
540 | JSON and YAML |
|
|
541 | You often hear that JSON is a subset (or a close subset) of YAML. This |
|
|
542 | is, however, a mass hysteria and very far from the truth. In general, |
|
|
543 | there is no way to configure JSON::XS to output a data structure as |
|
|
544 | valid YAML. |
|
|
545 | |
|
|
546 | If you really must use JSON::XS to generate YAML, you should use this |
|
|
547 | algorithm (subject to change in future versions): |
|
|
548 | |
|
|
549 | my $to_yaml = JSON::XS->new->utf8->space_after (1); |
|
|
550 | my $yaml = $to_yaml->encode ($ref) . "\n"; |
|
|
551 | |
|
|
552 | This will usually generate JSON texts that also parse as valid YAML. |
|
|
553 | Please note that YAML has hardcoded limits on (simple) object key |
|
|
554 | lengths that JSON doesn't have, so you should make sure that your hash |
|
|
555 | keys are noticably shorter than the 1024 characters YAML allows. |
|
|
556 | |
|
|
557 | There might be other incompatibilities that I am not aware of. In |
|
|
558 | general you should not try to generate YAML with a JSON generator or |
|
|
559 | vice versa, or try to parse JSON with a YAML parser or vice versa: |
|
|
560 | chances are high that you will run into severe interoperability |
|
|
561 | problems. |
|
|
562 | |
540 | SPEED |
563 | SPEED |
541 | It seems that JSON::XS is surprisingly fast, as shown in the following |
564 | It seems that JSON::XS is surprisingly fast, as shown in the following |
542 | tables. They have been generated with the help of the "eg/bench" program |
565 | tables. They have been generated with the help of the "eg/bench" program |
543 | in the JSON::XS distribution, to make it easy to compare on your own |
566 | in the JSON::XS distribution, to make it easy to compare on your own |
544 | system. |
567 | system. |
545 | |
568 | |
546 | First comes a comparison between various modules using a very short JSON |
569 | First comes a comparison between various modules using a very short |
547 | string: |
570 | single-line JSON string: |
548 | |
571 | |
549 | {"method": "handleMessage", "params": ["user1", "we were just talking"], "id": null} |
572 | {"method": "handleMessage", "params": ["user1", "we were just talking"], \ |
|
|
573 | "id": null, "array":[1,11,234,-5,1e5,1e7, true, false]} |
550 | |
574 | |
551 | It shows the number of encodes/decodes per second (JSON::XS uses the |
575 | It shows the number of encodes/decodes per second (JSON::XS uses the |
552 | functional interface, while JSON::XS/2 uses the OO interface with |
576 | functional interface, while JSON::XS/2 uses the OO interface with |
553 | pretty-printing and hashkey sorting enabled). Higher is better: |
577 | pretty-printing and hashkey sorting enabled, JSON::XS/3 enables shrink). |
|
|
578 | Higher is better: |
554 | |
579 | |
555 | module | encode | decode | |
580 | module | encode | decode | |
556 | -----------|------------|------------| |
581 | -----------|------------|------------| |
557 | JSON | 11488.516 | 7823.035 | |
582 | JSON | 7645.468 | 4208.613 | |
558 | JSON::DWIW | 94708.054 | 129094.260 | |
583 | JSON::DWIW | 40721.398 | 77101.176 | |
559 | JSON::PC | 63884.157 | 128528.212 | |
584 | JSON::PC | 65948.176 | 78251.940 | |
560 | JSON::Syck | 34898.677 | 42096.911 | |
585 | JSON::Syck | 22844.793 | 26479.192 | |
561 | JSON::XS | 654027.064 | 396423.669 | |
586 | JSON::XS | 388361.481 | 199728.762 | |
562 | JSON::XS/2 | 371564.190 | 371725.613 | |
587 | JSON::XS/2 | 218453.333 | 192399.266 | |
|
|
588 | JSON::XS/3 | 338250.323 | 192399.266 | |
|
|
589 | Storable | 15779.925 | 14169.946 | |
563 | -----------+------------+------------+ |
590 | -----------+------------+------------+ |
564 | |
591 | |
565 | That is, JSON::XS is more than six times faster than JSON::DWIW on |
592 | That is, JSON::XS is about five times faster than JSON::DWIW on |
566 | encoding, more than three times faster on decoding, and about thirty |
593 | encoding, about three times faster on decoding, and over fourty times |
567 | times faster than JSON, even with pretty-printing and key sorting. |
594 | faster than JSON, even with pretty-printing and key sorting. It also |
|
|
595 | compares favourably to Storable for small amounts of data. |
568 | |
596 | |
569 | Using a longer test string (roughly 18KB, generated from Yahoo! Locals |
597 | Using a longer test string (roughly 18KB, generated from Yahoo! Locals |
570 | search API (http://nanoref.com/yahooapis/mgPdGg): |
598 | search API (http://nanoref.com/yahooapis/mgPdGg): |
571 | |
599 | |
572 | module | encode | decode | |
600 | module | encode | decode | |
573 | -----------|------------|------------| |
601 | -----------|------------|------------| |
574 | JSON | 273.023 | 44.674 | |
602 | JSON | 254.685 | 37.665 | |
575 | JSON::DWIW | 1089.383 | 1145.704 | |
603 | JSON::DWIW | 843.343 | 1049.731 | |
576 | JSON::PC | 3097.419 | 2393.921 | |
604 | JSON::PC | 3602.116 | 2307.352 | |
577 | JSON::Syck | 514.060 | 843.053 | |
605 | JSON::Syck | 505.107 | 787.899 | |
578 | JSON::XS | 6479.668 | 3636.364 | |
606 | JSON::XS | 5747.196 | 3690.220 | |
579 | JSON::XS/2 | 3774.221 | 3599.124 | |
607 | JSON::XS/2 | 3968.121 | 3676.634 | |
|
|
608 | JSON::XS/3 | 6105.246 | 3662.508 | |
|
|
609 | Storable | 4417.337 | 5285.161 | |
580 | -----------+------------+------------+ |
610 | -----------+------------+------------+ |
581 | |
611 | |
582 | Again, JSON::XS leads by far. |
612 | Again, JSON::XS leads by far (except for Storable which non-surprisingly |
|
|
613 | decodes faster). |
583 | |
614 | |
584 | On large strings containing lots of high unicode characters, some |
615 | On large strings containing lots of high unicode characters, some |
585 | modules (such as JSON::PC) seem to decode faster than JSON::XS, but the |
616 | modules (such as JSON::PC) seem to decode faster than JSON::XS, but the |
586 | result will be broken due to missing (or wrong) unicode handling. Others |
617 | result will be broken due to missing (or wrong) unicode handling. Others |
587 | refuse to decode or encode properly, so it was impossible to prepare a |
618 | refuse to decode or encode properly, so it was impossible to prepare a |