… | |
… | |
81 | |
81 | |
82 | package JSON::XS; |
82 | package JSON::XS; |
83 | |
83 | |
84 | use strict; |
84 | use strict; |
85 | |
85 | |
86 | our $VERSION = '1.4'; |
86 | our $VERSION = '1.5'; |
87 | our @ISA = qw(Exporter); |
87 | our @ISA = qw(Exporter); |
88 | |
88 | |
89 | our @EXPORT = qw(to_json from_json); |
89 | our @EXPORT = qw(to_json from_json); |
90 | |
90 | |
91 | use Exporter; |
91 | use Exporter; |
… | |
… | |
277 | This setting has no effect when decoding JSON texts. |
277 | This setting has no effect when decoding JSON texts. |
278 | |
278 | |
279 | Example, space_before and indent disabled, space_after enabled: |
279 | Example, space_before and indent disabled, space_after enabled: |
280 | |
280 | |
281 | {"key": "value"} |
281 | {"key": "value"} |
|
|
282 | |
|
|
283 | =item $json = $json->relaxed ([$enable]) |
|
|
284 | |
|
|
285 | If C<$enable> is true (or missing), then C<decode> will accept some |
|
|
286 | extensions to normal JSON syntax (see below). C<encode> will not be |
|
|
287 | affected in anyway. I<Be aware that this option makes you accept invalid |
|
|
288 | JSON texts as if they were valid!>. I suggest only to use this option to |
|
|
289 | parse application-specific files written by humans (configuration files, |
|
|
290 | resource files etc.) |
|
|
291 | |
|
|
292 | If C<$enable> is false (the default), then C<decode> will only accept |
|
|
293 | valid JSON texts. |
|
|
294 | |
|
|
295 | Currently accepted extensions are: |
|
|
296 | |
|
|
297 | =over 4 |
|
|
298 | |
|
|
299 | =item * list items can have an end-comma |
|
|
300 | |
|
|
301 | JSON I<separates> array elements and key-value pairs with commas. This |
|
|
302 | can be annoying if you write JSON texts manually and want to be able to |
|
|
303 | quickly append elements, so this extension accepts comma at the end of |
|
|
304 | such items not just between them: |
|
|
305 | |
|
|
306 | [ |
|
|
307 | 1, |
|
|
308 | 2, <- this comma not normally allowed |
|
|
309 | ] |
|
|
310 | { |
|
|
311 | "k1": "v1", |
|
|
312 | "k2": "v2", <- this comma not normally allowed |
|
|
313 | } |
|
|
314 | |
|
|
315 | =item * shell-style '#'-comments |
|
|
316 | |
|
|
317 | Whenever JSON allows whitespace, shell-style comments are additionally |
|
|
318 | allowed. They are terminated by the first carriage-return or line-feed |
|
|
319 | character, after which more white-space and comments are allowed. |
|
|
320 | |
|
|
321 | [ |
|
|
322 | 1, # this comment not allowed in JSON |
|
|
323 | # neither this one... |
|
|
324 | ] |
|
|
325 | |
|
|
326 | =back |
282 | |
327 | |
283 | =item $json = $json->canonical ([$enable]) |
328 | =item $json = $json->canonical ([$enable]) |
284 | |
329 | |
285 | If C<$enable> is true (or missing), then the C<encode> method will output JSON objects |
330 | If C<$enable> is true (or missing), then the C<encode> method will output JSON objects |
286 | by sorting their keys. This is adding a comparatively high overhead. |
331 | by sorting their keys. This is adding a comparatively high overhead. |
… | |
… | |
553 | are represented by the same codepoints in the Perl string, so no manual |
598 | are represented by the same codepoints in the Perl string, so no manual |
554 | decoding is necessary. |
599 | decoding is necessary. |
555 | |
600 | |
556 | =item number |
601 | =item number |
557 | |
602 | |
558 | A JSON number becomes either an integer or numeric (floating point) |
603 | A JSON number becomes either an integer, numeric (floating point) or |
559 | scalar in perl, depending on its range and any fractional parts. On the |
604 | string scalar in perl, depending on its range and any fractional parts. On |
560 | Perl level, there is no difference between those as Perl handles all the |
605 | the Perl level, there is no difference between those as Perl handles all |
561 | conversion details, but an integer may take slightly less memory and might |
606 | the conversion details, but an integer may take slightly less memory and |
562 | represent more values exactly than (floating point) numbers. |
607 | might represent more values exactly than (floating point) numbers. |
|
|
608 | |
|
|
609 | If the number consists of digits only, JSON::XS will try to represent |
|
|
610 | it as an integer value. If that fails, it will try to represent it as |
|
|
611 | a numeric (floating point) value if that is possible without loss of |
|
|
612 | precision. Otherwise it will preserve the number as a string value. |
|
|
613 | |
|
|
614 | Numbers containing a fractional or exponential part will always be |
|
|
615 | represented as numeric (floating point) values, possibly at a loss of |
|
|
616 | precision. |
|
|
617 | |
|
|
618 | This might create round-tripping problems as numbers might become strings, |
|
|
619 | but as Perl is typeless there is no other way to do it. |
563 | |
620 | |
564 | =item true, false |
621 | =item true, false |
565 | |
622 | |
566 | These JSON atoms become C<JSON::XS::true> and C<JSON::XS::false>, |
623 | These JSON atoms become C<JSON::XS::true> and C<JSON::XS::false>, |
567 | respectively. They are overloaded to act almost exactly like the numbers |
624 | respectively. They are overloaded to act almost exactly like the numbers |
… | |
… | |
609 | to_json [\0,JSON::XS::true] # yields [false,true] |
666 | to_json [\0,JSON::XS::true] # yields [false,true] |
610 | |
667 | |
611 | =item JSON::XS::true, JSON::XS::false |
668 | =item JSON::XS::true, JSON::XS::false |
612 | |
669 | |
613 | These special values become JSON true and JSON false values, |
670 | These special values become JSON true and JSON false values, |
614 | respectively. You cna alos use C<\1> and C<\0> directly if you want. |
671 | respectively. You can also use C<\1> and C<\0> directly if you want. |
615 | |
672 | |
616 | =item blessed objects |
673 | =item blessed objects |
617 | |
674 | |
618 | Blessed objects are not allowed. JSON::XS currently tries to encode their |
675 | Blessed objects are not allowed. JSON::XS currently tries to encode their |
619 | underlying representation (hash- or arrayref), but this behaviour might |
676 | underlying representation (hash- or arrayref), but this behaviour might |