… | |
… | |
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. |
… | |
… | |
549 | {"method": "handleMessage", "params": ["user1", "we were just talking"], \ |
572 | {"method": "handleMessage", "params": ["user1", "we were just talking"], \ |
550 | "id": null, "array":[1,11,234,-5,1e5,1e7, true, false]} |
573 | "id": null, "array":[1,11,234,-5,1e5,1e7, true, false]} |
551 | |
574 | |
552 | 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 |
553 | functional interface, while JSON::XS/2 uses the OO interface with |
576 | functional interface, while JSON::XS/2 uses the OO interface with |
554 | 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: |
555 | |
579 | |
556 | module | encode | decode | |
580 | module | encode | decode | |
557 | -----------|------------|------------| |
581 | -----------|------------|------------| |
558 | JSON | 7645.468 | 4208.613 | |
582 | JSON | 7645.468 | 4208.613 | |
559 | JSON::DWIW | 68534.379 | 79437.576 | |
583 | JSON::DWIW | 40721.398 | 77101.176 | |
560 | JSON::PC | 65948.176 | 78251.940 | |
584 | JSON::PC | 65948.176 | 78251.940 | |
561 | JSON::Syck | 23379.621 | 28416.694 | |
585 | JSON::Syck | 22844.793 | 26479.192 | |
562 | JSON::XS | 388361.481 | 199728.762 | |
586 | JSON::XS | 388361.481 | 199728.762 | |
563 | JSON::XS/2 | 218453.333 | 192399.266 | |
587 | JSON::XS/2 | 218453.333 | 192399.266 | |
564 | JSON::XS/3 | 338250.323 | 192399.266 | |
588 | JSON::XS/3 | 338250.323 | 192399.266 | |
565 | Storable | 15732.573 | 28571.553 | |
589 | Storable | 15779.925 | 14169.946 | |
566 | -----------+------------+------------+ |
590 | -----------+------------+------------+ |
567 | |
591 | |
568 | That is, JSON::XS is about five times faster than JSON::DWIW on |
592 | That is, JSON::XS is about five times faster than JSON::DWIW on |
569 | encoding, about three times faster on decoding, and over fourty times |
593 | encoding, about three times faster on decoding, and over fourty times |
570 | faster than JSON, even with pretty-printing and key sorting. It also |
594 | faster than JSON, even with pretty-printing and key sorting. It also |
… | |
… | |
574 | search API (http://nanoref.com/yahooapis/mgPdGg): |
598 | search API (http://nanoref.com/yahooapis/mgPdGg): |
575 | |
599 | |
576 | module | encode | decode | |
600 | module | encode | decode | |
577 | -----------|------------|------------| |
601 | -----------|------------|------------| |
578 | JSON | 254.685 | 37.665 | |
602 | JSON | 254.685 | 37.665 | |
579 | JSON::DWIW | 1014.244 | 1087.678 | |
603 | JSON::DWIW | 843.343 | 1049.731 | |
580 | JSON::PC | 3602.116 | 2307.352 | |
604 | JSON::PC | 3602.116 | 2307.352 | |
581 | JSON::Syck | 558.035 | 776.263 | |
605 | JSON::Syck | 505.107 | 787.899 | |
582 | JSON::XS | 5747.196 | 3543.684 | |
606 | JSON::XS | 5747.196 | 3690.220 | |
583 | JSON::XS/2 | 3968.121 | 3589.170 | |
607 | JSON::XS/2 | 3968.121 | 3676.634 | |
584 | JSON::XS/3 | 6105.246 | 3561.134 | |
608 | JSON::XS/3 | 6105.246 | 3662.508 | |
585 | Storable | 4456.337 | 5320.020 | |
609 | Storable | 4417.337 | 5285.161 | |
586 | -----------+------------+------------+ |
610 | -----------+------------+------------+ |
587 | |
611 | |
588 | Again, JSON::XS leads by far. |
612 | Again, JSON::XS leads by far (except for Storable which non-surprisingly |
|
|
613 | decodes faster). |
589 | |
614 | |
590 | On large strings containing lots of high unicode characters, some |
615 | On large strings containing lots of high unicode characters, some |
591 | 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 |
592 | 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 |
593 | 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 |