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.3 by root, Thu Mar 22 18:10:29 2007 UTC vs.
Revision 1.7 by root, Fri Mar 23 15:10:55 2007 UTC

59=cut 59=cut
60 60
61package JSON::XS; 61package JSON::XS;
62 62
63BEGIN { 63BEGIN {
64 $VERSION = '0.1'; 64 $VERSION = '0.2';
65 @ISA = qw(Exporter); 65 @ISA = qw(Exporter);
66 66
67 @EXPORT = qw(to_json from_json); 67 @EXPORT = qw(to_json from_json);
68 require Exporter; 68 require Exporter;
69 69
114be chained: 114be chained:
115 115
116 my $json = JSON::XS->new->utf8(1)->space_after(1)->encode ({a => [1,2]}) 116 my $json = JSON::XS->new->utf8(1)->space_after(1)->encode ({a => [1,2]})
117 => {"a": [1, 2]} 117 => {"a": [1, 2]}
118 118
119=item $json = $json->ascii ($enable) 119=item $json = $json->ascii ([$enable])
120 120
121If C<$enable> is true, then the C<encode> method will not generate 121If C<$enable> is true (or missing), then the C<encode> method will
122characters outside the code range C<0..127>. Any unicode characters 122not generate characters outside the code range C<0..127>. Any unicode
123outside that range will be escaped using either a single \uXXXX (BMP 123characters outside that range will be escaped using either a single
124characters) or a double \uHHHH\uLLLLL escape sequence, as per RFC4627. 124\uXXXX (BMP characters) or a double \uHHHH\uLLLLL escape sequence, as per
125RFC4627.
125 126
126If C<$enable> is false, then the C<encode> method will not escape Unicode 127If C<$enable> is false, then the C<encode> method will not escape Unicode
127characters unless necessary. 128characters unless necessary.
128 129
129 JSON::XS->new->ascii (1)->encode (chr 0x10401) 130 JSON::XS->new->ascii (1)->encode (chr 0x10401)
130 => \ud801\udc01 131 => \ud801\udc01
131 132
132=item $json = $json->utf8 ($enable) 133=item $json = $json->utf8 ([$enable])
133 134
134If C<$enable> is true, then the C<encode> method will encode the JSON 135If C<$enable> is true (or missing), then the C<encode> method will encode
135string into UTF-8, as required by many protocols, while the C<decode> 136the JSON string into UTF-8, as required by many protocols, while the
136method expects to be handled an UTF-8-encoded string. Please note that 137C<decode> method expects to be handled an UTF-8-encoded string. Please
137UTF-8-encoded strings do not contain any characters outside the range 138note that UTF-8-encoded strings do not contain any characters outside the
138C<0..255>, they are thus useful for bytewise/binary I/O. 139range C<0..255>, they are thus useful for bytewise/binary I/O.
139 140
140If C<$enable> is false, then the C<encode> method will return the JSON 141If C<$enable> is false, then the C<encode> method will return the JSON
141string as a (non-encoded) unicode string, while C<decode> expects thus a 142string as a (non-encoded) unicode string, while C<decode> expects thus a
142unicode string. Any decoding or encoding (e.g. to UTF-8 or UTF-16) needs 143unicode string. Any decoding or encoding (e.g. to UTF-8 or UTF-16) needs
143to be done yourself, e.g. using the Encode module. 144to be done yourself, e.g. using the Encode module.
144 145
145=item $json = $json->pretty ($enable) 146=item $json = $json->pretty ([$enable])
146 147
147This enables (or disables) all of the C<indent>, C<space_before> and 148This enables (or disables) all of the C<indent>, C<space_before> and
148C<space_after> (and in the future possibly more) flags in one call to 149C<space_after> (and in the future possibly more) flags in one call to
149generate the most readable (or most compact) form possible. 150generate the most readable (or most compact) form possible.
150 151
155 1, 156 1,
156 2 157 2
157 ] 158 ]
158 } 159 }
159 160
160=item $json = $json->indent ($enable) 161=item $json = $json->indent ([$enable])
161 162
162If C<$enable> is true, then the C<encode> method will use a multiline 163If C<$enable> is true (or missing), then the C<encode> method will use a multiline
163format as output, putting every array member or object/hash key-value pair 164format as output, putting every array member or object/hash key-value pair
164into its own line, identing them properly. 165into its own line, identing them properly.
165 166
166If C<$enable> is false, no newlines or indenting will be produced, and the 167If C<$enable> is false, no newlines or indenting will be produced, and the
167resulting JSON strings is guarenteed not to contain any C<newlines>. 168resulting JSON strings is guarenteed not to contain any C<newlines>.
168 169
169This setting has no effect when decoding JSON strings. 170This setting has no effect when decoding JSON strings.
170 171
171=item $json = $json->space_before ($enable) 172=item $json = $json->space_before ([$enable])
172 173
173If C<$enable> is true, then the C<encode> method will add an extra 174If C<$enable> is true (or missing), then the C<encode> method will add an extra
174optional space before the C<:> separating keys from values in JSON objects. 175optional space before the C<:> separating keys from values in JSON objects.
175 176
176If C<$enable> is false, then the C<encode> method will not add any extra 177If C<$enable> is false, then the C<encode> method will not add any extra
177space at those places. 178space at those places.
178 179
179This setting has no effect when decoding JSON strings. You will also most 180This setting has no effect when decoding JSON strings. You will also most
180likely combine this setting with C<space_after>. 181likely combine this setting with C<space_after>.
181 182
182=item $json = $json->space_after ($enable) 183=item $json = $json->space_after ([$enable])
183 184
184If C<$enable> is true, then the C<encode> method will add an extra 185If C<$enable> is true (or missing), then the C<encode> method will add an extra
185optional space after the C<:> separating keys from values in JSON objects 186optional space after the C<:> separating keys from values in JSON objects
186and extra whitespace after the C<,> separating key-value pairs and array 187and extra whitespace after the C<,> separating key-value pairs and array
187members. 188members.
188 189
189If C<$enable> is false, then the C<encode> method will not add any extra 190If C<$enable> is false, then the C<encode> method will not add any extra
190space at those places. 191space at those places.
191 192
192This setting has no effect when decoding JSON strings. 193This setting has no effect when decoding JSON strings.
193 194
194=item $json = $json->canonical ($enable) 195=item $json = $json->canonical ([$enable])
195 196
196If C<$enable> is true, then the C<encode> method will output JSON objects 197If C<$enable> is true (or missing), then the C<encode> method will output JSON objects
197by sorting their keys. This is adding a comparatively high overhead. 198by sorting their keys. This is adding a comparatively high overhead.
198 199
199If C<$enable> is false, then the C<encode> method will output key-value 200If C<$enable> is false, then the C<encode> method will output key-value
200pairs in the order Perl stores them (which will likely change between runs 201pairs in the order Perl stores them (which will likely change between runs
201of the same script). 202of the same script).
205the same hash migh be encoded differently even if contains the same data, 206the same hash migh be encoded differently even if contains the same data,
206as key-value pairs have no inherent ordering in Perl. 207as key-value pairs have no inherent ordering in Perl.
207 208
208This setting has no effect when decoding JSON strings. 209This setting has no effect when decoding JSON strings.
209 210
210=item $json = $json->allow_nonref ($enable) 211=item $json = $json->allow_nonref ([$enable])
211 212
212If C<$enable> is true, then the C<encode> method can convert a 213If C<$enable> is true (or missing), then the C<encode> method can convert a
213non-reference into its corresponding string, number or null JSON value, 214non-reference into its corresponding string, number or null JSON value,
214which is an extension to RFC4627. Likewise, C<decode> will accept those JSON 215which is an extension to RFC4627. Likewise, C<decode> will accept those JSON
215values instead of croaking. 216values instead of croaking.
216 217
217If C<$enable> is false, then the C<encode> method will croak if it isn't 218If C<$enable> is false, then the C<encode> method will croak if it isn't
218passed an arrayref or hashref, as JSON strings must either be an object 219passed an arrayref or hashref, as JSON strings must either be an object
219or array. Likewise, C<decode> will croak if given something that is not a 220or array. Likewise, C<decode> will croak if given something that is not a
220JSON object or array. 221JSON object or array.
222
223=item $json = $json->shrink ([$enable])
224
225Perl usually over-allocates memory a bit when allocating space for
226strings. This flag optionally resizes strings generated by either
227C<encode> or C<decode> to their minimum size possible. This can save
228memory when your JSON strings are either very very long or you have many
229short strings.
230
231If C<$enable> is true (or missing), the string returned by C<encode> will be shrunk-to-fit,
232while all strings generated by C<decode> will also be shrunk-to-fit.
233
234If C<$enable> is false, then the normal perl allocation algorithms are used.
235If you work with your data, then this is likely to be faster.
236
237In the future, this setting might control other things, such as converting
238strings that look like integers or floats into integers or floats
239internally (there is no difference on the Perl level), saving space.
221 240
222=item $json_string = $json->encode ($perl_scalar) 241=item $json_string = $json->encode ($perl_scalar)
223 242
224Converts the given Perl data structure (a simple scalar or a reference 243Converts the given Perl data structure (a simple scalar or a reference
225to a hash or array) to its JSON representation. Simple scalars will be 244to a hash or array) to its JSON representation. Simple scalars will be
242=head1 COMPARISON 261=head1 COMPARISON
243 262
244As already mentioned, this module was created because none of the existing 263As already mentioned, this module was created because none of the existing
245JSON modules could be made to work correctly. First I will describe the 264JSON modules could be made to work correctly. First I will describe the
246problems (or pleasures) I encountered with various existing JSON modules, 265problems (or pleasures) I encountered with various existing JSON modules,
247followed by some benchmark values. 266followed by some benchmark values. JSON::XS was designed not to suffer
267from any of these problems or limitations.
248 268
249=over 4 269=over 4
250 270
251=item JSON 271=item JSON 1.07
252 272
253Slow (but very portable, as it is written in pure Perl). 273Slow (but very portable, as it is written in pure Perl).
254 274
255Undocumented/buggy Unicode handling (how JSON handles unicode values is 275Undocumented/buggy Unicode handling (how JSON handles unicode values is
256undocumented. One can get far by feeding it unicode strings and doing 276undocumented. One can get far by feeding it unicode strings and doing
258 278
259No roundtripping (strings get clobbered if they look like numbers, e.g. 279No roundtripping (strings get clobbered if they look like numbers, e.g.
260the string C<2.0> will encode to C<2.0> instead of C<"2.0">, and that will 280the string C<2.0> will encode to C<2.0> instead of C<"2.0">, and that will
261decode into the number 2. 281decode into the number 2.
262 282
263=item JSON::PC 283=item JSON::PC 0.01
264 284
265Very fast. 285Very fast.
286
287Undocumented/buggy Unicode handling.
288
289No roundtripping.
290
291Has problems handling many Perl values (e.g. regex results and other magic
292values will make it croak).
293
294Does not even generate valid JSON (C<{1,2}> gets converted to C<{1:2}>
295which is not a valid JSON string.
296
297Unmaintained (maintainer unresponsive for many months, bugs are not
298getting fixed).
299
300=item JSON::Syck 0.21
301
302Very buggy (often crashes).
266 303
267Very inflexible (no human-readable format supported, format pretty much 304Very inflexible (no human-readable format supported, format pretty much
268undocumented. I need at least a format for easy reading by humans and a 305undocumented. I need at least a format for easy reading by humans and a
269single-line compact format for use in a protocol, and preferably a way to 306single-line compact format for use in a protocol, and preferably a way to
270generate ASCII-only JSON strings). 307generate ASCII-only JSON strings).
271
272Undocumented/buggy Unicode handling.
273
274No roundtripping.
275
276Has problems handling many Perl values.
277
278Does not even generate valid JSON (C<{1,2}> gets converted to C<{1:2}>
279which is not a valid JSON string.
280
281Unmaintained (maintainer unresponsive for many months, bugs are not
282getting fixed).
283
284=item JSON::Syck
285
286Very buggy (often crashes).
287
288Very inflexible.
289 308
290Completely broken (and confusingly documented) Unicode handling (unicode 309Completely broken (and confusingly documented) Unicode handling (unicode
291escapes are not working properly, you need to set ImplicitUnicode to 310escapes are not working properly, you need to set ImplicitUnicode to
292I<different> values on en- and decoding to get symmetric behaviour). 311I<different> values on en- and decoding to get symmetric behaviour).
293 312
305JSON. One bank might parse a given non-JSON request and deduct money, 324JSON. One bank might parse a given non-JSON request and deduct money,
306while the other might reject the transaction with a syntax error. While a 325while the other might reject the transaction with a syntax error. While a
307good protocol will at least recover, that is extra unnecessary work and 326good protocol will at least recover, that is extra unnecessary work and
308the transaction will still not succeed). 327the transaction will still not succeed).
309 328
310=item JSON::DWIW 329=item JSON::DWIW 0.04
311 330
312Very fast. Very natural. Very nice. 331Very fast. Very natural. Very nice.
313 332
314Undocumented unicode handling (but the best of the pack. Unicode escapes 333Undocumented unicode handling (but the best of the pack. Unicode escapes
315still don't get parsed properly). 334still don't get parsed properly).
316 335
317Very inflexible. 336Very inflexible.
318 337
319No roundtripping. 338No roundtripping.
320 339
340Does not generate valid JSON (key strings are often unquoted, empty keys
341result in nothing being output)
342
321Does not check input for validity. 343Does not check input for validity.
322 344
323=back 345=back
324 346
325=head2 SPEED 347=head2 SPEED
348
349It seems that JSON::XS is surprisingly fast, as shown in the following
350tables. They have been generated with the help of the C<eg/bench> program
351in the JSON::XS distribution, to make it easy to compare on your own
352system.
353
354First is a comparison between various modules using a very simple JSON
355string, showing the number of encodes/decodes per second (JSON::XS is
356the functional interface, while JSON::XS/2 is the OO interface with
357pretty-printing and hashkey sorting enabled).
358
359 module | encode | decode |
360 -----------|------------|------------|
361 JSON | 14006 | 6820 |
362 JSON::DWIW | 200937 | 120386 |
363 JSON::PC | 85065 | 129366 |
364 JSON::Syck | 59898 | 44232 |
365 JSON::XS | 1171478 | 342435 |
366 JSON::XS/2 | 730760 | 328714 |
367 -----------+------------+------------+
368
369That is, JSON::XS is 6 times faster than than JSON::DWIW and about 80
370times faster than JSON, even with pretty-printing and key sorting.
371
372Using a longer test string (roughly 8KB, generated from Yahoo! Locals
373search API (http://nanoref.com/yahooapis/mgPdGg):
374
375 module | encode | decode |
376 -----------|------------|------------|
377 JSON | 673 | 38 |
378 JSON::DWIW | 5271 | 770 |
379 JSON::PC | 9901 | 2491 |
380 JSON::Syck | 2360 | 786 |
381 JSON::XS | 37398 | 3202 |
382 JSON::XS/2 | 13765 | 3153 |
383 -----------+------------+------------+
384
385Again, JSON::XS leads by far in the encoding case, while still beating
386every other module in the decoding case.
387
388Last example is an almost 8MB large hash with many large binary values
389(PNG files), resulting in a lot of escaping:
390
391=head1 BUGS
392
393While the goal of this module is to be correct, that unfortunately does
394not mean its bug-free, only that I think its design is bug-free. It is
395still very young and not well-tested. If you keep reporting bugs they will
396be fixed swiftly, though.
326 397
327=cut 398=cut
328 399
3291; 4001;
330 401

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines