|
|
1 | =encoding utf-8 |
|
|
2 | |
1 | =head1 NAME |
3 | =head1 NAME |
2 | |
4 | |
3 | JSON::XS - JSON serialising/deserialising, done correctly and fast |
5 | JSON::XS - JSON serialising/deserialising, done correctly and fast |
4 | |
6 | |
5 | JSON::XS - 正しくて高速な JSON シリアライザ/デシリアライザ |
7 | JSON::XS - 正しくて高速な JSON シリアライザ/デシリアライザ |
… | |
… | |
58 | |
60 | |
59 | =over 4 |
61 | =over 4 |
60 | |
62 | |
61 | =item * correct Unicode handling |
63 | =item * correct Unicode handling |
62 | |
64 | |
63 | This module knows how to handle Unicode, and even documents how and when |
65 | This module knows how to handle Unicode, documents how and when it does |
64 | it does so. |
66 | so, and even documents what "correct" means. |
65 | |
67 | |
66 | =item * round-trip integrity |
68 | =item * round-trip integrity |
67 | |
69 | |
68 | When you serialise a perl data structure using only datatypes supported |
70 | When you serialise a perl data structure using only datatypes supported |
69 | by JSON, the deserialised data structure is identical on the Perl level. |
71 | by JSON, the deserialised data structure is identical on the Perl level. |
70 | (e.g. the string "2.0" doesn't suddenly become "2" just because it looks |
72 | (e.g. the string "2.0" doesn't suddenly become "2" just because it looks |
71 | like a number). |
73 | like a number). There minor I<are> exceptions to this, read the MAPPING |
|
|
74 | section below to learn about those. |
72 | |
75 | |
73 | =item * strict checking of JSON correctness |
76 | =item * strict checking of JSON correctness |
74 | |
77 | |
75 | There is no guessing, no generating of illegal JSON texts by default, |
78 | There is no guessing, no generating of illegal JSON texts by default, |
76 | and only JSON is accepted as input by default (the latter is a security |
79 | and only JSON is accepted as input by default (the latter is a security |
77 | feature). |
80 | feature). |
78 | |
81 | |
79 | =item * fast |
82 | =item * fast |
80 | |
83 | |
81 | Compared to other JSON modules, this module compares favourably in terms |
84 | Compared to other JSON modules and other serialisers such as Storable, |
82 | of speed, too. |
85 | this module usually compares favourably in terms of speed, too. |
83 | |
86 | |
84 | =item * simple to use |
87 | =item * simple to use |
85 | |
88 | |
86 | This module has both a simple functional interface as well as an OO |
89 | This module has both a simple functional interface as well as an objetc |
87 | interface. |
90 | oriented interface interface. |
88 | |
91 | |
89 | =item * reasonably versatile output formats |
92 | =item * reasonably versatile output formats |
90 | |
93 | |
91 | You can choose between the most compact guaranteed single-line format |
94 | You can choose between the most compact guaranteed-single-line format |
92 | possible (nice for simple line-based protocols), a pure-ascii format |
95 | possible (nice for simple line-based protocols), a pure-ascii format |
93 | (for when your transport is not 8-bit clean, still supports the whole |
96 | (for when your transport is not 8-bit clean, still supports the whole |
94 | Unicode range), or a pretty-printed format (for when you want to read that |
97 | Unicode range), or a pretty-printed format (for when you want to read that |
95 | stuff). Or you can combine those features in whatever way you like. |
98 | stuff). Or you can combine those features in whatever way you like. |
96 | |
99 | |
… | |
… | |
174 | This enables you to store Unicode characters as single characters in a |
177 | This enables you to store Unicode characters as single characters in a |
175 | Perl string - very natural. |
178 | Perl string - very natural. |
176 | |
179 | |
177 | =item 2. Perl does I<not> associate an encoding with your strings. |
180 | =item 2. Perl does I<not> associate an encoding with your strings. |
178 | |
181 | |
179 | Unless you force it to, e.g. when matching it against a regex, or printing |
182 | ... until you force it to, e.g. when matching it against a regex, or |
180 | the scalar to a file, in which case Perl either interprets your string as |
183 | printing the scalar to a file, in which case Perl either interprets your |
181 | locale-encoded text, octets/binary, or as Unicode, depending on various |
184 | string as locale-encoded text, octets/binary, or as Unicode, depending |
182 | settings. In no case is an encoding stored together with your data, it is |
185 | on various settings. In no case is an encoding stored together with your |
183 | I<use> that decides encoding, not any magical metadata. |
186 | data, it is I<use> that decides encoding, not any magical meta data. |
184 | |
187 | |
185 | =item 3. The internal utf-8 flag has no meaning with regards to the |
188 | =item 3. The internal utf-8 flag has no meaning with regards to the |
186 | encoding of your string. |
189 | encoding of your string. |
187 | |
190 | |
188 | Just ignore that flag unless you debug a Perl bug, a module written in |
191 | Just ignore that flag unless you debug a Perl bug, a module written in |
… | |
… | |
706 | |
709 | |
707 | A JSON number becomes either an integer, numeric (floating point) or |
710 | A JSON number becomes either an integer, numeric (floating point) or |
708 | string scalar in perl, depending on its range and any fractional parts. On |
711 | string scalar in perl, depending on its range and any fractional parts. On |
709 | the Perl level, there is no difference between those as Perl handles all |
712 | the Perl level, there is no difference between those as Perl handles all |
710 | the conversion details, but an integer may take slightly less memory and |
713 | the conversion details, but an integer may take slightly less memory and |
711 | might represent more values exactly than (floating point) numbers. |
714 | might represent more values exactly than floating point numbers. |
712 | |
715 | |
713 | If the number consists of digits only, JSON::XS will try to represent |
716 | If the number consists of digits only, JSON::XS will try to represent |
714 | it as an integer value. If that fails, it will try to represent it as |
717 | it as an integer value. If that fails, it will try to represent it as |
715 | a numeric (floating point) value if that is possible without loss of |
718 | a numeric (floating point) value if that is possible without loss of |
716 | precision. Otherwise it will preserve the number as a string value. |
719 | precision. Otherwise it will preserve the number as a string value (in |
|
|
720 | which case you lose roundtripping ability, as the JSON number will be |
|
|
721 | re-encoded toa JSON string). |
717 | |
722 | |
718 | Numbers containing a fractional or exponential part will always be |
723 | Numbers containing a fractional or exponential part will always be |
719 | represented as numeric (floating point) values, possibly at a loss of |
724 | represented as numeric (floating point) values, possibly at a loss of |
720 | precision. |
725 | precision (in which case you might lose perfect roundtripping ability, but |
721 | |
726 | the JSON number will still be re-encoded as a JSON number). |
722 | This might create round-tripping problems as numbers might become strings, |
|
|
723 | but as Perl is typeless there is no other way to do it. |
|
|
724 | |
727 | |
725 | =item true, false |
728 | =item true, false |
726 | |
729 | |
727 | These JSON atoms become C<JSON::XS::true> and C<JSON::XS::false>, |
730 | These JSON atoms become C<JSON::XS::true> and C<JSON::XS::false>, |
728 | respectively. They are overloaded to act almost exactly like the numbers |
731 | respectively. They are overloaded to act almost exactly like the numbers |
… | |
… | |
774 | These special values become JSON true and JSON false values, |
777 | These special values become JSON true and JSON false values, |
775 | respectively. You can also use C<\1> and C<\0> directly if you want. |
778 | respectively. You can also use C<\1> and C<\0> directly if you want. |
776 | |
779 | |
777 | =item blessed objects |
780 | =item blessed objects |
778 | |
781 | |
779 | Blessed objects are not allowed. JSON::XS currently tries to encode their |
782 | Blessed objects are not directly representable in JSON. See the |
780 | underlying representation (hash- or arrayref), but this behaviour might |
783 | C<allow_blessed> and C<convert_blessed> methods on various options on |
781 | change in future versions. |
784 | how to deal with this: basically, you can choose between throwing an |
|
|
785 | exception, encoding the reference as if it weren't blessed, or provide |
|
|
786 | your own serialiser method. |
782 | |
787 | |
783 | =item simple scalars |
788 | =item simple scalars |
784 | |
789 | |
785 | Simple Perl scalars (any scalar that is not a reference) are the most |
790 | Simple Perl scalars (any scalar that is not a reference) are the most |
786 | difficult objects to encode: JSON::XS will encode undefined scalars as |
791 | difficult objects to encode: JSON::XS will encode undefined scalars as |
787 | JSON null value, scalars that have last been used in a string context |
792 | JSON C<null> values, scalars that have last been used in a string context |
788 | before encoding as JSON strings and anything else as number value: |
793 | before encoding as JSON strings, and anything else as number value: |
789 | |
794 | |
790 | # dump as number |
795 | # dump as number |
791 | encode_json [2] # yields [2] |
796 | encode_json [2] # yields [2] |
792 | encode_json [-3.0e17] # yields [-3e+17] |
797 | encode_json [-3.0e17] # yields [-3e+17] |
793 | my $value = 5; encode_json [$value] # yields [5] |
798 | my $value = 5; encode_json [$value] # yields [5] |
… | |
… | |
811 | my $x = "3"; # some variable containing a string |
816 | my $x = "3"; # some variable containing a string |
812 | $x += 0; # numify it, ensuring it will be dumped as a number |
817 | $x += 0; # numify it, ensuring it will be dumped as a number |
813 | $x *= 1; # same thing, the choice is yours. |
818 | $x *= 1; # same thing, the choice is yours. |
814 | |
819 | |
815 | You can not currently force the type in other, less obscure, ways. Tell me |
820 | You can not currently force the type in other, less obscure, ways. Tell me |
816 | if you need this capability. |
821 | if you need this capability (but don't forget to explain why its needed |
|
|
822 | :). |
|
|
823 | |
|
|
824 | =back |
|
|
825 | |
|
|
826 | |
|
|
827 | =head1 ENCODING/CODESET FLAG NOTES |
|
|
828 | |
|
|
829 | The interested reader might have seen a number of flags that signify |
|
|
830 | encodings or codesets - C<utf8>, C<latin1> and C<ascii>. There seems to be |
|
|
831 | some confusion on what these do, so here is a short comparison: |
|
|
832 | |
|
|
833 | C<utf8> controls wether the JSON text created by C<encode> (and expected |
|
|
834 | by C<decode>) is UTF-8 encoded or not, while C<latin1> and C<ascii> only |
|
|
835 | control wether C<encode> escapes character values outside their respective |
|
|
836 | codeset range. Neither of these flags conflict with each other, although |
|
|
837 | some combinations make less sense than others. |
|
|
838 | |
|
|
839 | Care has been taken to make all flags symmetrical with respect to |
|
|
840 | C<encode> and C<decode>, that is, texts encoded with any combination of |
|
|
841 | these flag values will be correctly decoded when the same flags are used |
|
|
842 | - in general, if you use different flag settings while encoding vs. when |
|
|
843 | decoding you likely have a bug somewhere. |
|
|
844 | |
|
|
845 | Below comes a verbose discussion of these flags. Note that a "codeset" is |
|
|
846 | simply an abstract set of character-codepoint pairs, while an encoding |
|
|
847 | takes those codepoint numbers and I<encodes> them, in our case into |
|
|
848 | octets. Unicode is (among other things) a codeset, UTF-8 is an encoding, |
|
|
849 | and ISO-8859-1 (= latin 1) and ASCII are both codesets I<and> encodings at |
|
|
850 | the same time, which can be confusing. |
|
|
851 | |
|
|
852 | =over 4 |
|
|
853 | |
|
|
854 | =item C<utf8> flag disabled |
|
|
855 | |
|
|
856 | When C<utf8> is disabled (the default), then C<encode>/C<decode> generate |
|
|
857 | and expect Unicode strings, that is, characters with high ordinal Unicode |
|
|
858 | values (> 255) will be encoded as such characters, and likewise such |
|
|
859 | characters are decoded as-is, no canges to them will be done, except |
|
|
860 | "(re-)interpreting" them as Unicode codepoints or Unicode characters, |
|
|
861 | respectively (to Perl, these are the same thing in strings unless you do |
|
|
862 | funny/weird/dumb stuff). |
|
|
863 | |
|
|
864 | This is useful when you want to do the encoding yourself (e.g. when you |
|
|
865 | want to have UTF-16 encoded JSON texts) or when some other layer does |
|
|
866 | the encoding for you (for example, when printing to a terminal using a |
|
|
867 | filehandle that transparently encodes to UTF-8 you certainly do NOT want |
|
|
868 | to UTF-8 encode your data first and have Perl encode it another time). |
|
|
869 | |
|
|
870 | =item C<utf8> flag enabled |
|
|
871 | |
|
|
872 | If the C<utf8>-flag is enabled, C<encode>/C<decode> will encode all |
|
|
873 | characters using the corresponding UTF-8 multi-byte sequence, and will |
|
|
874 | expect your input strings to be encoded as UTF-8, that is, no "character" |
|
|
875 | of the input string must have any value > 255, as UTF-8 does not allow |
|
|
876 | that. |
|
|
877 | |
|
|
878 | The C<utf8> flag therefore switches between two modes: disabled means you |
|
|
879 | will get a Unicode string in Perl, enabled means you get an UTF-8 encoded |
|
|
880 | octet/binary string in Perl. |
|
|
881 | |
|
|
882 | =item C<latin1> or C<ascii> flags enabled |
|
|
883 | |
|
|
884 | With C<latin1> (or C<ascii>) enabled, C<encode> will escape characters |
|
|
885 | with ordinal values > 255 (> 127 with C<ascii>) and encode the remaining |
|
|
886 | characters as specified by the C<utf8> flag. |
|
|
887 | |
|
|
888 | If C<utf8> is disabled, then the result is also correctly encoded in those |
|
|
889 | character sets (as both are proper subsets of Unicode, meaning that a |
|
|
890 | Unicode string with all character values < 256 is the same thing as a |
|
|
891 | ISO-8859-1 string, and a Unicode string with all character values < 128 is |
|
|
892 | the same thing as an ASCII string in Perl). |
|
|
893 | |
|
|
894 | If C<utf8> is enabled, you still get a correct UTF-8-encoded string, |
|
|
895 | regardless of these flags, just some more characters will be escaped using |
|
|
896 | C<\uXXXX> then before. |
|
|
897 | |
|
|
898 | Note that ISO-8859-1-I<encoded> strings are not compatible with UTF-8 |
|
|
899 | encoding, while ASCII-encoded strings are. That is because the ISO-8859-1 |
|
|
900 | encoding is NOT a subset of UTF-8 (despite the ISO-8859-1 I<codeset> being |
|
|
901 | a subset of Unicode), while ASCII is. |
|
|
902 | |
|
|
903 | Surprisingly, C<decode> will ignore these flags and so treat all input |
|
|
904 | values as governed by the C<utf8> flag. If it is disabled, this allows you |
|
|
905 | to decode ISO-8859-1- and ASCII-encoded strings, as both strict subsets of |
|
|
906 | Unicode. If it is enabled, you can correctly decode UTF-8 encoded strings. |
|
|
907 | |
|
|
908 | So neither C<latin1> nor C<ascii> are incompatible with the C<utf8> flag - |
|
|
909 | they only govern when the JSON output engine escapes a character or not. |
|
|
910 | |
|
|
911 | The main use for C<latin1> is to relatively efficiently store binary data |
|
|
912 | as JSON, at the expense of breaking compatibility with most JSON decoders. |
|
|
913 | |
|
|
914 | The main use for C<ascii> is to force the output to not contain characters |
|
|
915 | with values > 127, which means you can interpret the resulting string |
|
|
916 | as UTF-8, ISO-8859-1, ASCII, KOI8-R or most about any character set and |
|
|
917 | 8-bit-encoding, and still get the same data structure back. This is useful |
|
|
918 | when your channel for JSON transfer is not 8-bit clean or the encoding |
|
|
919 | might be mangled in between (e.g. in mail), and works because ASCII is a |
|
|
920 | proper subset of most 8-bit and multibyte encodings in use in the world. |
817 | |
921 | |
818 | =back |
922 | =back |
819 | |
923 | |
820 | |
924 | |
821 | =head1 COMPARISON |
925 | =head1 COMPARISON |
… | |
… | |
825 | problems (or pleasures) I encountered with various existing JSON modules, |
929 | problems (or pleasures) I encountered with various existing JSON modules, |
826 | followed by some benchmark values. JSON::XS was designed not to suffer |
930 | followed by some benchmark values. JSON::XS was designed not to suffer |
827 | from any of these problems or limitations. |
931 | from any of these problems or limitations. |
828 | |
932 | |
829 | =over 4 |
933 | =over 4 |
|
|
934 | |
|
|
935 | =item JSON 2.xx |
|
|
936 | |
|
|
937 | A marvellous piece of engineering, this module either uses JSON::XS |
|
|
938 | directly when available (so will be 100% compatible with it, including |
|
|
939 | speed), or it uses JSON::PP, which is basically JSON::XS translated to |
|
|
940 | Pure Perl, which should be 100% compatible with JSON::XS, just a bit |
|
|
941 | slower. |
|
|
942 | |
|
|
943 | You cannot really lose by using this module. |
830 | |
944 | |
831 | =item JSON 1.07 |
945 | =item JSON 1.07 |
832 | |
946 | |
833 | Slow (but very portable, as it is written in pure Perl). |
947 | Slow (but very portable, as it is written in pure Perl). |
834 | |
948 | |
… | |
… | |
916 | algorithm (subject to change in future versions): |
1030 | algorithm (subject to change in future versions): |
917 | |
1031 | |
918 | my $to_yaml = JSON::XS->new->utf8->space_after (1); |
1032 | my $to_yaml = JSON::XS->new->utf8->space_after (1); |
919 | my $yaml = $to_yaml->encode ($ref) . "\n"; |
1033 | my $yaml = $to_yaml->encode ($ref) . "\n"; |
920 | |
1034 | |
921 | This will usually generate JSON texts that also parse as valid |
1035 | This will I<usually> generate JSON texts that also parse as valid |
922 | YAML. Please note that YAML has hardcoded limits on (simple) object key |
1036 | YAML. Please note that YAML has hardcoded limits on (simple) object key |
923 | lengths that JSON doesn't have and also has different and incompatible |
1037 | 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 |
1038 | unicode handling, so you should make sure that your hash keys are |
925 | noticeably shorter than the 1024 "stream characters" YAML allows and that |
1039 | noticeably shorter than the 1024 "stream characters" YAML allows and that |
926 | you do not have codepoints with values outside the Unicode BMP (basic |
1040 | you do not have codepoints with values outside the Unicode BMP (basic |
927 | multilingual page). YAML also does not allow C<\/> sequences in strings |
1041 | multilingual page). YAML also does not allow C<\/> sequences in strings |
928 | (which JSON::XS does not I<currently> generate). |
1042 | (which JSON::XS does not I<currently> generate). |
929 | |
1043 | |
930 | There might be other incompatibilities that I am not aware of. In general |
1044 | There might be other incompatibilities that I am not aware of (or the YAML |
|
|
1045 | specification has been changed yet again - it does so quite often). In |
931 | you should not try to generate YAML with a JSON generator or vice versa, |
1046 | general you should not try to generate YAML with a JSON generator or vice |
932 | or try to parse JSON with a YAML parser or vice versa: chances are high |
1047 | versa, or try to parse JSON with a YAML parser or vice versa: chances are |
933 | that you will run into severe interoperability problems when you least |
1048 | high that you will run into severe interoperability problems when you |
934 | expect it. |
1049 | least expect it. |
935 | |
1050 | |
936 | =over 4 |
1051 | =over 4 |
937 | |
1052 | |
938 | =item (*) |
1053 | =item (*) |
939 | |
1054 | |
… | |
… | |
943 | Even the author of this manpage was at some point accused of providing |
1058 | Even the author of this manpage was at some point accused of providing |
944 | "incorrect" information, despite the evidence presented (claims ranged |
1059 | "incorrect" information, despite the evidence presented (claims ranged |
945 | from "your documentation contains inaccurate and negative statements about |
1060 | from "your documentation contains inaccurate and negative statements about |
946 | YAML" (the only negative comment is this footnote, and it didn't exist |
1061 | YAML" (the only negative comment is this footnote, and it didn't exist |
947 | back then; the question on which claims were inaccurate was never answered |
1062 | back then; the question on which claims were inaccurate was never answered |
948 | etc.) to "the YAML spec is not up-to-date" (the *real* and suppsedly |
1063 | etc.) to "the YAML spec is not up-to-date" (the *real* and supposedly |
949 | JSON-compatible spec is apparently not currently publicly available) |
1064 | JSON-compatible spec is apparently not currently publicly available) |
950 | to actual requests to replace this section by *incorrect* information, |
1065 | to actual requests to replace this section by *incorrect* information, |
951 | suppressing information about the real problem). |
1066 | suppressing information about the real problem). |
952 | |
1067 | |
953 | So whenever you are told that YAML was a superset of JSON, first check |
1068 | So whenever you are told that YAML was a superset of JSON, first check |
954 | wether it is really true (it might be when you check it, but it cetrainly |
1069 | wether it is really true (it might be when you check it, but it certainly |
955 | is not true when this was written). I would much prefer if the YAML team |
1070 | was not true when this was written). I would much prefer if the YAML team |
956 | would spent their time on actually making JSON compatibility a truth |
1071 | would spent their time on actually making JSON compatibility a truth |
957 | (JSON, after all, has a very small and simple specification) instead of |
1072 | (JSON, after all, has a very small and simple specification) instead of |
958 | trying to lobby/force people into reporting untruths. |
1073 | trying to lobby/force people into reporting untruths. |
959 | |
1074 | |
960 | =back |
1075 | =back |