… | |
… | |
905 | =back |
905 | =back |
906 | |
906 | |
907 | |
907 | |
908 | =head2 JSON and YAML |
908 | =head2 JSON and YAML |
909 | |
909 | |
910 | You often hear that JSON is a subset (or a close subset) of YAML. This is, |
910 | You often hear that JSON is a subset of YAML. This is, however, a mass |
911 | however, a mass hysteria and very far from the truth. In general, there is |
911 | hysteria and very far from the truth. In general, there is no way to |
912 | no way to configure JSON::XS to output a data structure as valid YAML. |
912 | configure JSON::XS to output a data structure as valid YAML that works for |
|
|
913 | all cases. |
913 | |
914 | |
914 | If you really must use JSON::XS to generate YAML, you should use this |
915 | If you really must use JSON::XS to generate YAML, you should use this |
915 | algorithm (subject to change in future versions): |
916 | algorithm (subject to change in future versions): |
916 | |
917 | |
917 | my $to_yaml = JSON::XS->new->utf8->space_after (1); |
918 | my $to_yaml = JSON::XS->new->utf8->space_after (1); |
918 | my $yaml = $to_yaml->encode ($ref) . "\n"; |
919 | my $yaml = $to_yaml->encode ($ref) . "\n"; |
919 | |
920 | |
920 | This will usually generate JSON texts that also parse as valid |
921 | This will usually generate JSON texts that also parse as valid |
921 | YAML. Please note that YAML has hardcoded limits on (simple) object key |
922 | YAML. Please note that YAML has hardcoded limits on (simple) object key |
922 | lengths that JSON doesn't have, so you should make sure that your hash |
923 | lengths that JSON doesn't have and also has different and incompatible |
|
|
924 | unicode handling, so you should make sure that your hash keys are |
923 | keys are noticeably shorter than the 1024 characters YAML allows. |
925 | noticeably shorter than the 1024 "stream characters" YAML allows and that |
|
|
926 | you do not have codepoints with values outside the Unicode BMP (basic |
|
|
927 | multilingual page). |
924 | |
928 | |
925 | There might be other incompatibilities that I am not aware of. In general |
929 | There might be other incompatibilities that I am not aware of. In general |
926 | you should not try to generate YAML with a JSON generator or vice versa, |
930 | you should not try to generate YAML with a JSON generator or vice versa, |
927 | or try to parse JSON with a YAML parser or vice versa: chances are high |
931 | or try to parse JSON with a YAML parser or vice versa: chances are high |
928 | that you will run into severe interoperability problems. |
932 | that you will run into severe interoperability problems when you least |
|
|
933 | expect it. |
929 | |
934 | |
930 | |
935 | |
931 | =head2 SPEED |
936 | =head2 SPEED |
932 | |
937 | |
933 | It seems that JSON::XS is surprisingly fast, as shown in the following |
938 | It seems that JSON::XS is surprisingly fast, as shown in the following |
… | |
… | |
1010 | |
1015 | |
1011 | Third, JSON::XS recurses using the C stack when decoding objects and |
1016 | Third, JSON::XS recurses using the C stack when decoding objects and |
1012 | arrays. The C stack is a limited resource: for instance, on my amd64 |
1017 | arrays. The C stack is a limited resource: for instance, on my amd64 |
1013 | machine with 8MB of stack size I can decode around 180k nested arrays but |
1018 | machine with 8MB of stack size I can decode around 180k nested arrays but |
1014 | only 14k nested JSON objects (due to perl itself recursing deeply on croak |
1019 | only 14k nested JSON objects (due to perl itself recursing deeply on croak |
1015 | to free the temporary). If that is exceeded, the program crashes. to be |
1020 | to free the temporary). If that is exceeded, the program crashes. To be |
1016 | conservative, the default nesting limit is set to 512. If your process |
1021 | conservative, the default nesting limit is set to 512. If your process |
1017 | has a smaller stack, you should adjust this setting accordingly with the |
1022 | has a smaller stack, you should adjust this setting accordingly with the |
1018 | C<max_depth> method. |
1023 | C<max_depth> method. |
1019 | |
1024 | |
1020 | And last but least, something else could bomb you that I forgot to think |
1025 | And last but least, something else could bomb you that I forgot to think |
… | |
… | |
1024 | If you are using JSON::XS to return packets to consumption |
1029 | If you are using JSON::XS to return packets to consumption |
1025 | by JavaScript scripts in a browser you should have a look at |
1030 | by JavaScript scripts in a browser you should have a look at |
1026 | L<http://jpsykes.com/47/practical-csrf-and-json-security> to see whether |
1031 | L<http://jpsykes.com/47/practical-csrf-and-json-security> to see whether |
1027 | you are vulnerable to some common attack vectors (which really are browser |
1032 | you are vulnerable to some common attack vectors (which really are browser |
1028 | design bugs, but it is still you who will have to deal with it, as major |
1033 | design bugs, but it is still you who will have to deal with it, as major |
1029 | browser developers care only for features, not about doing security |
1034 | browser developers care only for features, not about getting security |
1030 | right). |
1035 | right). |
1031 | |
1036 | |
1032 | |
1037 | |
1033 | =head1 THREADS |
1038 | =head1 THREADS |
1034 | |
1039 | |