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.69 by root, Tue Oct 23 03:31:14 2007 UTC vs.
Revision 1.77 by root, Tue Dec 4 10:37:42 2007 UTC

19 19
20 $coder = JSON::XS->new->ascii->pretty->allow_nonref; 20 $coder = JSON::XS->new->ascii->pretty->allow_nonref;
21 $pretty_printed_unencoded = $coder->encode ($perl_scalar); 21 $pretty_printed_unencoded = $coder->encode ($perl_scalar);
22 $perl_scalar = $coder->decode ($unicode_json_text); 22 $perl_scalar = $coder->decode ($unicode_json_text);
23 23
24 # Note that JSON version 2.0 and above will automatically use JSON::XS
25 # if available, at virtually no speed overhead either, so you should
26 # be able to just:
27
28 use JSON;
29
30 # and do the same things, except that you have a pure-perl fallback now.
31
24=head1 DESCRIPTION 32=head1 DESCRIPTION
25 33
26This module converts Perl data structures to JSON and vice versa. Its 34This module converts Perl data structures to JSON and vice versa. Its
27primary goal is to be I<correct> and its secondary goal is to be 35primary goal is to be I<correct> and its secondary goal is to be
28I<fast>. To reach the latter goal it was written in C. 36I<fast>. To reach the latter goal it was written in C.
37
38Beginning with version 2.0 of the JSON module, when both JSON and
39JSON::XS are installed, then JSON will fall back on JSON::XS (this can be
40overriden) with no overhead due to emulation (by inheritign constructor
41and methods). If JSON::XS is not available, it will fall back to the
42compatible JSON::PP module as backend, so using JSON instead of JSON::XS
43gives you a portable JSON API that can be fast when you need and doesn't
44require a C compiler when that is a problem.
29 45
30As this is the n-th-something JSON module on CPAN, what was the reason 46As this is the n-th-something JSON module on CPAN, what was the reason
31to write yet another JSON module? While it seems there are many JSON 47to write yet another JSON module? While it seems there are many JSON
32modules, none of them correctly handle all corner cases, and in most cases 48modules, none of them correctly handle all corner cases, and in most cases
33their maintainers are unresponsive, gone missing, or not listening to bug 49their maintainers are unresponsive, gone missing, or not listening to bug
84 100
85package JSON::XS; 101package JSON::XS;
86 102
87use strict; 103use strict;
88 104
89our $VERSION = '1.52'; 105our $VERSION = '2.0';
90our @ISA = qw(Exporter); 106our @ISA = qw(Exporter);
91 107
92our @EXPORT = qw(to_json from_json); 108our @EXPORT = qw(to_json from_json);
93 109
94use Exporter; 110use Exporter;
201 217
202 my $json = JSON::XS->new->utf8->space_after->encode ({a => [1,2]}) 218 my $json = JSON::XS->new->utf8->space_after->encode ({a => [1,2]})
203 => {"a": [1, 2]} 219 => {"a": [1, 2]}
204 220
205=item $json = $json->ascii ([$enable]) 221=item $json = $json->ascii ([$enable])
222
223=item $enabled = $json->get_ascii
206 224
207If C<$enable> is true (or missing), then the C<encode> method will not 225If C<$enable> is true (or missing), then the C<encode> method will not
208generate characters outside the code range C<0..127> (which is ASCII). Any 226generate characters outside the code range C<0..127> (which is ASCII). Any
209Unicode characters outside that range will be escaped using either a 227Unicode characters outside that range will be escaped using either a
210single \uXXXX (BMP characters) or a double \uHHHH\uLLLLL escape sequence, 228single \uXXXX (BMP characters) or a double \uHHHH\uLLLLL escape sequence,
223 JSON::XS->new->ascii (1)->encode ([chr 0x10401]) 241 JSON::XS->new->ascii (1)->encode ([chr 0x10401])
224 => ["\ud801\udc01"] 242 => ["\ud801\udc01"]
225 243
226=item $json = $json->latin1 ([$enable]) 244=item $json = $json->latin1 ([$enable])
227 245
246=item $enabled = $json->get_latin1
247
228If C<$enable> is true (or missing), then the C<encode> method will encode 248If C<$enable> is true (or missing), then the C<encode> method will encode
229the resulting JSON text as latin1 (or iso-8859-1), escaping any characters 249the resulting JSON text as latin1 (or iso-8859-1), escaping any characters
230outside the code range C<0..255>. The resulting string can be treated as a 250outside the code range C<0..255>. The resulting string can be treated as a
231latin1-encoded JSON text or a native Unicode string. The C<decode> method 251latin1-encoded JSON text or a native Unicode string. The C<decode> method
232will not be affected in any way by this flag, as C<decode> by default 252will not be affected in any way by this flag, as C<decode> by default
245 265
246 JSON::XS->new->latin1->encode (["\x{89}\x{abc}"] 266 JSON::XS->new->latin1->encode (["\x{89}\x{abc}"]
247 => ["\x{89}\\u0abc"] # (perl syntax, U+abc escaped, U+89 not) 267 => ["\x{89}\\u0abc"] # (perl syntax, U+abc escaped, U+89 not)
248 268
249=item $json = $json->utf8 ([$enable]) 269=item $json = $json->utf8 ([$enable])
270
271=item $enabled = $json->get_utf8
250 272
251If C<$enable> is true (or missing), then the C<encode> method will encode 273If C<$enable> is true (or missing), then the C<encode> method will encode
252the JSON result into UTF-8, as required by many protocols, while the 274the JSON result into UTF-8, as required by many protocols, while the
253C<decode> method expects to be handled an UTF-8-encoded string. Please 275C<decode> method expects to be handled an UTF-8-encoded string. Please
254note that UTF-8-encoded strings do not contain any characters outside the 276note that UTF-8-encoded strings do not contain any characters outside the
288 ] 310 ]
289 } 311 }
290 312
291=item $json = $json->indent ([$enable]) 313=item $json = $json->indent ([$enable])
292 314
315=item $enabled = $json->get_indent
316
293If C<$enable> is true (or missing), then the C<encode> method will use a multiline 317If C<$enable> is true (or missing), then the C<encode> method will use a multiline
294format as output, putting every array member or object/hash key-value pair 318format as output, putting every array member or object/hash key-value pair
295into its own line, indenting them properly. 319into its own line, indenting them properly.
296 320
297If C<$enable> is false, no newlines or indenting will be produced, and the 321If C<$enable> is false, no newlines or indenting will be produced, and the
299 323
300This setting has no effect when decoding JSON texts. 324This setting has no effect when decoding JSON texts.
301 325
302=item $json = $json->space_before ([$enable]) 326=item $json = $json->space_before ([$enable])
303 327
328=item $enabled = $json->get_space_before
329
304If C<$enable> is true (or missing), then the C<encode> method will add an extra 330If C<$enable> is true (or missing), then the C<encode> method will add an extra
305optional space before the C<:> separating keys from values in JSON objects. 331optional space before the C<:> separating keys from values in JSON objects.
306 332
307If C<$enable> is false, then the C<encode> method will not add any extra 333If C<$enable> is false, then the C<encode> method will not add any extra
308space at those places. 334space at those places.
313Example, space_before enabled, space_after and indent disabled: 339Example, space_before enabled, space_after and indent disabled:
314 340
315 {"key" :"value"} 341 {"key" :"value"}
316 342
317=item $json = $json->space_after ([$enable]) 343=item $json = $json->space_after ([$enable])
344
345=item $enabled = $json->get_space_after
318 346
319If C<$enable> is true (or missing), then the C<encode> method will add an extra 347If C<$enable> is true (or missing), then the C<encode> method will add an extra
320optional space after the C<:> separating keys from values in JSON objects 348optional space after the C<:> separating keys from values in JSON objects
321and extra whitespace after the C<,> separating key-value pairs and array 349and extra whitespace after the C<,> separating key-value pairs and array
322members. 350members.
329Example, space_before and indent disabled, space_after enabled: 357Example, space_before and indent disabled, space_after enabled:
330 358
331 {"key": "value"} 359 {"key": "value"}
332 360
333=item $json = $json->relaxed ([$enable]) 361=item $json = $json->relaxed ([$enable])
362
363=item $enabled = $json->get_relaxed
334 364
335If C<$enable> is true (or missing), then C<decode> will accept some 365If C<$enable> is true (or missing), then C<decode> will accept some
336extensions to normal JSON syntax (see below). C<encode> will not be 366extensions to normal JSON syntax (see below). C<encode> will not be
337affected in anyway. I<Be aware that this option makes you accept invalid 367affected in anyway. I<Be aware that this option makes you accept invalid
338JSON texts as if they were valid!>. I suggest only to use this option to 368JSON texts as if they were valid!>. I suggest only to use this option to
375 405
376=back 406=back
377 407
378=item $json = $json->canonical ([$enable]) 408=item $json = $json->canonical ([$enable])
379 409
410=item $enabled = $json->get_canonical
411
380If C<$enable> is true (or missing), then the C<encode> method will output JSON objects 412If C<$enable> is true (or missing), then the C<encode> method will output JSON objects
381by sorting their keys. This is adding a comparatively high overhead. 413by sorting their keys. This is adding a comparatively high overhead.
382 414
383If C<$enable> is false, then the C<encode> method will output key-value 415If C<$enable> is false, then the C<encode> method will output key-value
384pairs in the order Perl stores them (which will likely change between runs 416pairs in the order Perl stores them (which will likely change between runs
391 423
392This setting has no effect when decoding JSON texts. 424This setting has no effect when decoding JSON texts.
393 425
394=item $json = $json->allow_nonref ([$enable]) 426=item $json = $json->allow_nonref ([$enable])
395 427
428=item $enabled = $json->get_allow_nonref
429
396If C<$enable> is true (or missing), then the C<encode> method can convert a 430If C<$enable> is true (or missing), then the C<encode> method can convert a
397non-reference into its corresponding string, number or null JSON value, 431non-reference into its corresponding string, number or null JSON value,
398which is an extension to RFC4627. Likewise, C<decode> will accept those JSON 432which is an extension to RFC4627. Likewise, C<decode> will accept those JSON
399values instead of croaking. 433values instead of croaking.
400 434
409 JSON::XS->new->allow_nonref->encode ("Hello, World!") 443 JSON::XS->new->allow_nonref->encode ("Hello, World!")
410 => "Hello, World!" 444 => "Hello, World!"
411 445
412=item $json = $json->allow_blessed ([$enable]) 446=item $json = $json->allow_blessed ([$enable])
413 447
448=item $enabled = $json->get_allow_blessed
449
414If C<$enable> is true (or missing), then the C<encode> method will not 450If C<$enable> is true (or missing), then the C<encode> method will not
415barf when it encounters a blessed reference. Instead, the value of the 451barf when it encounters a blessed reference. Instead, the value of the
416B<convert_blessed> option will decide whether C<null> (C<convert_blessed> 452B<convert_blessed> option will decide whether C<null> (C<convert_blessed>
417disabled or no C<to_json> method found) or a representation of the 453disabled or no C<TO_JSON> method found) or a representation of the
418object (C<convert_blessed> enabled and C<to_json> method found) is being 454object (C<convert_blessed> enabled and C<TO_JSON> method found) is being
419encoded. Has no effect on C<decode>. 455encoded. Has no effect on C<decode>.
420 456
421If C<$enable> is false (the default), then C<encode> will throw an 457If C<$enable> is false (the default), then C<encode> will throw an
422exception when it encounters a blessed object. 458exception when it encounters a blessed object.
423 459
424=item $json = $json->convert_blessed ([$enable]) 460=item $json = $json->convert_blessed ([$enable])
461
462=item $enabled = $json->get_convert_blessed
425 463
426If C<$enable> is true (or missing), then C<encode>, upon encountering a 464If C<$enable> is true (or missing), then C<encode>, upon encountering a
427blessed object, will check for the availability of the C<TO_JSON> method 465blessed object, will check for the availability of the C<TO_JSON> method
428on the object's class. If found, it will be called in scalar context 466on the object's class. If found, it will be called in scalar context
429and the resulting scalar will be encoded instead of the object. If no 467and the resulting scalar will be encoded instead of the object. If no
520 558
521 { __widget__ => $self->{id} } 559 { __widget__ => $self->{id} }
522 } 560 }
523 561
524=item $json = $json->shrink ([$enable]) 562=item $json = $json->shrink ([$enable])
563
564=item $enabled = $json->get_shrink
525 565
526Perl usually over-allocates memory a bit when allocating space for 566Perl usually over-allocates memory a bit when allocating space for
527strings. This flag optionally resizes strings generated by either 567strings. This flag optionally resizes strings generated by either
528C<encode> or C<decode> to their minimum size possible. This can save 568C<encode> or C<decode> to their minimum size possible. This can save
529memory when your JSON texts are either very very long or you have many 569memory when your JSON texts are either very very long or you have many
547strings that look like integers or floats into integers or floats 587strings that look like integers or floats into integers or floats
548internally (there is no difference on the Perl level), saving space. 588internally (there is no difference on the Perl level), saving space.
549 589
550=item $json = $json->max_depth ([$maximum_nesting_depth]) 590=item $json = $json->max_depth ([$maximum_nesting_depth])
551 591
592=item $max_depth = $json->get_max_depth
593
552Sets the maximum nesting level (default C<512>) accepted while encoding 594Sets the maximum nesting level (default C<512>) accepted while encoding
553or decoding. If the JSON text or Perl data structure has an equal or 595or decoding. If the JSON text or Perl data structure has an equal or
554higher nesting level then this limit, then the encoder and decoder will 596higher nesting level then this limit, then the encoder and decoder will
555stop and croak at that point. 597stop and croak at that point.
556 598
567used, which is rarely useful. 609used, which is rarely useful.
568 610
569See SECURITY CONSIDERATIONS, below, for more info on why this is useful. 611See SECURITY CONSIDERATIONS, below, for more info on why this is useful.
570 612
571=item $json = $json->max_size ([$maximum_string_size]) 613=item $json = $json->max_size ([$maximum_string_size])
614
615=item $max_size = $json->get_max_size
572 616
573Set the maximum length a JSON text may have (in bytes) where decoding is 617Set the maximum length a JSON text may have (in bytes) where decoding is
574being attempted. The default is C<0>, meaning no limit. When C<decode> 618being attempted. The default is C<0>, meaning no limit. When C<decode>
575is called on a string longer then this number of characters it will not 619is called on a string longer then this number of characters it will not
576attempt to decode the string but throw an exception. This setting has no 620attempt to decode the string but throw an exception. This setting has no
890It shows the number of encodes/decodes per second (JSON::XS uses 934It shows the number of encodes/decodes per second (JSON::XS uses
891the functional interface, while JSON::XS/2 uses the OO interface 935the functional interface, while JSON::XS/2 uses the OO interface
892with pretty-printing and hashkey sorting enabled, JSON::XS/3 enables 936with pretty-printing and hashkey sorting enabled, JSON::XS/3 enables
893shrink). Higher is better: 937shrink). Higher is better:
894 938
895 Storable | 15779.925 | 14169.946 |
896 -----------+------------+------------+
897 module | encode | decode | 939 module | encode | decode |
898 -----------|------------|------------| 940 -----------|------------|------------|
899 JSON | 4990.842 | 4088.813 | 941 JSON 1.x | 4990.842 | 4088.813 |
900 JSON::DWIW | 51653.990 | 71575.154 | 942 JSON::DWIW | 51653.990 | 71575.154 |
901 JSON::PC | 65948.176 | 74631.744 | 943 JSON::PC | 65948.176 | 74631.744 |
902 JSON::PP | 8931.652 | 3817.168 | 944 JSON::PP | 8931.652 | 3817.168 |
903 JSON::Syck | 24877.248 | 27776.848 | 945 JSON::Syck | 24877.248 | 27776.848 |
904 JSON::XS | 388361.481 | 227951.304 | 946 JSON::XS | 388361.481 | 227951.304 |
915Using a longer test string (roughly 18KB, generated from Yahoo! Locals 957Using a longer test string (roughly 18KB, generated from Yahoo! Locals
916search API (http://nanoref.com/yahooapis/mgPdGg): 958search API (http://nanoref.com/yahooapis/mgPdGg):
917 959
918 module | encode | decode | 960 module | encode | decode |
919 -----------|------------|------------| 961 -----------|------------|------------|
920 JSON | 55.260 | 34.971 | 962 JSON 1.x | 55.260 | 34.971 |
921 JSON::DWIW | 825.228 | 1082.513 | 963 JSON::DWIW | 825.228 | 1082.513 |
922 JSON::PC | 3571.444 | 2394.829 | 964 JSON::PC | 3571.444 | 2394.829 |
923 JSON::PP | 210.987 | 32.574 | 965 JSON::PP | 210.987 | 32.574 |
924 JSON::Syck | 552.551 | 787.544 | 966 JSON::Syck | 552.551 | 787.544 |
925 JSON::XS | 5780.463 | 4854.519 | 967 JSON::XS | 5780.463 | 4854.519 |

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines