… | |
… | |
83 | this module usually compares favourably in terms of speed, too. |
83 | this module usually compares favourably in terms of speed, too. |
84 | |
84 | |
85 | =item * simple to use |
85 | =item * simple to use |
86 | |
86 | |
87 | This module has both a simple functional interface as well as an object |
87 | This module has both a simple functional interface as well as an object |
88 | oriented interface interface. |
88 | oriented interface. |
89 | |
89 | |
90 | =item * reasonably versatile output formats |
90 | =item * reasonably versatile output formats |
91 | |
91 | |
92 | You can choose between the most compact guaranteed-single-line format |
92 | You can choose between the most compact guaranteed-single-line format |
93 | possible (nice for simple line-based protocols), a pure-ASCII format |
93 | possible (nice for simple line-based protocols), a pure-ASCII format |
… | |
… | |
104 | use common::sense; |
104 | use common::sense; |
105 | |
105 | |
106 | our $VERSION = 2.34; |
106 | our $VERSION = 2.34; |
107 | our @ISA = qw(Exporter); |
107 | our @ISA = qw(Exporter); |
108 | |
108 | |
109 | our @EXPORT = qw(encode_json decode_json to_json from_json); |
109 | our @EXPORT = qw(encode_json decode_json); |
110 | |
|
|
111 | sub to_json($) { |
|
|
112 | require Carp; |
|
|
113 | Carp::croak ("JSON::XS::to_json has been renamed to encode_json, either downgrade to pre-2.0 versions of JSON::XS or rename the call"); |
|
|
114 | } |
|
|
115 | |
|
|
116 | sub from_json($) { |
|
|
117 | require Carp; |
|
|
118 | Carp::croak ("JSON::XS::from_json has been renamed to decode_json, either downgrade to pre-2.0 versions of JSON::XS or rename the call"); |
|
|
119 | } |
|
|
120 | |
110 | |
121 | use Exporter; |
111 | use Exporter; |
122 | use XSLoader; |
112 | use XSLoader; |
123 | |
113 | |
124 | =head1 FUNCTIONAL INTERFACE |
114 | =head1 FUNCTIONAL INTERFACE |
… | |
… | |
667 | |
657 | |
668 | See SECURITY CONSIDERATIONS, below, for more info on why this is useful. |
658 | See SECURITY CONSIDERATIONS, below, for more info on why this is useful. |
669 | |
659 | |
670 | =item $json_text = $json->encode ($perl_scalar) |
660 | =item $json_text = $json->encode ($perl_scalar) |
671 | |
661 | |
672 | Converts the given Perl data structure (a simple scalar or a reference |
662 | Converts the given Perl value or data structure to its JSON |
673 | to a hash or array) to its JSON representation. Simple scalars will be |
663 | representation. Croaks on error. |
674 | converted into JSON string or number sequences, while references to arrays |
|
|
675 | become JSON arrays and references to hashes become JSON objects. Undefined |
|
|
676 | Perl values (e.g. C<undef>) become JSON C<null> values. Neither C<true> |
|
|
677 | nor C<false> values will be generated. |
|
|
678 | |
664 | |
679 | =item $perl_scalar = $json->decode ($json_text) |
665 | =item $perl_scalar = $json->decode ($json_text) |
680 | |
666 | |
681 | The opposite of C<encode>: expects a JSON text and tries to parse it, |
667 | The opposite of C<encode>: expects a JSON text and tries to parse it, |
682 | returning the resulting simple scalar or reference. Croaks on error. |
668 | returning the resulting simple scalar or reference. Croaks on error. |
683 | |
|
|
684 | JSON numbers and strings become simple Perl scalars. JSON arrays become |
|
|
685 | Perl arrayrefs and JSON objects become Perl hashrefs. C<true> becomes |
|
|
686 | C<1>, C<false> becomes C<0> and C<null> becomes C<undef>. |
|
|
687 | |
669 | |
688 | =item ($perl_scalar, $characters) = $json->decode_prefix ($json_text) |
670 | =item ($perl_scalar, $characters) = $json->decode_prefix ($json_text) |
689 | |
671 | |
690 | This works like the C<decode> method, but instead of raising an exception |
672 | This works like the C<decode> method, but instead of raising an exception |
691 | when there is trailing garbage after the first JSON object, it will |
673 | when there is trailing garbage after the first JSON object, it will |
… | |
… | |
741 | |
723 | |
742 | If the method is called in scalar context, then it will try to extract |
724 | If the method is called in scalar context, then it will try to extract |
743 | exactly I<one> JSON object. If that is successful, it will return this |
725 | exactly I<one> JSON object. If that is successful, it will return this |
744 | object, otherwise it will return C<undef>. If there is a parse error, |
726 | object, otherwise it will return C<undef>. If there is a parse error, |
745 | this method will croak just as C<decode> would do (one can then use |
727 | this method will croak just as C<decode> would do (one can then use |
746 | C<incr_skip> to skip the errornous part). This is the most common way of |
728 | C<incr_skip> to skip the erroneous part). This is the most common way of |
747 | using the method. |
729 | using the method. |
748 | |
730 | |
749 | And finally, in list context, it will try to extract as many objects |
731 | And finally, in list context, it will try to extract as many objects |
750 | from the stream as it can find and return them, or the empty list |
732 | from the stream as it can find and return them, or the empty list |
751 | otherwise. For this to work, there must be no separators between the JSON |
733 | otherwise. For this to work, there must be no separators between the JSON |
… | |
… | |
780 | C<incr_parse> died, in which case the input buffer and incremental parser |
762 | C<incr_parse> died, in which case the input buffer and incremental parser |
781 | state is left unchanged, to skip the text parsed so far and to reset the |
763 | state is left unchanged, to skip the text parsed so far and to reset the |
782 | parse state. |
764 | parse state. |
783 | |
765 | |
784 | The difference to C<incr_reset> is that only text until the parse error |
766 | The difference to C<incr_reset> is that only text until the parse error |
785 | occured is removed. |
767 | occurred is removed. |
786 | |
768 | |
787 | =item $json->incr_reset |
769 | =item $json->incr_reset |
788 | |
770 | |
789 | This completely resets the incremental parser, that is, after this call, |
771 | This completely resets the incremental parser, that is, after this call, |
790 | it will be as if the parser had never parsed anything. |
772 | it will be as if the parser had never parsed anything. |
… | |
… | |
988 | If the number consists of digits only, JSON::XS will try to represent |
970 | If the number consists of digits only, JSON::XS will try to represent |
989 | it as an integer value. If that fails, it will try to represent it as |
971 | it as an integer value. If that fails, it will try to represent it as |
990 | a numeric (floating point) value if that is possible without loss of |
972 | a numeric (floating point) value if that is possible without loss of |
991 | precision. Otherwise it will preserve the number as a string value (in |
973 | precision. Otherwise it will preserve the number as a string value (in |
992 | which case you lose roundtripping ability, as the JSON number will be |
974 | which case you lose roundtripping ability, as the JSON number will be |
993 | re-encoded toa JSON string). |
975 | re-encoded to a JSON string). |
994 | |
976 | |
995 | Numbers containing a fractional or exponential part will always be |
977 | Numbers containing a fractional or exponential part will always be |
996 | represented as numeric (floating point) values, possibly at a loss of |
978 | represented as numeric (floating point) values, possibly at a loss of |
997 | precision (in which case you might lose perfect roundtripping ability, but |
979 | precision (in which case you might lose perfect roundtripping ability, but |
998 | the JSON number will still be re-encoded as a JSON number). |
980 | the JSON number will still be re-encoded as a JSON number). |
999 | |
981 | |
1000 | Note that precision is not accuracy - binary floating point values cannot |
982 | Note that precision is not accuracy - binary floating point values cannot |
1001 | represent most decimal fractions exactly, and when converting from and to |
983 | represent most decimal fractions exactly, and when converting from and to |
1002 | floating point, JSON::XS only guarantees precision up to but not including |
984 | floating point, JSON::XS only guarantees precision up to but not including |
1003 | the leats significant bit. |
985 | the least significant bit. |
1004 | |
986 | |
1005 | =item true, false |
987 | =item true, false |
1006 | |
988 | |
1007 | These JSON atoms become C<JSON::XS::true> and C<JSON::XS::false>, |
989 | These JSON atoms become C<JSON::XS::true> and C<JSON::XS::false>, |
1008 | respectively. They are overloaded to act almost exactly like the numbers |
990 | respectively. They are overloaded to act almost exactly like the numbers |
… | |
… | |
1138 | =item C<utf8> flag disabled |
1120 | =item C<utf8> flag disabled |
1139 | |
1121 | |
1140 | When C<utf8> is disabled (the default), then C<encode>/C<decode> generate |
1122 | When C<utf8> is disabled (the default), then C<encode>/C<decode> generate |
1141 | and expect Unicode strings, that is, characters with high ordinal Unicode |
1123 | and expect Unicode strings, that is, characters with high ordinal Unicode |
1142 | values (> 255) will be encoded as such characters, and likewise such |
1124 | values (> 255) will be encoded as such characters, and likewise such |
1143 | characters are decoded as-is, no canges to them will be done, except |
1125 | characters are decoded as-is, no changes to them will be done, except |
1144 | "(re-)interpreting" them as Unicode codepoints or Unicode characters, |
1126 | "(re-)interpreting" them as Unicode codepoints or Unicode characters, |
1145 | respectively (to Perl, these are the same thing in strings unless you do |
1127 | respectively (to Perl, these are the same thing in strings unless you do |
1146 | funny/weird/dumb stuff). |
1128 | funny/weird/dumb stuff). |
1147 | |
1129 | |
1148 | This is useful when you want to do the encoding yourself (e.g. when you |
1130 | This is useful when you want to do the encoding yourself (e.g. when you |
… | |
… | |
1264 | output for these property strings, e.g.: |
1246 | output for these property strings, e.g.: |
1265 | |
1247 | |
1266 | $json =~ s/"__proto__"\s*:/"__proto__renamed":/g; |
1248 | $json =~ s/"__proto__"\s*:/"__proto__renamed":/g; |
1267 | |
1249 | |
1268 | This works because C<__proto__> is not valid outside of strings, so every |
1250 | This works because C<__proto__> is not valid outside of strings, so every |
1269 | occurence of C<"__proto__"\s*:> must be a string used as property name. |
1251 | occurrence of C<"__proto__"\s*:> must be a string used as property name. |
1270 | |
1252 | |
1271 | If you know of other incompatibilities, please let me know. |
1253 | If you know of other incompatibilities, please let me know. |
1272 | |
1254 | |
1273 | |
1255 | |
1274 | =head2 JSON and YAML |
1256 | =head2 JSON and YAML |
… | |
… | |
1452 | |
1434 | |
1453 | Sometimes people avoid the Perl locale support and directly call the |
1435 | Sometimes people avoid the Perl locale support and directly call the |
1454 | system's setlocale function with C<LC_ALL>. |
1436 | system's setlocale function with C<LC_ALL>. |
1455 | |
1437 | |
1456 | This breaks both perl and modules such as JSON::XS, as stringification of |
1438 | This breaks both perl and modules such as JSON::XS, as stringification of |
1457 | numbers no longer works correcly (e.g. C<$x = 0.1; print "$x"+1> might |
1439 | numbers no longer works correctly (e.g. C<$x = 0.1; print "$x"+1> might |
1458 | print C<1>, and JSON::XS might output illegal JSON as JSON::XS relies on |
1440 | print C<1>, and JSON::XS might output illegal JSON as JSON::XS relies on |
1459 | perl to stringify numbers). |
1441 | perl to stringify numbers). |
1460 | |
1442 | |
1461 | The solution is simple: don't call C<setlocale>, or use it for only those |
1443 | The solution is simple: don't call C<setlocale>, or use it for only those |
1462 | categories you need, such as C<LC_MESSAGES> or C<LC_CTYPE>. |
1444 | categories you need, such as C<LC_MESSAGES> or C<LC_CTYPE>. |