… | |
… | |
614 | |
614 | |
615 | You often hear that JSON is a subset (or a close subset) of YAML. This is, |
615 | You often hear that JSON is a subset (or a close subset) of YAML. This is, |
616 | however, a mass hysteria and very far from the truth. In general, there is |
616 | however, a mass hysteria and very far from the truth. In general, there is |
617 | no way to configure JSON::XS to output a data structure as valid YAML. |
617 | no way to configure JSON::XS to output a data structure as valid YAML. |
618 | |
618 | |
619 | If you really must use JSON::XS to generate YAML, you should this |
619 | If you really must use JSON::XS to generate YAML, you should use this |
620 | algorithm (subject to change in future versions): |
620 | algorithm (subject to change in future versions): |
621 | |
621 | |
622 | my $to_yaml = JSON::XS->new->utf8->space_after (1); |
622 | my $to_yaml = JSON::XS->new->utf8->space_after (1); |
623 | my $yaml = $to_yaml->encode ($ref) . "\n"; |
623 | my $yaml = $to_yaml->encode ($ref) . "\n"; |
624 | |
624 | |
625 | This will usually generate JSON texts that also parse as valid |
625 | This will usually generate JSON texts that also parse as valid |
626 | YAML. Please note that YAML has hardcoded limits on object key lengths |
626 | YAML. Please note that YAML has hardcoded limits on (simple) object key |
627 | that JSON doesn't have, so you should make sure that your hash keys are |
627 | lengths that JSON doesn't have, so you should make sure that your hash |
628 | noticably shorter than 1024 characters. |
628 | keys are noticably shorter than the 1024 characters YAML allows. |
629 | |
629 | |
630 | There might be other incompatibilities that I am not aware of. In general |
630 | There might be other incompatibilities that I am not aware of. In general |
631 | you should not try to generate YAML with a JSON generator or vice versa, |
631 | you should not try to generate YAML with a JSON generator or vice versa, |
632 | or try to parse JSON with a YAML parser or vice versa. |
632 | or try to parse JSON with a YAML parser or vice versa: chances are high |
|
|
633 | that you will run into severe interoperability problems. |
633 | |
634 | |
634 | |
635 | |
635 | =head2 SPEED |
636 | =head2 SPEED |
636 | |
637 | |
637 | It seems that JSON::XS is surprisingly fast, as shown in the following |
638 | It seems that JSON::XS is surprisingly fast, as shown in the following |
… | |
… | |
719 | |
720 | |
720 | And last but least, something else could bomb you that I forgot to think |
721 | And last but least, something else could bomb you that I forgot to think |
721 | of. In that case, you get to keep the pieces. I am always open for hints, |
722 | of. In that case, you get to keep the pieces. I am always open for hints, |
722 | though... |
723 | though... |
723 | |
724 | |
|
|
725 | If you are using JSON::XS to return packets to consumption |
|
|
726 | by javascript scripts in a browser you should have a look at |
|
|
727 | L<http://jpsykes.com/47/practical-csrf-and-json-security> to see wether |
|
|
728 | you are vulnerable to some common attack vectors (which really are browser |
|
|
729 | design bugs, but it is still you who will have to deal with it, as major |
|
|
730 | browser developers care only for features, not about doing security |
|
|
731 | right). |
|
|
732 | |
724 | |
733 | |
725 | =head1 BUGS |
734 | =head1 BUGS |
726 | |
735 | |
727 | While the goal of this module is to be correct, that unfortunately does |
736 | While the goal of this module is to be correct, that unfortunately does |
728 | not mean its bug-free, only that I think its design is bug-free. It is |
737 | not mean its bug-free, only that I think its design is bug-free. It is |