… | |
… | |
109 | our @EXPORT = qw(encode_json decode_json); |
109 | our @EXPORT = qw(encode_json decode_json); |
110 | |
110 | |
111 | use Exporter; |
111 | use Exporter; |
112 | use XSLoader; |
112 | use XSLoader; |
113 | |
113 | |
|
|
114 | use Types::Serialiser (); |
|
|
115 | |
114 | =head1 FUNCTIONAL INTERFACE |
116 | =head1 FUNCTIONAL INTERFACE |
115 | |
117 | |
116 | The following convenience methods are provided by this module. They are |
118 | The following convenience methods are provided by this module. They are |
117 | exported by default: |
119 | exported by default: |
118 | |
120 | |
… | |
… | |
138 | This function call is functionally identical to: |
140 | This function call is functionally identical to: |
139 | |
141 | |
140 | $perl_scalar = JSON::XS->new->utf8->decode ($json_text) |
142 | $perl_scalar = JSON::XS->new->utf8->decode ($json_text) |
141 | |
143 | |
142 | Except being faster. |
144 | Except being faster. |
143 | |
|
|
144 | =item $is_boolean = JSON::XS::is_bool $scalar |
|
|
145 | |
|
|
146 | Returns true if the passed scalar represents either JSON::XS::true or |
|
|
147 | JSON::XS::false, two constants that act like C<1> and C<0>, respectively |
|
|
148 | and are used to represent JSON C<true> and C<false> values in Perl. |
|
|
149 | |
|
|
150 | See MAPPING, below, for more information on how JSON values are mapped to |
|
|
151 | Perl. |
|
|
152 | |
145 | |
153 | =back |
146 | =back |
154 | |
147 | |
155 | |
148 | |
156 | =head1 A FEW NOTES ON UNICODE AND PERL |
149 | =head1 A FEW NOTES ON UNICODE AND PERL |
… | |
… | |
673 | when there is trailing garbage after the first JSON object, it will |
666 | when there is trailing garbage after the first JSON object, it will |
674 | silently stop parsing there and return the number of characters consumed |
667 | silently stop parsing there and return the number of characters consumed |
675 | so far. |
668 | so far. |
676 | |
669 | |
677 | This is useful if your JSON texts are not delimited by an outer protocol |
670 | This is useful if your JSON texts are not delimited by an outer protocol |
678 | (which is not the brightest thing to do in the first place) and you need |
|
|
679 | to know where the JSON text ends. |
671 | and you need to know where the JSON text ends. |
680 | |
672 | |
681 | JSON::XS->new->decode_prefix ("[1] the tail") |
673 | JSON::XS->new->decode_prefix ("[1] the tail") |
682 | => ([], 3) |
674 | => ([], 3) |
683 | |
675 | |
684 | =back |
676 | =back |
… | |
… | |
778 | =back |
770 | =back |
779 | |
771 | |
780 | =head2 LIMITATIONS |
772 | =head2 LIMITATIONS |
781 | |
773 | |
782 | All options that affect decoding are supported, except |
774 | All options that affect decoding are supported, except |
783 | C<allow_nonref>. The reason for this is that it cannot be made to |
775 | C<allow_nonref>. The reason for this is that it cannot be made to work |
784 | work sensibly: JSON objects and arrays are self-delimited, i.e. you can concatenate |
776 | sensibly: JSON objects and arrays are self-delimited, i.e. you can |
785 | them back to back and still decode them perfectly. This does not hold true |
777 | concatenate them back to back and still decode them perfectly. This does |
786 | for JSON numbers, however. |
778 | not hold true for JSON numbers, however. |
787 | |
779 | |
788 | For example, is the string C<1> a single JSON number, or is it simply the |
780 | For example, is the string C<1> a single JSON number, or is it simply the |
789 | start of C<12>? Or is C<12> a single JSON number, or the concatenation |
781 | start of C<12>? Or is C<12> a single JSON number, or the concatenation |
790 | of C<1> and C<2>? In neither case you can tell, and this is why JSON::XS |
782 | of C<1> and C<2>? In neither case you can tell, and this is why JSON::XS |
791 | takes the conservative route and disallows this case. |
783 | takes the conservative route and disallows this case. |
… | |
… | |
984 | floating point, JSON::XS only guarantees precision up to but not including |
976 | floating point, JSON::XS only guarantees precision up to but not including |
985 | the least significant bit. |
977 | the least significant bit. |
986 | |
978 | |
987 | =item true, false |
979 | =item true, false |
988 | |
980 | |
989 | These JSON atoms become C<JSON::XS::true> and C<JSON::XS::false>, |
981 | These JSON atoms become C<Types::Serialiser::true> and |
990 | respectively. They are overloaded to act almost exactly like the numbers |
982 | C<Types::Serialiser::false>, respectively. They are overloaded to act |
991 | C<1> and C<0>. You can check whether a scalar is a JSON boolean by using |
983 | almost exactly like the numbers C<1> and C<0>. You can check whether |
992 | the C<JSON::XS::is_bool> function. |
984 | a scalar is a JSON boolean by using the C<Types::Serialiser::is_bool> |
|
|
985 | function (after C<use Types::Serialier>, of course). |
993 | |
986 | |
994 | =item null |
987 | =item null |
995 | |
988 | |
996 | A JSON null atom becomes C<undef> in Perl. |
989 | A JSON null atom becomes C<undef> in Perl. |
997 | |
990 | |
… | |
… | |
1022 | |
1015 | |
1023 | =item other references |
1016 | =item other references |
1024 | |
1017 | |
1025 | Other unblessed references are generally not allowed and will cause an |
1018 | Other unblessed references are generally not allowed and will cause an |
1026 | exception to be thrown, except for references to the integers C<0> and |
1019 | exception to be thrown, except for references to the integers C<0> and |
1027 | C<1>, which get turned into C<false> and C<true> atoms in JSON. You can |
1020 | C<1>, which get turned into C<false> and C<true> atoms in JSON. |
1028 | also use C<JSON::XS::false> and C<JSON::XS::true> to improve readability. |
|
|
1029 | |
1021 | |
|
|
1022 | Since C<JSON::XS> uses the boolean model from L<Types::Serialiser>, you |
|
|
1023 | can also C<use Types::Serialiser> and then use C<Types::Serialiser::false> |
|
|
1024 | and C<Types::Serialiser::true> to improve readability. |
|
|
1025 | |
|
|
1026 | use Types::Serialiser; |
1030 | encode_json [\0, JSON::XS::true] # yields [false,true] |
1027 | encode_json [\0, Types::Serialiser::true] # yields [false,true] |
1031 | |
1028 | |
1032 | =item JSON::XS::true, JSON::XS::false |
1029 | =item Types::Serialiser::true, Types::Serialiser::false |
1033 | |
1030 | |
1034 | These special values become JSON true and JSON false values, |
1031 | These special values from the L<Types::Serialiser> module become JSON true |
1035 | respectively. You can also use C<\1> and C<\0> directly if you want. |
1032 | and JSON false values, respectively. You can also use C<\1> and C<\0> |
|
|
1033 | directly if you want. |
1036 | |
1034 | |
1037 | =item blessed objects |
1035 | =item blessed objects |
1038 | |
1036 | |
1039 | Blessed objects are not directly representable in JSON. See the |
1037 | Blessed objects are not directly representable in JSON. See the |
1040 | C<allow_blessed> and C<convert_blessed> methods on various options on |
1038 | C<allow_blessed> and C<convert_blessed> methods on various options on |
… | |
… | |
1416 | are browser design bugs, but it is still you who will have to deal with |
1414 | are browser design bugs, but it is still you who will have to deal with |
1417 | it, as major browser developers care only for features, not about getting |
1415 | it, as major browser developers care only for features, not about getting |
1418 | security right). |
1416 | security right). |
1419 | |
1417 | |
1420 | |
1418 | |
|
|
1419 | =head1 INTEROPERABILITY WITH OTHER MODULES |
|
|
1420 | |
|
|
1421 | C<JSON::XS> uses the L<Types::Serialiser> module to provide boolean |
|
|
1422 | constants. That means that the JSON true and false values will be |
|
|
1423 | comaptible to true and false values of iother modules that do the same, |
|
|
1424 | such as L<JSON::PP> and L<CBOR::XS>. |
|
|
1425 | |
|
|
1426 | |
1421 | =head1 THREADS |
1427 | =head1 THREADS |
1422 | |
1428 | |
1423 | This module is I<not> guaranteed to be thread safe and there are no |
1429 | This module is I<not> guaranteed to be thread safe and there are no |
1424 | plans to change this until Perl gets thread support (as opposed to the |
1430 | plans to change this until Perl gets thread support (as opposed to the |
1425 | horribly slow so-called "threads" which are simply slow and bloated |
1431 | horribly slow so-called "threads" which are simply slow and bloated |
… | |
… | |
1455 | Please refrain from using rt.cpan.org or any other bug reporting |
1461 | Please refrain from using rt.cpan.org or any other bug reporting |
1456 | service. I put the contact address into my modules for a reason. |
1462 | service. I put the contact address into my modules for a reason. |
1457 | |
1463 | |
1458 | =cut |
1464 | =cut |
1459 | |
1465 | |
1460 | our $true = do { bless \(my $dummy = 1), "JSON::XS::Boolean" }; |
1466 | BEGIN { |
1461 | our $false = do { bless \(my $dummy = 0), "JSON::XS::Boolean" }; |
1467 | *true = \$Types::Serialiser::true; |
|
|
1468 | *true = \&Types::Serialiser::true; |
|
|
1469 | *false = \$Types::Serialiser::false; |
|
|
1470 | *false = \&Types::Serialiser::false; |
|
|
1471 | *is_bool = \&Types::Serialiser::is_bool; |
1462 | |
1472 | |
1463 | sub true() { $true } |
1473 | *JSON::XS::Boolean:: = *Types::Serialiser::Boolean::; |
1464 | sub false() { $false } |
|
|
1465 | |
|
|
1466 | sub is_bool($) { |
|
|
1467 | UNIVERSAL::isa $_[0], "JSON::XS::Boolean" |
|
|
1468 | # or UNIVERSAL::isa $_[0], "JSON::Literal" |
|
|
1469 | } |
1474 | } |
1470 | |
1475 | |
1471 | XSLoader::load "JSON::XS", $VERSION; |
1476 | XSLoader::load "JSON::XS", $VERSION; |
1472 | |
|
|
1473 | package JSON::XS::Boolean; |
|
|
1474 | |
|
|
1475 | use overload |
|
|
1476 | "0+" => sub { ${$_[0]} }, |
|
|
1477 | "++" => sub { $_[0] = ${$_[0]} + 1 }, |
|
|
1478 | "--" => sub { $_[0] = ${$_[0]} - 1 }, |
|
|
1479 | fallback => 1; |
|
|
1480 | |
|
|
1481 | 1; |
|
|
1482 | |
1477 | |
1483 | =head1 SEE ALSO |
1478 | =head1 SEE ALSO |
1484 | |
1479 | |
1485 | The F<json_xs> command line utility for quick experiments. |
1480 | The F<json_xs> command line utility for quick experiments. |
1486 | |
1481 | |
… | |
… | |
1489 | Marc Lehmann <schmorp@schmorp.de> |
1484 | Marc Lehmann <schmorp@schmorp.de> |
1490 | http://home.schmorp.de/ |
1485 | http://home.schmorp.de/ |
1491 | |
1486 | |
1492 | =cut |
1487 | =cut |
1493 | |
1488 | |
|
|
1489 | 1 |
|
|
1490 | |