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.62 by root, Thu Oct 11 22:52:52 2007 UTC vs.
Revision 1.63 by root, Thu Oct 11 23:07:43 2007 UTC

103 103
104=over 4 104=over 4
105 105
106=item $json_text = to_json $perl_scalar 106=item $json_text = to_json $perl_scalar
107 107
108Converts the given Perl data structure (a simple scalar or a reference to 108Converts the given Perl data structure to a UTF-8 encoded, binary string
109a hash or array) to a UTF-8 encoded, binary string (that is, the string contains 109(that is, the string contains octets only). Croaks on error.
110octets only). Croaks on error.
111 110
112This function call is functionally identical to: 111This function call is functionally identical to:
113 112
114 $json_text = JSON::XS->new->utf8->encode ($perl_scalar) 113 $json_text = JSON::XS->new->utf8->encode ($perl_scalar)
115 114
116except being faster. 115except being faster.
117 116
118=item $perl_scalar = from_json $json_text 117=item $perl_scalar = from_json $json_text
119 118
120The opposite of C<to_json>: expects an UTF-8 (binary) string and tries to 119The opposite of C<to_json>: expects an UTF-8 (binary) string and tries
121parse that as an UTF-8 encoded JSON text, returning the resulting simple 120to parse that as an UTF-8 encoded JSON text, returning the resulting
122scalar or reference. Croaks on error. 121reference. Croaks on error.
123 122
124This function call is functionally identical to: 123This function call is functionally identical to:
125 124
126 $perl_scalar = JSON::XS->new->utf8->decode ($json_text) 125 $perl_scalar = JSON::XS->new->utf8->decode ($json_text)
127 126
135 134
136See MAPPING, below, for more information on how JSON values are mapped to 135See MAPPING, below, for more information on how JSON values are mapped to
137Perl. 136Perl.
138 137
139=back 138=back
139
140
141=head1 A FEW NOTES ON UNICODE AND PERL
142
143Since this often leads to confusion, here are a few very clear words on
144how Unicode works in Perl, modulo bugs.
145
146=over 4
147
148=item 1. Perl strings can store characters with ordinal values > 255.
149
150This enables you to store unicode characters as single characters in a
151Perl string - very natural.
152
153=item 2. Perl does I<not> associate an encoding with your strings.
154
155Unless you force it to, e.g. when matching it against a regex, or printing
156the scalar to a file, in which case Perl either interprets your string as
157locale-encoded text, octets/binary, or as Unicode, depending on various
158settings. In no case is an encoding stored together with your data, it is
159I<use> that decides encoding, not any magical metadata.
160
161=item 3. The internal utf-8 flag has no meaning with regards to the
162encoding of your string.
163
164Just ignore that flag unless you debug a Perl bug, a module written in
165XS or want to dive into the internals of perl. Otherwise it will only
166confuse you, as, despite the name, it says nothing about how your string
167is encoded. You can have unicode strings with that flag set, with that
168flag clear, and you can have binary data with that flag set and that flag
169clear. Other possibilities exist, too.
170
171If you didn't know about that flag, just the better, pretend it doesn't
172exist.
173
174=item 4. A "Unicode String" is simply a string where each character can be
175validly interpreted as a Unicode codepoint.
176
177If you have UTF-8 encoded data, it is no longer a Unicode string, but a
178Unicode string encoded in UTF-8, giving you a binary string.
179
180=item 5. A string containing "high" (> 255) character values is I<not> a UTF-8 string.
181
182Its a fact. Learn to live with it.
183
184=back
185
186I hope this helps :)
140 187
141 188
142=head1 OBJECT-ORIENTED INTERFACE 189=head1 OBJECT-ORIENTED INTERFACE
143 190
144The object oriented interface lets you configure your own encoding or 191The object oriented interface lets you configure your own encoding or

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines