ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/JSON-XS/XS.pm
(Generate patch)

Comparing JSON-XS/XS.pm (file contents):
Revision 1.83 by root, Sun Jan 20 19:19:07 2008 UTC vs.
Revision 1.84 by root, Wed Mar 19 02:52:15 2008 UTC

1=encoding utf-8
2
1=head1 NAME 3=head1 NAME
2 4
3JSON::XS - JSON serialising/deserialising, done correctly and fast 5JSON::XS - JSON serialising/deserialising, done correctly and fast
4 6
5JSON::XS - 正しくて高速な JSON シリアライザ/デシリアライザ 7JSON::XS - 正しくて高速な JSON シリアライザ/デシリアライザ
58 60
59=over 4 61=over 4
60 62
61=item * correct Unicode handling 63=item * correct Unicode handling
62 64
63This module knows how to handle Unicode, and even documents how and when 65This module knows how to handle Unicode, documents how and when it does
64it does so. 66so, and even documents what "correct" means.
65 67
66=item * round-trip integrity 68=item * round-trip integrity
67 69
68When you serialise a perl data structure using only datatypes supported 70When you serialise a perl data structure using only datatypes supported
69by JSON, the deserialised data structure is identical on the Perl level. 71by 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
71like a number). 73like a number). There minor I<are> exceptions to this, read the MAPPING
74section below to learn about those.
72 75
73=item * strict checking of JSON correctness 76=item * strict checking of JSON correctness
74 77
75There is no guessing, no generating of illegal JSON texts by default, 78There is no guessing, no generating of illegal JSON texts by default,
76and only JSON is accepted as input by default (the latter is a security 79and only JSON is accepted as input by default (the latter is a security
77feature). 80feature).
78 81
79=item * fast 82=item * fast
80 83
81Compared to other JSON modules, this module compares favourably in terms 84Compared to other JSON modules and other serialisers such as Storable,
82of speed, too. 85this module usually compares favourably in terms of speed, too.
83 86
84=item * simple to use 87=item * simple to use
85 88
86This module has both a simple functional interface as well as an OO 89This module has both a simple functional interface as well as an objetc
87interface. 90oriented interface interface.
88 91
89=item * reasonably versatile output formats 92=item * reasonably versatile output formats
90 93
91You can choose between the most compact guaranteed single-line format 94You can choose between the most compact guaranteed-single-line format
92possible (nice for simple line-based protocols), a pure-ascii format 95possible (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
94Unicode range), or a pretty-printed format (for when you want to read that 97Unicode range), or a pretty-printed format (for when you want to read that
95stuff). Or you can combine those features in whatever way you like. 98stuff). Or you can combine those features in whatever way you like.
96 99
174This enables you to store Unicode characters as single characters in a 177This enables you to store Unicode characters as single characters in a
175Perl string - very natural. 178Perl 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
179Unless 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
180the scalar to a file, in which case Perl either interprets your string as 183printing the scalar to a file, in which case Perl either interprets your
181locale-encoded text, octets/binary, or as Unicode, depending on various 184string as locale-encoded text, octets/binary, or as Unicode, depending
182settings. In no case is an encoding stored together with your data, it is 185on various settings. In no case is an encoding stored together with your
183I<use> that decides encoding, not any magical metadata. 186data, 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
186encoding of your string. 189encoding of your string.
187 190
188Just ignore that flag unless you debug a Perl bug, a module written in 191Just ignore that flag unless you debug a Perl bug, a module written in
706 709
707A JSON number becomes either an integer, numeric (floating point) or 710A JSON number becomes either an integer, numeric (floating point) or
708string scalar in perl, depending on its range and any fractional parts. On 711string scalar in perl, depending on its range and any fractional parts. On
709the Perl level, there is no difference between those as Perl handles all 712the Perl level, there is no difference between those as Perl handles all
710the conversion details, but an integer may take slightly less memory and 713the conversion details, but an integer may take slightly less memory and
711might represent more values exactly than (floating point) numbers. 714might represent more values exactly than floating point numbers.
712 715
713If the number consists of digits only, JSON::XS will try to represent 716If the number consists of digits only, JSON::XS will try to represent
714it as an integer value. If that fails, it will try to represent it as 717it as an integer value. If that fails, it will try to represent it as
715a numeric (floating point) value if that is possible without loss of 718a numeric (floating point) value if that is possible without loss of
716precision. Otherwise it will preserve the number as a string value. 719precision. Otherwise it will preserve the number as a string value (in
720which case you lose roundtripping ability, as the JSON number will be
721re-encoded toa JSON string).
717 722
718Numbers containing a fractional or exponential part will always be 723Numbers containing a fractional or exponential part will always be
719represented as numeric (floating point) values, possibly at a loss of 724represented as numeric (floating point) values, possibly at a loss of
720precision. 725precision (in which case you might lose perfect roundtripping ability, but
721 726the JSON number will still be re-encoded as a JSON number).
722This might create round-tripping problems as numbers might become strings,
723but as Perl is typeless there is no other way to do it.
724 727
725=item true, false 728=item true, false
726 729
727These JSON atoms become C<JSON::XS::true> and C<JSON::XS::false>, 730These JSON atoms become C<JSON::XS::true> and C<JSON::XS::false>,
728respectively. They are overloaded to act almost exactly like the numbers 731respectively. They are overloaded to act almost exactly like the numbers
819:). 822:).
820 823
821=back 824=back
822 825
823 826
827=head1 ENCODING/CODESET FLAG NOTES
828
829The interested reader might have seen a number of flags that signify
830encodings or codesets - C<utf8>, C<latin1> and C<ascii>. There seems to be
831some confusion on what these do, so here is a short comparison:
832
833C<utf8> controls wether the JSON text created by C<encode> (and expected
834by C<decode>) is UTF-8 encoded or not, while C<latin1> and C<ascii> only
835control wether C<encode> escapes character values outside their respective
836codeset range. Neither of these flags conflict with each other, although
837some combinations make less sense than others.
838
839Care has been taken to make all flags symmetrical with respect to
840C<encode> and C<decode>, that is, texts encoded with any combination of
841these 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
843decoding you likely have a bug somewhere.
844
845Below comes a verbose discussion of these flags. Note that a "codeset" is
846simply an abstract set of character-codepoint pairs, while an encoding
847takes those codepoint numbers and I<encodes> them, in our case into
848octets. Unicode is (among other things) a codeset, UTF-8 is an encoding,
849and ISO-8859-1 (= latin 1) and ASCII are both codesets I<and> encodings at
850the same time, which can be confusing.
851
852=over 4
853
854=item C<utf8> flag disabled
855
856When C<utf8> is disabled (the default), then C<encode>/C<decode> generate
857and expect Unicode strings, that is, characters with high ordinal Unicode
858values (> 255) will be encoded as such characters, and likewise such
859characters are decoded as-is, no canges to them will be done, except
860"(re-)interpreting" them as Unicode codepoints or Unicode characters,
861respectively (to Perl, these are the same thing in strings unless you do
862funny/weird/dumb stuff).
863
864This is useful when you want to do the encoding yourself (e.g. when you
865want to have UTF-16 encoded JSON texts) or when some other layer does
866the encoding for you (for example, when printing to a terminal using a
867filehandle that transparently encodes to UTF-8 you certainly do NOT want
868to UTF-8 encode your data first and have Perl encode it another time).
869
870=item C<utf8> flag enabled
871
872If the C<utf8>-flag is enabled, C<encode>/C<decode> will encode all
873characters using the corresponding UTF-8 multi-byte sequence, and will
874expect your input strings to be encoded as UTF-8, that is, no "character"
875of the input string must have any value > 255, as UTF-8 does not allow
876that.
877
878The C<utf8> flag therefore switches between two modes: disabled means you
879will get a Unicode string in Perl, enabled means you get an UTF-8 encoded
880octet/binary string in Perl.
881
882=item C<latin1> or C<ascii> flags enabled
883
884With C<latin1> (or C<ascii>) enabled, C<encode> will escape characters
885with ordinal values > 255 (> 127 with C<ascii>) and encode the remaining
886characters as specified by the C<utf8> flag.
887
888If C<utf8> is disabled, then the result is also correctly encoded in those
889character sets (as both are proper subsets of Unicode, meaning that a
890Unicode string with all character values < 256 is the same thing as a
891ISO-8859-1 string, and a Unicode string with all character values < 128 is
892the same thing as an ASCII string in Perl).
893
894If C<utf8> is enabled, you still get a correct UTF-8-encoded string,
895regardless of these flags, just some more characters will be escaped using
896C<\uXXXX> then before.
897
898Note that ISO-8859-1-I<encoded> strings are not compatible with UTF-8
899encoding, while ASCII-encoded strings are. That is because the ISO-8859-1
900encoding is NOT a subset of UTF-8 (despite the ISO-8859-1 I<codeset> being
901a subset of Unicode), while ASCII is.
902
903Surprisingly, C<decode> will ignore these flags and so treat all input
904values as governed by the C<utf8> flag. If it is disabled, this allows you
905to decode ISO-8859-1- and ASCII-encoded strings, as both strict subsets of
906Unicode. If it is enabled, you can correctly decode UTF-8 encoded strings.
907
908So neither C<latin1> nor C<ascii> are incompatible with the C<utf8> flag -
909they only govern when the JSON output engine escapes a character or not.
910
911The main use for C<latin1> is to relatively efficiently store binary data
912as JSON, at the expense of breaking compatibility with most JSON decoders.
913
914The main use for C<ascii> is to force the output to not contain characters
915with values > 127, which means you can interpret the resulting string
916as UTF-8, ISO-8859-1, ASCII, KOI8-R or most about any character set and
9178-bit-encoding, and still get the same data structure back. This is useful
918when your channel for JSON transfer is not 8-bit clean or the encoding
919might be mangled in between (e.g. in mail), and works because ASCII is a
920proper subset of most 8-bit and multibyte encodings in use in the world.
921
922=back
923
924
824=head1 COMPARISON 925=head1 COMPARISON
825 926
826As already mentioned, this module was created because none of the existing 927As already mentioned, this module was created because none of the existing
827JSON modules could be made to work correctly. First I will describe the 928JSON modules could be made to work correctly. First I will describe the
828problems (or pleasures) I encountered with various existing JSON modules, 929problems (or pleasures) I encountered with various existing JSON modules,
829followed by some benchmark values. JSON::XS was designed not to suffer 930followed by some benchmark values. JSON::XS was designed not to suffer
830from any of these problems or limitations. 931from any of these problems or limitations.
831 932
832=over 4 933=over 4
934
935=item JSON 2.xx
936
937A marvellous piece of engineering, this module either uses JSON::XS
938directly when available (so will be 100% compatible with it, including
939speed), or it uses JSON::PP, which is basically JSON::XS translated to
940Pure Perl, which should be 100% compatible with JSON::XS, just a bit
941slower.
942
943You cannot really lose by using this module.
833 944
834=item JSON 1.07 945=item JSON 1.07
835 946
836Slow (but very portable, as it is written in pure Perl). 947Slow (but very portable, as it is written in pure Perl).
837 948

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines