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.2 by root, Thu Mar 22 17:28:50 2007 UTC vs.
Revision 1.10 by root, Fri Mar 23 17:40:29 2007 UTC

18their maintainers are unresponsive, gone missing, or not listening to bug 18their maintainers are unresponsive, gone missing, or not listening to bug
19reports for other reasons. 19reports for other reasons.
20 20
21See COMPARISON, below, for a comparison to some other JSON modules. 21See COMPARISON, below, for a comparison to some other JSON modules.
22 22
23See MAPPING, below, on how JSON::XS maps perl values to JSON values and
24vice versa.
25
23=head2 FEATURES 26=head2 FEATURES
24 27
25=over 4 28=over 4
26 29
27=item * correct handling of unicode issues 30=item * correct handling of unicode issues
28 31
29This module knows how to handle Unicode, and even documents how it does so. 32This module knows how to handle Unicode, and even documents how and when
33it does so.
30 34
31=item * round-trip integrity 35=item * round-trip integrity
32 36
33When you serialise a perl data structure using only datatypes supported 37When you serialise a perl data structure using only datatypes supported
34by JSON, the deserialised data structure is identical on the Perl level. 38by JSON, the deserialised data structure is identical on the Perl level.
35(e.g. the string "2.0" doesn't suddenly become "2"). 39(e.g. the string "2.0" doesn't suddenly become "2").
36 40
37=item * strict checking of JSON correctness 41=item * strict checking of JSON correctness
38 42
39There is no guessing, no generating of illegal JSON strings by default, 43There is no guessing, no generating of illegal JSON strings by default,
40and only JSON is accepted as input (the latter is a security feature). 44and only JSON is accepted as input by default (the latter is a security
45feature).
41 46
42=item * fast 47=item * fast
43 48
44compared to other JSON modules, this module compares favourably. 49Compared to other JSON modules, this module compares favourably in terms
50of speed, too.
45 51
46=item * simple to use 52=item * simple to use
47 53
48This module has both a simple functional interface as well as an OO 54This module has both a simple functional interface as well as an OO
49interface. 55interface.
50 56
51=item * reasonably versatile output formats 57=item * reasonably versatile output formats
52 58
53You can choose between the most compact format possible, a pure-ascii 59You can choose between the most compact guarenteed single-line format
54format, or a pretty-printed format. Or you can combine those features in 60possible (nice for simple line-based protocols), a pure-ascii format (for
61when your transport is not 8-bit clean), or a pretty-printed format (for
62when you want to read that stuff). Or you can combine those features in
55whatever way you like. 63whatever way you like.
56 64
57=back 65=back
58 66
59=cut 67=cut
60 68
61package JSON::XS; 69package JSON::XS;
62 70
63BEGIN { 71BEGIN {
64 $VERSION = '0.1'; 72 $VERSION = '0.3';
65 @ISA = qw(Exporter); 73 @ISA = qw(Exporter);
66 74
67 @EXPORT = qw(to_json from_json); 75 @EXPORT = qw(to_json from_json);
68 require Exporter; 76 require Exporter;
69 77
82 90
83Converts the given Perl data structure (a simple scalar or a reference to 91Converts the given Perl data structure (a simple scalar or a reference to
84a hash or array) to a UTF-8 encoded, binary string (that is, the string contains 92a hash or array) to a UTF-8 encoded, binary string (that is, the string contains
85octets only). Croaks on error. 93octets only). Croaks on error.
86 94
87This function call is functionally identical to C<< JSON::XS->new->utf8 95This function call is functionally identical to C<< JSON::XS->new->utf8->encode ($perl_scalar) >>.
88(1)->encode ($perl_scalar) >>.
89 96
90=item $perl_scalar = from_json $json_string 97=item $perl_scalar = from_json $json_string
91 98
92The opposite of C<to_json>: expects an UTF-8 (binary) string and tries to 99The opposite of C<to_json>: expects an UTF-8 (binary) string and tries to
93parse that as an UTF-8 encoded JSON string, returning the resulting simple 100parse that as an UTF-8 encoded JSON string, returning the resulting simple
94scalar or reference. Croaks on error. 101scalar or reference. Croaks on error.
95 102
96This function call is functionally identical to C<< JSON::XS->new->utf8 103This function call is functionally identical to C<< JSON::XS->new->utf8->decode ($json_string) >>.
97(1)->decode ($json_string) >>.
98 104
99=back 105=back
100 106
101=head1 OBJECT-ORIENTED INTERFACE 107=head1 OBJECT-ORIENTED INTERFACE
102 108
111strings. All boolean flags described below are by default I<disabled>. 117strings. All boolean flags described below are by default I<disabled>.
112 118
113The mutators for flags all return the JSON object again and thus calls can 119The mutators for flags all return the JSON object again and thus calls can
114be chained: 120be chained:
115 121
116 my $json = JSON::XS->new->utf8(1)->pretty(1)->encode ({a => [1,2]}) 122 my $json = JSON::XS->new->utf8(1)->space_after(1)->encode ({a => [1,2]})
117 => {"a" : [1, 2]} 123 => {"a": [1, 2]}
118 124
119=item $json = $json->ascii ($enable) 125=item $json = $json->ascii ([$enable])
120 126
121If C<$enable> is true, then the C<encode> method will not generate 127If C<$enable> is true (or missing), then the C<encode> method will
122characters outside the code range C<0..127>. Any unicode characters 128not generate characters outside the code range C<0..127>. Any unicode
123outside that range will be escaped using either a single \uXXXX (BMP 129characters outside that range will be escaped using either a single
124characters) or a double \uHHHH\uLLLLL escape sequence, as per RFC4627. 130\uXXXX (BMP characters) or a double \uHHHH\uLLLLL escape sequence, as per
131RFC4627.
125 132
126If C<$enable> is false, then the C<encode> method will not escape Unicode 133If C<$enable> is false, then the C<encode> method will not escape Unicode
127characters unless necessary. 134characters unless necessary.
128 135
136 JSON::XS->new->ascii (1)->encode (chr 0x10401)
137 => \ud801\udc01
138
129=item $json = $json->utf8 ($enable) 139=item $json = $json->utf8 ([$enable])
130 140
131If C<$enable> is true, then the C<encode> method will encode the JSON 141If C<$enable> is true (or missing), then the C<encode> method will encode
132string into UTF-8, as required by many protocols, while the C<decode> 142the JSON string into UTF-8, as required by many protocols, while the
133method expects to be handled an UTF-8-encoded string. Please note that 143C<decode> method expects to be handled an UTF-8-encoded string. Please
134UTF-8-encoded strings do not contain any characters outside the range 144note that UTF-8-encoded strings do not contain any characters outside the
135C<0..255>, they are thus useful for bytewise/binary I/O. 145range C<0..255>, they are thus useful for bytewise/binary I/O.
136 146
137If C<$enable> is false, then the C<encode> method will return the JSON 147If C<$enable> is false, then the C<encode> method will return the JSON
138string as a (non-encoded) unicode string, while C<decode> expects thus a 148string as a (non-encoded) unicode string, while C<decode> expects thus a
139unicode string. Any decoding or encoding (e.g. to UTF-8 or UTF-16) needs 149unicode string. Any decoding or encoding (e.g. to UTF-8 or UTF-16) needs
140to be done yourself, e.g. using the Encode module. 150to be done yourself, e.g. using the Encode module.
141 151
142=item $json = $json->pretty ($enabla) 152=item $json = $json->pretty ([$enable])
143 153
144This enables (or disables) all of the C<indent>, C<space_before> and 154This enables (or disables) all of the C<indent>, C<space_before> and
145C<space_after> (and in the future possibly more) settings in one call to 155C<space_after> (and in the future possibly more) flags in one call to
146generate the most readable (or most compact) form possible. 156generate the most readable (or most compact) form possible.
147 157
158 my $json = JSON::XS->new->pretty(1)->encode ({a => [1,2]})
159 =>
160 {
161 "a" : [
162 1,
163 2
164 ]
165 }
166
148=item $json = $json->indent ($enable) 167=item $json = $json->indent ([$enable])
149 168
150If C<$enable> is true, then the C<encode> method will use a multiline 169If C<$enable> is true (or missing), then the C<encode> method will use a multiline
151format as output, putting every array member or object/hash key-value pair 170format as output, putting every array member or object/hash key-value pair
152into its own line, identing them properly. 171into its own line, identing them properly.
153 172
154If C<$enable> is false, no newlines or indenting will be produced, and the 173If C<$enable> is false, no newlines or indenting will be produced, and the
155resulting JSON strings is guarenteed not to contain any C<newlines>. 174resulting JSON strings is guarenteed not to contain any C<newlines>.
156 175
157This setting has no effect when decoding JSON strings. 176This setting has no effect when decoding JSON strings.
158 177
159=item $json = $json->space_before ($enable) 178=item $json = $json->space_before ([$enable])
160 179
161If C<$enable> is true, then the C<encode> method will add an extra 180If C<$enable> is true (or missing), then the C<encode> method will add an extra
162optional space before the C<:> separating keys from values in JSON objects. 181optional space before the C<:> separating keys from values in JSON objects.
163 182
164If C<$enable> is false, then the C<encode> method will not add any extra 183If C<$enable> is false, then the C<encode> method will not add any extra
165space at those places. 184space at those places.
166 185
167This setting has no effect when decoding JSON strings. You will also most 186This setting has no effect when decoding JSON strings. You will also most
168likely combine this setting with C<space_after>. 187likely combine this setting with C<space_after>.
169 188
170=item $json = $json->space_after ($enable) 189=item $json = $json->space_after ([$enable])
171 190
172If C<$enable> is true, then the C<encode> method will add an extra 191If C<$enable> is true (or missing), then the C<encode> method will add an extra
173optional space after the C<:> separating keys from values in JSON objects 192optional space after the C<:> separating keys from values in JSON objects
174and extra whitespace after the C<,> separating key-value pairs and array 193and extra whitespace after the C<,> separating key-value pairs and array
175members. 194members.
176 195
177If C<$enable> is false, then the C<encode> method will not add any extra 196If C<$enable> is false, then the C<encode> method will not add any extra
178space at those places. 197space at those places.
179 198
180This setting has no effect when decoding JSON strings. 199This setting has no effect when decoding JSON strings.
181 200
182=item $json = $json->canonical ($enable) 201=item $json = $json->canonical ([$enable])
183 202
184If C<$enable> is true, then the C<encode> method will output JSON objects 203If C<$enable> is true (or missing), then the C<encode> method will output JSON objects
185by sorting their keys. This is adding a comparatively high overhead. 204by sorting their keys. This is adding a comparatively high overhead.
186 205
187If C<$enable> is false, then the C<encode> method will output key-value 206If C<$enable> is false, then the C<encode> method will output key-value
188pairs in the order Perl stores them (which will likely change between runs 207pairs in the order Perl stores them (which will likely change between runs
189of the same script). 208of the same script).
192the same JSON string (given the same overall settings). If it is disabled, 211the same JSON string (given the same overall settings). If it is disabled,
193the same hash migh be encoded differently even if contains the same data, 212the same hash migh be encoded differently even if contains the same data,
194as key-value pairs have no inherent ordering in Perl. 213as key-value pairs have no inherent ordering in Perl.
195 214
196This setting has no effect when decoding JSON strings. 215This setting has no effect when decoding JSON strings.
216
217=item $json = $json->allow_nonref ([$enable])
218
219If C<$enable> is true (or missing), then the C<encode> method can convert a
220non-reference into its corresponding string, number or null JSON value,
221which is an extension to RFC4627. Likewise, C<decode> will accept those JSON
222values instead of croaking.
223
224If C<$enable> is false, then the C<encode> method will croak if it isn't
225passed an arrayref or hashref, as JSON strings must either be an object
226or array. Likewise, C<decode> will croak if given something that is not a
227JSON object or array.
228
229=item $json = $json->shrink ([$enable])
230
231Perl usually over-allocates memory a bit when allocating space for
232strings. This flag optionally resizes strings generated by either
233C<encode> or C<decode> to their minimum size possible. This can save
234memory when your JSON strings are either very very long or you have many
235short strings. It will also try to downgrade any strings to octet-form
236if possible: perl stores strings internally either in an encoding called
237UTF-X or in octet-form. The latter cannot store everything but uses less
238space in general.
239
240If C<$enable> is true (or missing), the string returned by C<encode> will be shrunk-to-fit,
241while all strings generated by C<decode> will also be shrunk-to-fit.
242
243If C<$enable> is false, then the normal perl allocation algorithms are used.
244If you work with your data, then this is likely to be faster.
245
246In the future, this setting might control other things, such as converting
247strings that look like integers or floats into integers or floats
248internally (there is no difference on the Perl level), saving space.
197 249
198=item $json_string = $json->encode ($perl_scalar) 250=item $json_string = $json->encode ($perl_scalar)
199 251
200Converts the given Perl data structure (a simple scalar or a reference 252Converts the given Perl data structure (a simple scalar or a reference
201to a hash or array) to its JSON representation. Simple scalars will be 253to a hash or array) to its JSON representation. Simple scalars will be
213Perl arrayrefs and JSON objects become Perl hashrefs. C<true> becomes 265Perl arrayrefs and JSON objects become Perl hashrefs. C<true> becomes
214C<1>, C<false> becomes C<0> and C<null> becomes C<undef>. 266C<1>, C<false> becomes C<0> and C<null> becomes C<undef>.
215 267
216=back 268=back
217 269
270=head1 MAPPING
271
272This section describes how JSON::XS maps Perl values to JSON values and
273vice versa. These mappings are designed to "do the right thing" in most
274circumstances automatically, preserving round-tripping characteristics
275(what you put in comes out as something equivalent).
276
277For the more enlightened: note that in the following descriptions,
278lowercase I<perl> refers to the Perl interpreter, while uppcercase I<Perl>
279refers to the abstract Perl language itself.
280
281=head2 JSON -> PERL
282
283=over 4
284
285=item object
286
287A JSON object becomes a reference to a hash in Perl. No ordering of object
288keys is preserved.
289
290=item array
291
292A JSON array becomes a reference to an array in Perl.
293
294=item string
295
296A JSON string becomes a string scalar in Perl - Unicode codepoints in JSON
297are represented by the same codepoints in the Perl string, so no manual
298decoding is necessary.
299
300=item number
301
302A JSON number becomes either an integer or numeric (floating point)
303scalar in perl, depending on its range and any fractional parts. On the
304Perl level, there is no difference between those as Perl handles all the
305conversion details, but an integer may take slightly less memory and might
306represent more values exactly than (floating point) numbers.
307
308=item true, false
309
310These JSON atoms become C<0>, C<1>, respectively. Information is lost in
311this process. Future versions might represent those values differently,
312but they will be guarenteed to act like these integers would normally in
313Perl.
314
315=item null
316
317A JSON null atom becomes C<undef> in Perl.
318
319=back
320
321=head2 PERL -> JSON
322
323The mapping from Perl to JSON is slightly more difficult, as Perl is a
324truly typeless language, so we can only guess which JSON type is meant by
325a Perl value.
326
327=over 4
328
329=item hash references
330
331Perl hash references become JSON objects. As there is no inherent ordering
332in hash keys, they will usually be encoded in a pseudo-random order that
333can change between runs of the same program but stays generally the same
334within the single run of a program. JSON::XS can optionally sort the hash
335keys (determined by the I<canonical> flag), so the same datastructure
336will serialise to the same JSON text (given same settings and version of
337JSON::XS), but this incurs a runtime overhead.
338
339=item array references
340
341Perl array references become JSON arrays.
342
343=item blessed objects
344
345Blessed objects are not allowed. JSON::XS currently tries to encode their
346underlying representation (hash- or arrayref), but this behaviour might
347change in future versions.
348
349=item simple scalars
350
351Simple Perl scalars (any scalar that is not a reference) are the most
352difficult objects to encode: JSON::XS will encode undefined scalars as
353JSON null value, scalars that have last been used in a string context
354before encoding as JSON strings and anything else as number value:
355
356 # dump as number
357 to_json [2] # yields [2]
358 to_json [-3.0e17] # yields [-3e+17]
359 my $value = 5; to_json [$value] # yields [5]
360
361 # used as string, so dump as string
362 print $value;
363 to_json [$value] # yields ["5"]
364
365 # undef becomes null
366 to_json [undef] # yields [null]
367
368You can force the type to be a string by stringifying it:
369
370 my $x = 3.1; # some variable containing a number
371 "$x"; # stringified
372 $x .= ""; # another, more awkward way to stringify
373 print $x; # perl does it for you, too, quite often
374
375You can force the type to be a number by numifying it:
376
377 my $x = "3"; # some variable containing a string
378 $x += 0; # numify it, ensuring it will be dumped as a number
379 $x *= 1; # same thing, the choise is yours.
380
381You can not currently output JSON booleans or force the type in other,
382less obscure, ways. Tell me if you need this capability.
383
384=back
385
386=head1 COMPARISON
387
388As already mentioned, this module was created because none of the existing
389JSON modules could be made to work correctly. First I will describe the
390problems (or pleasures) I encountered with various existing JSON modules,
391followed by some benchmark values. JSON::XS was designed not to suffer
392from any of these problems or limitations.
393
394=over 4
395
396=item JSON 1.07
397
398Slow (but very portable, as it is written in pure Perl).
399
400Undocumented/buggy Unicode handling (how JSON handles unicode values is
401undocumented. One can get far by feeding it unicode strings and doing
402en-/decoding oneself, but unicode escapes are not working properly).
403
404No roundtripping (strings get clobbered if they look like numbers, e.g.
405the string C<2.0> will encode to C<2.0> instead of C<"2.0">, and that will
406decode into the number 2.
407
408=item JSON::PC 0.01
409
410Very fast.
411
412Undocumented/buggy Unicode handling.
413
414No roundtripping.
415
416Has problems handling many Perl values (e.g. regex results and other magic
417values will make it croak).
418
419Does not even generate valid JSON (C<{1,2}> gets converted to C<{1:2}>
420which is not a valid JSON string.
421
422Unmaintained (maintainer unresponsive for many months, bugs are not
423getting fixed).
424
425=item JSON::Syck 0.21
426
427Very buggy (often crashes).
428
429Very inflexible (no human-readable format supported, format pretty much
430undocumented. I need at least a format for easy reading by humans and a
431single-line compact format for use in a protocol, and preferably a way to
432generate ASCII-only JSON strings).
433
434Completely broken (and confusingly documented) Unicode handling (unicode
435escapes are not working properly, you need to set ImplicitUnicode to
436I<different> values on en- and decoding to get symmetric behaviour).
437
438No roundtripping (simple cases work, but this depends on wether the scalar
439value was used in a numeric context or not).
440
441Dumping hashes may skip hash values depending on iterator state.
442
443Unmaintained (maintainer unresponsive for many months, bugs are not
444getting fixed).
445
446Does not check input for validity (i.e. will accept non-JSON input and
447return "something" instead of raising an exception. This is a security
448issue: imagine two banks transfering money between each other using
449JSON. One bank might parse a given non-JSON request and deduct money,
450while the other might reject the transaction with a syntax error. While a
451good protocol will at least recover, that is extra unnecessary work and
452the transaction will still not succeed).
453
454=item JSON::DWIW 0.04
455
456Very fast. Very natural. Very nice.
457
458Undocumented unicode handling (but the best of the pack. Unicode escapes
459still don't get parsed properly).
460
461Very inflexible.
462
463No roundtripping.
464
465Does not generate valid JSON (key strings are often unquoted, empty keys
466result in nothing being output)
467
468Does not check input for validity.
469
470=back
471
472=head2 SPEED
473
474It seems that JSON::XS is surprisingly fast, as shown in the following
475tables. They have been generated with the help of the C<eg/bench> program
476in the JSON::XS distribution, to make it easy to compare on your own
477system.
478
479First is a comparison between various modules using a very simple JSON
480string, showing the number of encodes/decodes per second (JSON::XS is
481the functional interface, while JSON::XS/2 is the OO interface with
482pretty-printing and hashkey sorting enabled).
483
484 module | encode | decode |
485 -----------|------------|------------|
486 JSON | 14006 | 6820 |
487 JSON::DWIW | 200937 | 120386 |
488 JSON::PC | 85065 | 129366 |
489 JSON::Syck | 59898 | 44232 |
490 JSON::XS | 1171478 | 342435 |
491 JSON::XS/2 | 730760 | 328714 |
492 -----------+------------+------------+
493
494That is, JSON::XS is 6 times faster than than JSON::DWIW and about 80
495times faster than JSON, even with pretty-printing and key sorting.
496
497Using a longer test string (roughly 8KB, generated from Yahoo! Locals
498search API (http://nanoref.com/yahooapis/mgPdGg):
499
500 module | encode | decode |
501 -----------|------------|------------|
502 JSON | 673 | 38 |
503 JSON::DWIW | 5271 | 770 |
504 JSON::PC | 9901 | 2491 |
505 JSON::Syck | 2360 | 786 |
506 JSON::XS | 37398 | 3202 |
507 JSON::XS/2 | 13765 | 3153 |
508 -----------+------------+------------+
509
510Again, JSON::XS leads by far in the encoding case, while still beating
511every other module in the decoding case.
512
513Last example is an almost 8MB large hash with many large binary values
514(PNG files), resulting in a lot of escaping:
515
516=head1 BUGS
517
518While the goal of this module is to be correct, that unfortunately does
519not mean its bug-free, only that I think its design is bug-free. It is
520still very young and not well-tested. If you keep reporting bugs they will
521be fixed swiftly, though.
522
218=cut 523=cut
219 524
2201; 5251;
221 526
222=head1 AUTHOR 527=head1 AUTHOR

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines