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.71 by root, Sun Nov 25 14:23:57 2007 UTC vs.
Revision 1.74 by root, Wed Nov 28 13:57:15 2007 UTC

84 84
85package JSON::XS; 85package JSON::XS;
86 86
87use strict; 87use strict;
88 88
89our $VERSION = '1.53'; 89our $VERSION = '2.0';
90our @ISA = qw(Exporter); 90our @ISA = qw(Exporter);
91 91
92our @EXPORT = qw(to_json from_json); 92our @EXPORT = qw(to_json from_json);
93 93
94use Exporter; 94use Exporter;
201 201
202 my $json = JSON::XS->new->utf8->space_after->encode ({a => [1,2]}) 202 my $json = JSON::XS->new->utf8->space_after->encode ({a => [1,2]})
203 => {"a": [1, 2]} 203 => {"a": [1, 2]}
204 204
205=item $json = $json->ascii ([$enable]) 205=item $json = $json->ascii ([$enable])
206
207=item $enabled = $json->get_ascii
206 208
207If C<$enable> is true (or missing), then the C<encode> method will not 209If 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 210generate characters outside the code range C<0..127> (which is ASCII). Any
209Unicode characters outside that range will be escaped using either a 211Unicode characters outside that range will be escaped using either a
210single \uXXXX (BMP characters) or a double \uHHHH\uLLLLL escape sequence, 212single \uXXXX (BMP characters) or a double \uHHHH\uLLLLL escape sequence,
223 JSON::XS->new->ascii (1)->encode ([chr 0x10401]) 225 JSON::XS->new->ascii (1)->encode ([chr 0x10401])
224 => ["\ud801\udc01"] 226 => ["\ud801\udc01"]
225 227
226=item $json = $json->latin1 ([$enable]) 228=item $json = $json->latin1 ([$enable])
227 229
230=item $enabled = $json->get_latin1
231
228If C<$enable> is true (or missing), then the C<encode> method will encode 232If 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 233the 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 234outside 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 235latin1-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 236will not be affected in any way by this flag, as C<decode> by default
245 249
246 JSON::XS->new->latin1->encode (["\x{89}\x{abc}"] 250 JSON::XS->new->latin1->encode (["\x{89}\x{abc}"]
247 => ["\x{89}\\u0abc"] # (perl syntax, U+abc escaped, U+89 not) 251 => ["\x{89}\\u0abc"] # (perl syntax, U+abc escaped, U+89 not)
248 252
249=item $json = $json->utf8 ([$enable]) 253=item $json = $json->utf8 ([$enable])
254
255=item $enabled = $json->get_utf8
250 256
251If C<$enable> is true (or missing), then the C<encode> method will encode 257If 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 258the 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 259C<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 260note that UTF-8-encoded strings do not contain any characters outside the
288 ] 294 ]
289 } 295 }
290 296
291=item $json = $json->indent ([$enable]) 297=item $json = $json->indent ([$enable])
292 298
299=item $enabled = $json->get_indent
300
293If C<$enable> is true (or missing), then the C<encode> method will use a multiline 301If 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 302format as output, putting every array member or object/hash key-value pair
295into its own line, indenting them properly. 303into its own line, indenting them properly.
296 304
297If C<$enable> is false, no newlines or indenting will be produced, and the 305If C<$enable> is false, no newlines or indenting will be produced, and the
299 307
300This setting has no effect when decoding JSON texts. 308This setting has no effect when decoding JSON texts.
301 309
302=item $json = $json->space_before ([$enable]) 310=item $json = $json->space_before ([$enable])
303 311
312=item $enabled = $json->get_space_before
313
304If C<$enable> is true (or missing), then the C<encode> method will add an extra 314If 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. 315optional space before the C<:> separating keys from values in JSON objects.
306 316
307If C<$enable> is false, then the C<encode> method will not add any extra 317If C<$enable> is false, then the C<encode> method will not add any extra
308space at those places. 318space at those places.
313Example, space_before enabled, space_after and indent disabled: 323Example, space_before enabled, space_after and indent disabled:
314 324
315 {"key" :"value"} 325 {"key" :"value"}
316 326
317=item $json = $json->space_after ([$enable]) 327=item $json = $json->space_after ([$enable])
328
329=item $enabled = $json->get_space_after
318 330
319If C<$enable> is true (or missing), then the C<encode> method will add an extra 331If 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 332optional space after the C<:> separating keys from values in JSON objects
321and extra whitespace after the C<,> separating key-value pairs and array 333and extra whitespace after the C<,> separating key-value pairs and array
322members. 334members.
329Example, space_before and indent disabled, space_after enabled: 341Example, space_before and indent disabled, space_after enabled:
330 342
331 {"key": "value"} 343 {"key": "value"}
332 344
333=item $json = $json->relaxed ([$enable]) 345=item $json = $json->relaxed ([$enable])
346
347=item $enabled = $json->get_relaxed
334 348
335If C<$enable> is true (or missing), then C<decode> will accept some 349If C<$enable> is true (or missing), then C<decode> will accept some
336extensions to normal JSON syntax (see below). C<encode> will not be 350extensions to normal JSON syntax (see below). C<encode> will not be
337affected in anyway. I<Be aware that this option makes you accept invalid 351affected 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 352JSON texts as if they were valid!>. I suggest only to use this option to
375 389
376=back 390=back
377 391
378=item $json = $json->canonical ([$enable]) 392=item $json = $json->canonical ([$enable])
379 393
394=item $enabled = $json->get_canonical
395
380If C<$enable> is true (or missing), then the C<encode> method will output JSON objects 396If 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. 397by sorting their keys. This is adding a comparatively high overhead.
382 398
383If C<$enable> is false, then the C<encode> method will output key-value 399If 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 400pairs in the order Perl stores them (which will likely change between runs
391 407
392This setting has no effect when decoding JSON texts. 408This setting has no effect when decoding JSON texts.
393 409
394=item $json = $json->allow_nonref ([$enable]) 410=item $json = $json->allow_nonref ([$enable])
395 411
412=item $enabled = $json->get_allow_nonref
413
396If C<$enable> is true (or missing), then the C<encode> method can convert a 414If 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, 415non-reference into its corresponding string, number or null JSON value,
398which is an extension to RFC4627. Likewise, C<decode> will accept those JSON 416which is an extension to RFC4627. Likewise, C<decode> will accept those JSON
399values instead of croaking. 417values instead of croaking.
400 418
408 426
409 JSON::XS->new->allow_nonref->encode ("Hello, World!") 427 JSON::XS->new->allow_nonref->encode ("Hello, World!")
410 => "Hello, World!" 428 => "Hello, World!"
411 429
412=item $json = $json->allow_blessed ([$enable]) 430=item $json = $json->allow_blessed ([$enable])
431
432=item $enabled = $json->get_allow_bless
413 433
414If C<$enable> is true (or missing), then the C<encode> method will not 434If 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 435barf when it encounters a blessed reference. Instead, the value of the
416B<convert_blessed> option will decide whether C<null> (C<convert_blessed> 436B<convert_blessed> option will decide whether C<null> (C<convert_blessed>
417disabled or no C<to_json> method found) or a representation of the 437disabled or no C<to_json> method found) or a representation of the
420 440
421If C<$enable> is false (the default), then C<encode> will throw an 441If C<$enable> is false (the default), then C<encode> will throw an
422exception when it encounters a blessed object. 442exception when it encounters a blessed object.
423 443
424=item $json = $json->convert_blessed ([$enable]) 444=item $json = $json->convert_blessed ([$enable])
445
446=item $enabled = $json->get_convert_blessed
425 447
426If C<$enable> is true (or missing), then C<encode>, upon encountering a 448If 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 449blessed 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 450on 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 451and the resulting scalar will be encoded instead of the object. If no
520 542
521 { __widget__ => $self->{id} } 543 { __widget__ => $self->{id} }
522 } 544 }
523 545
524=item $json = $json->shrink ([$enable]) 546=item $json = $json->shrink ([$enable])
547
548=item $enabled = $json->get_shrink
525 549
526Perl usually over-allocates memory a bit when allocating space for 550Perl usually over-allocates memory a bit when allocating space for
527strings. This flag optionally resizes strings generated by either 551strings. This flag optionally resizes strings generated by either
528C<encode> or C<decode> to their minimum size possible. This can save 552C<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 553memory when your JSON texts are either very very long or you have many
547strings that look like integers or floats into integers or floats 571strings that look like integers or floats into integers or floats
548internally (there is no difference on the Perl level), saving space. 572internally (there is no difference on the Perl level), saving space.
549 573
550=item $json = $json->max_depth ([$maximum_nesting_depth]) 574=item $json = $json->max_depth ([$maximum_nesting_depth])
551 575
576=item $max_depth = $json->get_max_depth
577
552Sets the maximum nesting level (default C<512>) accepted while encoding 578Sets the maximum nesting level (default C<512>) accepted while encoding
553or decoding. If the JSON text or Perl data structure has an equal or 579or 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 580higher nesting level then this limit, then the encoder and decoder will
555stop and croak at that point. 581stop and croak at that point.
556 582
567used, which is rarely useful. 593used, which is rarely useful.
568 594
569See SECURITY CONSIDERATIONS, below, for more info on why this is useful. 595See SECURITY CONSIDERATIONS, below, for more info on why this is useful.
570 596
571=item $json = $json->max_size ([$maximum_string_size]) 597=item $json = $json->max_size ([$maximum_string_size])
598
599=item $max_size = $json->get_max_size
572 600
573Set the maximum length a JSON text may have (in bytes) where decoding is 601Set 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> 602being 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 603is 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 604attempt to decode the string but throw an exception. This setting has no
892with pretty-printing and hashkey sorting enabled, JSON::XS/3 enables 920with pretty-printing and hashkey sorting enabled, JSON::XS/3 enables
893shrink). Higher is better: 921shrink). Higher is better:
894 922
895 module | encode | decode | 923 module | encode | decode |
896 -----------|------------|------------| 924 -----------|------------|------------|
897 JSON | 4990.842 | 4088.813 | 925 JSON 1.x | 4990.842 | 4088.813 |
898 JSON::DWIW | 51653.990 | 71575.154 | 926 JSON::DWIW | 51653.990 | 71575.154 |
899 JSON::PC | 65948.176 | 74631.744 | 927 JSON::PC | 65948.176 | 74631.744 |
900 JSON::PP | 8931.652 | 3817.168 | 928 JSON::PP | 8931.652 | 3817.168 |
901 JSON::Syck | 24877.248 | 27776.848 | 929 JSON::Syck | 24877.248 | 27776.848 |
902 JSON::XS | 388361.481 | 227951.304 | 930 JSON::XS | 388361.481 | 227951.304 |
913Using a longer test string (roughly 18KB, generated from Yahoo! Locals 941Using a longer test string (roughly 18KB, generated from Yahoo! Locals
914search API (http://nanoref.com/yahooapis/mgPdGg): 942search API (http://nanoref.com/yahooapis/mgPdGg):
915 943
916 module | encode | decode | 944 module | encode | decode |
917 -----------|------------|------------| 945 -----------|------------|------------|
918 JSON | 55.260 | 34.971 | 946 JSON 1.x | 55.260 | 34.971 |
919 JSON::DWIW | 825.228 | 1082.513 | 947 JSON::DWIW | 825.228 | 1082.513 |
920 JSON::PC | 3571.444 | 2394.829 | 948 JSON::PC | 3571.444 | 2394.829 |
921 JSON::PP | 210.987 | 32.574 | 949 JSON::PP | 210.987 | 32.574 |
922 JSON::Syck | 552.551 | 787.544 | 950 JSON::Syck | 552.551 | 787.544 |
923 JSON::XS | 5780.463 | 4854.519 | 951 JSON::XS | 5780.463 | 4854.519 |

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines