… | |
… | |
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 |