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.141 by root, Fri Oct 25 19:53:08 2013 UTC vs.
Revision 1.144 by root, Mon Oct 28 23:19:54 2013 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines