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

6 6
7 use JSON::XS; 7 use JSON::XS;
8 8
9=head1 DESCRIPTION 9=head1 DESCRIPTION
10 10
11This module converts Perl data structures to JSON and vice versa. Its
12primary goal is to be I<correct> and its secondary goal is to be
13I<fast>. To reach the latter goal it was written in C.
14
15As this is the n-th-something JSON module on CPAN, what was the reason
16to write yet another JSON module? While it seems there are many JSON
17modules, none of them correctly handle all corner cases, and in most cases
18their maintainers are unresponsive, gone missing, or not listening to bug
19reports for other reasons.
20
21See COMPARISON, below, for a comparison to some other JSON modules.
22
23See MAPPING, below, on how JSON::XS maps perl values to JSON values and
24vice versa.
25
26=head2 FEATURES
27
11=over 4 28=over 4
12 29
30=item * correct handling of unicode issues
31
32This module knows how to handle Unicode, and even documents how and when
33it does so.
34
35=item * round-trip integrity
36
37When you serialise a perl data structure using only datatypes supported
38by JSON, the deserialised data structure is identical on the Perl level.
39(e.g. the string "2.0" doesn't suddenly become "2").
40
41=item * strict checking of JSON correctness
42
43There is no guessing, no generating of illegal JSON strings by default,
44and only JSON is accepted as input by default (the latter is a security
45feature).
46
47=item * fast
48
49Compared to other JSON modules, this module compares favourably in terms
50of speed, too.
51
52=item * simple to use
53
54This module has both a simple functional interface as well as an OO
55interface.
56
57=item * reasonably versatile output formats
58
59You can choose between the most compact guarenteed single-line format
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
63whatever way you like.
64
65=back
66
13=cut 67=cut
14 68
15package JSON::XS; 69package JSON::XS;
16 70
17BEGIN { 71BEGIN {
18 $VERSION = '0.1'; 72 $VERSION = '0.3';
19 @ISA = qw(Exporter); 73 @ISA = qw(Exporter);
20 74
75 @EXPORT = qw(to_json from_json);
21 require Exporter; 76 require Exporter;
22 77
23 require XSLoader; 78 require XSLoader;
24 XSLoader::load JSON::XS::, $VERSION; 79 XSLoader::load JSON::XS::, $VERSION;
25} 80}
26 81
27=item 82=head1 FUNCTIONAL INTERFACE
83
84The following convinience methods are provided by this module. They are
85exported by default:
86
87=over 4
88
89=item $json_string = to_json $perl_scalar
90
91Converts the given Perl data structure (a simple scalar or a reference to
92a hash or array) to a UTF-8 encoded, binary string (that is, the string contains
93octets only). Croaks on error.
94
95This function call is functionally identical to C<< JSON::XS->new->utf8->encode ($perl_scalar) >>.
96
97=item $perl_scalar = from_json $json_string
98
99The opposite of C<to_json>: expects an UTF-8 (binary) string and tries to
100parse that as an UTF-8 encoded JSON string, returning the resulting simple
101scalar or reference. Croaks on error.
102
103This function call is functionally identical to C<< JSON::XS->new->utf8->decode ($json_string) >>.
104
105=back
106
107=head1 OBJECT-ORIENTED INTERFACE
108
109The object oriented interface lets you configure your own encoding or
110decoding style, within the limits of supported formats.
111
112=over 4
113
114=item $json = new JSON::XS
115
116Creates a new JSON::XS object that can be used to de/encode JSON
117strings. All boolean flags described below are by default I<disabled>.
118
119The mutators for flags all return the JSON object again and thus calls can
120be chained:
121
122 my $json = JSON::XS->new->utf8(1)->space_after(1)->encode ({a => [1,2]})
123 => {"a": [1, 2]}
124
125=item $json = $json->ascii ([$enable])
126
127If C<$enable> is true (or missing), then the C<encode> method will
128not generate characters outside the code range C<0..127>. Any unicode
129characters outside that range will be escaped using either a single
130\uXXXX (BMP characters) or a double \uHHHH\uLLLLL escape sequence, as per
131RFC4627.
132
133If C<$enable> is false, then the C<encode> method will not escape Unicode
134characters unless necessary.
135
136 JSON::XS->new->ascii (1)->encode (chr 0x10401)
137 => \ud801\udc01
138
139=item $json = $json->utf8 ([$enable])
140
141If C<$enable> is true (or missing), then the C<encode> method will encode
142the JSON string into UTF-8, as required by many protocols, while the
143C<decode> method expects to be handled an UTF-8-encoded string. Please
144note that UTF-8-encoded strings do not contain any characters outside the
145range C<0..255>, they are thus useful for bytewise/binary I/O.
146
147If C<$enable> is false, then the C<encode> method will return the JSON
148string as a (non-encoded) unicode string, while C<decode> expects thus a
149unicode string. Any decoding or encoding (e.g. to UTF-8 or UTF-16) needs
150to be done yourself, e.g. using the Encode module.
151
152=item $json = $json->pretty ([$enable])
153
154This enables (or disables) all of the C<indent>, C<space_before> and
155C<space_after> (and in the future possibly more) flags in one call to
156generate the most readable (or most compact) form possible.
157
158 my $json = JSON::XS->new->pretty(1)->encode ({a => [1,2]})
159 =>
160 {
161 "a" : [
162 1,
163 2
164 ]
165 }
166
167=item $json = $json->indent ([$enable])
168
169If C<$enable> is true (or missing), then the C<encode> method will use a multiline
170format as output, putting every array member or object/hash key-value pair
171into its own line, identing them properly.
172
173If C<$enable> is false, no newlines or indenting will be produced, and the
174resulting JSON strings is guarenteed not to contain any C<newlines>.
175
176This setting has no effect when decoding JSON strings.
177
178=item $json = $json->space_before ([$enable])
179
180If C<$enable> is true (or missing), then the C<encode> method will add an extra
181optional space before the C<:> separating keys from values in JSON objects.
182
183If C<$enable> is false, then the C<encode> method will not add any extra
184space at those places.
185
186This setting has no effect when decoding JSON strings. You will also most
187likely combine this setting with C<space_after>.
188
189=item $json = $json->space_after ([$enable])
190
191If C<$enable> is true (or missing), then the C<encode> method will add an extra
192optional space after the C<:> separating keys from values in JSON objects
193and extra whitespace after the C<,> separating key-value pairs and array
194members.
195
196If C<$enable> is false, then the C<encode> method will not add any extra
197space at those places.
198
199This setting has no effect when decoding JSON strings.
200
201=item $json = $json->canonical ([$enable])
202
203If C<$enable> is true (or missing), then the C<encode> method will output JSON objects
204by sorting their keys. This is adding a comparatively high overhead.
205
206If C<$enable> is false, then the C<encode> method will output key-value
207pairs in the order Perl stores them (which will likely change between runs
208of the same script).
209
210This option is useful if you want the same data structure to be encoded as
211the same JSON string (given the same overall settings). If it is disabled,
212the same hash migh be encoded differently even if contains the same data,
213as key-value pairs have no inherent ordering in Perl.
214
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.
249
250=item $json_string = $json->encode ($perl_scalar)
251
252Converts the given Perl data structure (a simple scalar or a reference
253to a hash or array) to its JSON representation. Simple scalars will be
254converted into JSON string or number sequences, while references to arrays
255become JSON arrays and references to hashes become JSON objects. Undefined
256Perl values (e.g. C<undef>) become JSON C<null> values. Neither C<true>
257nor C<false> values will be generated.
258
259=item $perl_scalar = $json->decode ($json_string)
260
261The opposite of C<encode>: expects a JSON string and tries to parse it,
262returning the resulting simple scalar or reference. Croaks on error.
263
264JSON numbers and strings become simple Perl scalars. JSON arrays become
265Perl arrayrefs and JSON objects become Perl hashrefs. C<true> becomes
266C<1>, C<false> becomes C<0> and C<null> becomes C<undef>.
267
268=back
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.
28 522
29=cut 523=cut
30 524
31use JSON::DWIW;
32use Benchmark;
33
34use utf8;
35#my $json = '{"ü":1,"a":[1,{"3":4},2],"b":5,"üü":2}';
36my $json = '{"test":9555555555555555555,"hu" : -1e+5, "arr" : [ 1,2,3,4,5]}';
37
38my $js = JSON::XS->new;
39warn $js->indent (0);
40warn $js->canonical (0);
41warn $js->ascii (0);
42warn $js->space_after (0);
43use Data::Dumper;
44warn Dumper $js->decode ($json);
45warn Dumper $js->encode ($js->decode ($json));
46#my $x = {"üü" => 2, "ü" => 1, "a" => [1,{3,4},2], b => 5};
47
48#my $js2 = JSON::DWIW->new;
49#
50#timethese 200000, {
51# a => sub { $js->encode ($x) },
52# b => sub { $js2->to_json ($x) },
53#};
54
551; 5251;
56
57=back
58 526
59=head1 AUTHOR 527=head1 AUTHOR
60 528
61 Marc Lehmann <schmorp@schmorp.de> 529 Marc Lehmann <schmorp@schmorp.de>
62 http://home.schmorp.de/ 530 http://home.schmorp.de/

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines