… | |
… | |
64 | so, and even documents what "correct" means. |
64 | so, and even documents what "correct" means. |
65 | |
65 | |
66 | =item * round-trip integrity |
66 | =item * round-trip integrity |
67 | |
67 | |
68 | When you serialise a perl data structure using only data types supported |
68 | When you serialise a perl data structure using only data types supported |
69 | by JSON, the deserialised data structure is identical on the Perl level. |
69 | by JSON and Perl, the deserialised data structure is identical on the Perl |
70 | (e.g. the string "2.0" doesn't suddenly become "2" just because it looks |
70 | level. (e.g. the string "2.0" doesn't suddenly become "2" just because |
71 | like a number). There minor I<are> exceptions to this, read the MAPPING |
71 | it looks like a number). There I<are> minor exceptions to this, read the |
72 | section below to learn about those. |
72 | MAPPING section below to learn about those. |
73 | |
73 | |
74 | =item * strict checking of JSON correctness |
74 | =item * strict checking of JSON correctness |
75 | |
75 | |
76 | There is no guessing, no generating of illegal JSON texts by default, |
76 | There is no guessing, no generating of illegal JSON texts by default, |
77 | and only JSON is accepted as input by default (the latter is a security |
77 | and only JSON is accepted as input by default (the latter is a security |
… | |
… | |
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 |
… | |
… | |
101 | |
101 | |
102 | package JSON::XS; |
102 | package JSON::XS; |
103 | |
103 | |
104 | use common::sense; |
104 | use common::sense; |
105 | |
105 | |
106 | our $VERSION = '2.26'; |
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 |
… | |
… | |
432 | If C<$enable> is true (or missing), then the C<encode> method will output JSON objects |
422 | If C<$enable> is true (or missing), then the C<encode> method will output JSON objects |
433 | by sorting their keys. This is adding a comparatively high overhead. |
423 | by sorting their keys. This is adding a comparatively high overhead. |
434 | |
424 | |
435 | If C<$enable> is false, then the C<encode> method will output key-value |
425 | If C<$enable> is false, then the C<encode> method will output key-value |
436 | pairs in the order Perl stores them (which will likely change between runs |
426 | pairs in the order Perl stores them (which will likely change between runs |
437 | of the same script). |
427 | of the same script, and can change even within the same run from 5.18 |
|
|
428 | onwards). |
438 | |
429 | |
439 | This option is useful if you want the same data structure to be encoded as |
430 | This option is useful if you want the same data structure to be encoded as |
440 | the same JSON text (given the same overall settings). If it is disabled, |
431 | the same JSON text (given the same overall settings). If it is disabled, |
441 | the same hash might be encoded differently even if contains the same data, |
432 | the same hash might be encoded differently even if contains the same data, |
442 | as key-value pairs have no inherent ordering in Perl. |
433 | as key-value pairs have no inherent ordering in Perl. |
… | |
… | |
666 | |
657 | |
667 | 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. |
668 | |
659 | |
669 | =item $json_text = $json->encode ($perl_scalar) |
660 | =item $json_text = $json->encode ($perl_scalar) |
670 | |
661 | |
671 | Converts the given Perl data structure (a simple scalar or a reference |
662 | Converts the given Perl value or data structure to its JSON |
672 | to a hash or array) to its JSON representation. Simple scalars will be |
663 | representation. Croaks on error. |
673 | converted into JSON string or number sequences, while references to arrays |
|
|
674 | become JSON arrays and references to hashes become JSON objects. Undefined |
|
|
675 | Perl values (e.g. C<undef>) become JSON C<null> values. Neither C<true> |
|
|
676 | nor C<false> values will be generated. |
|
|
677 | |
664 | |
678 | =item $perl_scalar = $json->decode ($json_text) |
665 | =item $perl_scalar = $json->decode ($json_text) |
679 | |
666 | |
680 | 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, |
681 | returning the resulting simple scalar or reference. Croaks on error. |
668 | returning the resulting simple scalar or reference. Croaks on error. |
682 | |
|
|
683 | JSON numbers and strings become simple Perl scalars. JSON arrays become |
|
|
684 | Perl arrayrefs and JSON objects become Perl hashrefs. C<true> becomes |
|
|
685 | C<1>, C<false> becomes C<0> and C<null> becomes C<undef>. |
|
|
686 | |
669 | |
687 | =item ($perl_scalar, $characters) = $json->decode_prefix ($json_text) |
670 | =item ($perl_scalar, $characters) = $json->decode_prefix ($json_text) |
688 | |
671 | |
689 | 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 |
690 | 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 |
691 | silently stop parsing there and return the number of characters consumed |
674 | silently stop parsing there and return the number of characters consumed |
692 | so far. |
675 | so far. |
693 | |
676 | |
694 | This is useful if your JSON texts are not delimited by an outer protocol |
677 | This is useful if your JSON texts are not delimited by an outer protocol |
695 | (which is not the brightest thing to do in the first place) and you need |
|
|
696 | to know where the JSON text ends. |
678 | and you need to know where the JSON text ends. |
697 | |
679 | |
698 | JSON::XS->new->decode_prefix ("[1] the tail") |
680 | JSON::XS->new->decode_prefix ("[1] the tail") |
699 | => ([], 3) |
681 | => ([], 3) |
700 | |
682 | |
701 | =back |
683 | =back |
… | |
… | |
713 | calls). |
695 | calls). |
714 | |
696 | |
715 | JSON::XS will only attempt to parse the JSON text once it is sure it |
697 | JSON::XS will only attempt to parse the JSON text once it is sure it |
716 | has enough text to get a decisive result, using a very simple but |
698 | has enough text to get a decisive result, using a very simple but |
717 | truly incremental parser. This means that it sometimes won't stop as |
699 | truly incremental parser. This means that it sometimes won't stop as |
718 | early as the full parser, for example, it doesn't detect parenthese |
700 | early as the full parser, for example, it doesn't detect mismatched |
719 | mismatches. The only thing it guarantees is that it starts decoding as |
701 | parentheses. The only thing it guarantees is that it starts decoding as |
720 | soon as a syntactically valid JSON text has been seen. This means you need |
702 | soon as a syntactically valid JSON text has been seen. This means you need |
721 | to set resource limits (e.g. C<max_size>) to ensure the parser will stop |
703 | to set resource limits (e.g. C<max_size>) to ensure the parser will stop |
722 | parsing in the presence if syntax errors. |
704 | parsing in the presence if syntax errors. |
723 | |
705 | |
724 | The following methods implement this incremental parser. |
706 | The following methods implement this incremental parser. |
… | |
… | |
740 | |
722 | |
741 | If the method is called in scalar context, then it will try to extract |
723 | If the method is called in scalar context, then it will try to extract |
742 | exactly I<one> JSON object. If that is successful, it will return this |
724 | exactly I<one> JSON object. If that is successful, it will return this |
743 | object, otherwise it will return C<undef>. If there is a parse error, |
725 | object, otherwise it will return C<undef>. If there is a parse error, |
744 | this method will croak just as C<decode> would do (one can then use |
726 | this method will croak just as C<decode> would do (one can then use |
745 | C<incr_skip> to skip the errornous part). This is the most common way of |
727 | C<incr_skip> to skip the erroneous part). This is the most common way of |
746 | using the method. |
728 | using the method. |
747 | |
729 | |
748 | And finally, in list context, it will try to extract as many objects |
730 | And finally, in list context, it will try to extract as many objects |
749 | from the stream as it can find and return them, or the empty list |
731 | from the stream as it can find and return them, or the empty list |
750 | otherwise. For this to work, there must be no separators between the JSON |
732 | otherwise. For this to work, there must be no separators between the JSON |
751 | objects or arrays, instead they must be concatenated back-to-back. If |
733 | objects or arrays, instead they must be concatenated back-to-back. If |
752 | an error occurs, an exception will be raised as in the scalar context |
734 | an error occurs, an exception will be raised as in the scalar context |
753 | case. Note that in this case, any previously-parsed JSON texts will be |
735 | case. Note that in this case, any previously-parsed JSON texts will be |
754 | lost. |
736 | lost. |
755 | |
737 | |
|
|
738 | Example: Parse some JSON arrays/objects in a given string and return |
|
|
739 | them. |
|
|
740 | |
|
|
741 | my @objs = JSON::XS->new->incr_parse ("[5][7][1,2]"); |
|
|
742 | |
756 | =item $lvalue_string = $json->incr_text |
743 | =item $lvalue_string = $json->incr_text |
757 | |
744 | |
758 | This method returns the currently stored JSON fragment as an lvalue, that |
745 | This method returns the currently stored JSON fragment as an lvalue, that |
759 | is, you can manipulate it. This I<only> works when a preceding call to |
746 | is, you can manipulate it. This I<only> works when a preceding call to |
760 | C<incr_parse> in I<scalar context> successfully returned an object. Under |
747 | C<incr_parse> in I<scalar context> successfully returned an object. Under |
… | |
… | |
774 | C<incr_parse> died, in which case the input buffer and incremental parser |
761 | C<incr_parse> died, in which case the input buffer and incremental parser |
775 | state is left unchanged, to skip the text parsed so far and to reset the |
762 | state is left unchanged, to skip the text parsed so far and to reset the |
776 | parse state. |
763 | parse state. |
777 | |
764 | |
778 | The difference to C<incr_reset> is that only text until the parse error |
765 | The difference to C<incr_reset> is that only text until the parse error |
779 | occured is removed. |
766 | occurred is removed. |
780 | |
767 | |
781 | =item $json->incr_reset |
768 | =item $json->incr_reset |
782 | |
769 | |
783 | This completely resets the incremental parser, that is, after this call, |
770 | This completely resets the incremental parser, that is, after this call, |
784 | it will be as if the parser had never parsed anything. |
771 | it will be as if the parser had never parsed anything. |
… | |
… | |
790 | =back |
777 | =back |
791 | |
778 | |
792 | =head2 LIMITATIONS |
779 | =head2 LIMITATIONS |
793 | |
780 | |
794 | All options that affect decoding are supported, except |
781 | All options that affect decoding are supported, except |
795 | C<allow_nonref>. The reason for this is that it cannot be made to |
782 | C<allow_nonref>. The reason for this is that it cannot be made to work |
796 | work sensibly: JSON objects and arrays are self-delimited, i.e. you can concatenate |
783 | sensibly: JSON objects and arrays are self-delimited, i.e. you can |
797 | them back to back and still decode them perfectly. This does not hold true |
784 | concatenate them back to back and still decode them perfectly. This does |
798 | for JSON numbers, however. |
785 | not hold true for JSON numbers, however. |
799 | |
786 | |
800 | For example, is the string C<1> a single JSON number, or is it simply the |
787 | For example, is the string C<1> a single JSON number, or is it simply the |
801 | start of C<12>? Or is C<12> a single JSON number, or the concatenation |
788 | start of C<12>? Or is C<12> a single JSON number, or the concatenation |
802 | of C<1> and C<2>? In neither case you can tell, and this is why JSON::XS |
789 | of C<1> and C<2>? In neither case you can tell, and this is why JSON::XS |
803 | takes the conservative route and disallows this case. |
790 | takes the conservative route and disallows this case. |
… | |
… | |
982 | If the number consists of digits only, JSON::XS will try to represent |
969 | If the number consists of digits only, JSON::XS will try to represent |
983 | it as an integer value. If that fails, it will try to represent it as |
970 | it as an integer value. If that fails, it will try to represent it as |
984 | a numeric (floating point) value if that is possible without loss of |
971 | a numeric (floating point) value if that is possible without loss of |
985 | precision. Otherwise it will preserve the number as a string value (in |
972 | precision. Otherwise it will preserve the number as a string value (in |
986 | which case you lose roundtripping ability, as the JSON number will be |
973 | which case you lose roundtripping ability, as the JSON number will be |
987 | re-encoded toa JSON string). |
974 | re-encoded to a JSON string). |
988 | |
975 | |
989 | Numbers containing a fractional or exponential part will always be |
976 | Numbers containing a fractional or exponential part will always be |
990 | represented as numeric (floating point) values, possibly at a loss of |
977 | represented as numeric (floating point) values, possibly at a loss of |
991 | precision (in which case you might lose perfect roundtripping ability, but |
978 | precision (in which case you might lose perfect roundtripping ability, but |
992 | the JSON number will still be re-encoded as a JSON number). |
979 | the JSON number will still be re-encoded as a JSON number). |
993 | |
980 | |
|
|
981 | Note that precision is not accuracy - binary floating point values cannot |
|
|
982 | represent most decimal fractions exactly, and when converting from and to |
|
|
983 | floating point, JSON::XS only guarantees precision up to but not including |
|
|
984 | the least significant bit. |
|
|
985 | |
994 | =item true, false |
986 | =item true, false |
995 | |
987 | |
996 | These JSON atoms become C<JSON::XS::true> and C<JSON::XS::false>, |
988 | These JSON atoms become C<JSON::XS::true> and C<JSON::XS::false>, |
997 | respectively. They are overloaded to act almost exactly like the numbers |
989 | respectively. They are overloaded to act almost exactly like the numbers |
998 | C<1> and C<0>. You can check whether a scalar is a JSON boolean by using |
990 | C<1> and C<0>. You can check whether a scalar is a JSON boolean by using |
… | |
… | |
1013 | |
1005 | |
1014 | =over 4 |
1006 | =over 4 |
1015 | |
1007 | |
1016 | =item hash references |
1008 | =item hash references |
1017 | |
1009 | |
1018 | Perl hash references become JSON objects. As there is no inherent ordering |
1010 | Perl hash references become JSON objects. As there is no inherent |
1019 | in hash keys (or JSON objects), they will usually be encoded in a |
1011 | ordering in hash keys (or JSON objects), they will usually be encoded |
1020 | pseudo-random order that can change between runs of the same program but |
1012 | in a pseudo-random order. JSON::XS can optionally sort the hash keys |
1021 | stays generally the same within a single run of a program. JSON::XS can |
1013 | (determined by the I<canonical> flag), so the same datastructure will |
1022 | optionally sort the hash keys (determined by the I<canonical> flag), so |
1014 | serialise to the same JSON text (given same settings and version of |
1023 | the same datastructure will serialise to the same JSON text (given same |
1015 | JSON::XS), but this incurs a runtime overhead and is only rarely useful, |
1024 | settings and version of JSON::XS), but this incurs a runtime overhead |
1016 | e.g. when you want to compare some JSON text against another for equality. |
1025 | and is only rarely useful, e.g. when you want to compare some JSON text |
|
|
1026 | against another for equality. |
|
|
1027 | |
1017 | |
1028 | =item array references |
1018 | =item array references |
1029 | |
1019 | |
1030 | Perl array references become JSON arrays. |
1020 | Perl array references become JSON arrays. |
1031 | |
1021 | |
… | |
… | |
1084 | $x *= 1; # same thing, the choice is yours. |
1074 | $x *= 1; # same thing, the choice is yours. |
1085 | |
1075 | |
1086 | You can not currently force the type in other, less obscure, ways. Tell me |
1076 | You can not currently force the type in other, less obscure, ways. Tell me |
1087 | if you need this capability (but don't forget to explain why it's needed |
1077 | if you need this capability (but don't forget to explain why it's needed |
1088 | :). |
1078 | :). |
|
|
1079 | |
|
|
1080 | Note that numerical precision has the same meaning as under Perl (so |
|
|
1081 | binary to decimal conversion follows the same rules as in Perl, which |
|
|
1082 | can differ to other languages). Also, your perl interpreter might expose |
|
|
1083 | extensions to the floating point numbers of your platform, such as |
|
|
1084 | infinities or NaN's - these cannot be represented in JSON, and it is an |
|
|
1085 | error to pass those in. |
1089 | |
1086 | |
1090 | =back |
1087 | =back |
1091 | |
1088 | |
1092 | |
1089 | |
1093 | =head1 ENCODING/CODESET FLAG NOTES |
1090 | =head1 ENCODING/CODESET FLAG NOTES |
… | |
… | |
1120 | =item C<utf8> flag disabled |
1117 | =item C<utf8> flag disabled |
1121 | |
1118 | |
1122 | When C<utf8> is disabled (the default), then C<encode>/C<decode> generate |
1119 | When C<utf8> is disabled (the default), then C<encode>/C<decode> generate |
1123 | and expect Unicode strings, that is, characters with high ordinal Unicode |
1120 | and expect Unicode strings, that is, characters with high ordinal Unicode |
1124 | values (> 255) will be encoded as such characters, and likewise such |
1121 | values (> 255) will be encoded as such characters, and likewise such |
1125 | characters are decoded as-is, no canges to them will be done, except |
1122 | characters are decoded as-is, no changes to them will be done, except |
1126 | "(re-)interpreting" them as Unicode codepoints or Unicode characters, |
1123 | "(re-)interpreting" them as Unicode codepoints or Unicode characters, |
1127 | respectively (to Perl, these are the same thing in strings unless you do |
1124 | respectively (to Perl, these are the same thing in strings unless you do |
1128 | funny/weird/dumb stuff). |
1125 | funny/weird/dumb stuff). |
1129 | |
1126 | |
1130 | This is useful when you want to do the encoding yourself (e.g. when you |
1127 | This is useful when you want to do the encoding yourself (e.g. when you |
… | |
… | |
1238 | well - using C<eval> naively simply I<will> cause problems. |
1235 | well - using C<eval> naively simply I<will> cause problems. |
1239 | |
1236 | |
1240 | Another problem is that some javascript implementations reserve |
1237 | Another problem is that some javascript implementations reserve |
1241 | some property names for their own purposes (which probably makes |
1238 | some property names for their own purposes (which probably makes |
1242 | them non-ECMAscript-compliant). For example, Iceweasel reserves the |
1239 | them non-ECMAscript-compliant). For example, Iceweasel reserves the |
1243 | C<__proto__> property name for it's own purposes. |
1240 | C<__proto__> property name for its own purposes. |
1244 | |
1241 | |
1245 | If that is a problem, you could parse try to filter the resulting JSON |
1242 | If that is a problem, you could parse try to filter the resulting JSON |
1246 | output for these property strings, e.g.: |
1243 | output for these property strings, e.g.: |
1247 | |
1244 | |
1248 | $json =~ s/"__proto__"\s*:/"__proto__renamed":/g; |
1245 | $json =~ s/"__proto__"\s*:/"__proto__renamed":/g; |
1249 | |
1246 | |
1250 | This works because C<__proto__> is not valid outside of strings, so every |
1247 | This works because C<__proto__> is not valid outside of strings, so every |
1251 | occurence of C<"__proto__"\s*:> must be a string used as property name. |
1248 | occurrence of C<"__proto__"\s*:> must be a string used as property name. |
1252 | |
1249 | |
1253 | If you know of other incompatibilities, please let me know. |
1250 | If you know of other incompatibilities, please let me know. |
1254 | |
1251 | |
1255 | |
1252 | |
1256 | =head2 JSON and YAML |
1253 | =head2 JSON and YAML |
… | |
… | |
1302 | that difficult or long) and finally make YAML compatible to it, and |
1299 | that difficult or long) and finally make YAML compatible to it, and |
1303 | educating users about the changes, instead of spreading lies about the |
1300 | educating users about the changes, instead of spreading lies about the |
1304 | real compatibility for many I<years> and trying to silence people who |
1301 | real compatibility for many I<years> and trying to silence people who |
1305 | point out that it isn't true. |
1302 | point out that it isn't true. |
1306 | |
1303 | |
1307 | Addendum/2009: the YAML 1.2 spec is still incomaptible with JSON, even |
1304 | Addendum/2009: the YAML 1.2 spec is still incompatible with JSON, even |
1308 | though the incompatibilities have been documented (and are known to |
1305 | though the incompatibilities have been documented (and are known to Brian) |
1309 | Brian) for many years and the spec makes explicit claims that YAML is a |
1306 | for many years and the spec makes explicit claims that YAML is a superset |
1310 | superset of JSON. It would be so easy to fix, but apparently, bullying and |
1307 | of JSON. It would be so easy to fix, but apparently, bullying people and |
1311 | corrupting userdata is so much easier. |
1308 | corrupting userdata is so much easier. |
1312 | |
1309 | |
1313 | =back |
1310 | =back |
1314 | |
1311 | |
1315 | |
1312 | |
… | |
… | |
1324 | a very short single-line JSON string (also available at |
1321 | a very short single-line JSON string (also available at |
1325 | L<http://dist.schmorp.de/misc/json/short.json>). |
1322 | L<http://dist.schmorp.de/misc/json/short.json>). |
1326 | |
1323 | |
1327 | {"method": "handleMessage", "params": ["user1", |
1324 | {"method": "handleMessage", "params": ["user1", |
1328 | "we were just talking"], "id": null, "array":[1,11,234,-5,1e5,1e7, |
1325 | "we were just talking"], "id": null, "array":[1,11,234,-5,1e5,1e7, |
1329 | true, false]} |
1326 | 1, 0]} |
1330 | |
1327 | |
1331 | It shows the number of encodes/decodes per second (JSON::XS uses |
1328 | It shows the number of encodes/decodes per second (JSON::XS uses |
1332 | the functional interface, while JSON::XS/2 uses the OO interface |
1329 | the functional interface, while JSON::XS/2 uses the OO interface |
1333 | with pretty-printing and hashkey sorting enabled, JSON::XS/3 enables |
1330 | with pretty-printing and hashkey sorting enabled, JSON::XS/3 enables |
1334 | shrink). Higher is better: |
1331 | shrink. JSON::DWIW/DS uses the deserialise function, while JSON::DWIW::FJ |
|
|
1332 | uses the from_json method). Higher is better: |
1335 | |
1333 | |
1336 | module | encode | decode | |
1334 | module | encode | decode | |
1337 | -----------|------------|------------| |
1335 | --------------|------------|------------| |
1338 | JSON 1.x | 4990.842 | 4088.813 | |
1336 | JSON::DWIW/DS | 86302.551 | 102300.098 | |
1339 | JSON::DWIW | 51653.990 | 71575.154 | |
1337 | JSON::DWIW/FJ | 86302.551 | 75983.768 | |
1340 | JSON::PC | 65948.176 | 74631.744 | |
1338 | JSON::PP | 15827.562 | 6638.658 | |
1341 | JSON::PP | 8931.652 | 3817.168 | |
1339 | JSON::Syck | 63358.066 | 47662.545 | |
1342 | JSON::Syck | 24877.248 | 27776.848 | |
1340 | JSON::XS | 511500.488 | 511500.488 | |
1343 | JSON::XS | 388361.481 | 227951.304 | |
1341 | JSON::XS/2 | 291271.111 | 388361.481 | |
1344 | JSON::XS/2 | 227951.304 | 218453.333 | |
1342 | JSON::XS/3 | 361577.931 | 361577.931 | |
1345 | JSON::XS/3 | 338250.323 | 218453.333 | |
1343 | Storable | 66788.280 | 265462.278 | |
1346 | Storable | 16500.016 | 135300.129 | |
|
|
1347 | -----------+------------+------------+ |
1344 | --------------+------------+------------+ |
1348 | |
1345 | |
1349 | That is, JSON::XS is about five times faster than JSON::DWIW on encoding, |
1346 | That is, JSON::XS is almost six times faster than JSON::DWIW on encoding, |
1350 | about three times faster on decoding, and over forty times faster |
1347 | about five times faster on decoding, and over thirty to seventy times |
1351 | than JSON, even with pretty-printing and key sorting. It also compares |
1348 | faster than JSON's pure perl implementation. It also compares favourably |
1352 | favourably to Storable for small amounts of data. |
1349 | to Storable for small amounts of data. |
1353 | |
1350 | |
1354 | Using a longer test string (roughly 18KB, generated from Yahoo! Locals |
1351 | Using a longer test string (roughly 18KB, generated from Yahoo! Locals |
1355 | search API (L<http://dist.schmorp.de/misc/json/long.json>). |
1352 | search API (L<http://dist.schmorp.de/misc/json/long.json>). |
1356 | |
1353 | |
1357 | module | encode | decode | |
1354 | module | encode | decode | |
1358 | -----------|------------|------------| |
1355 | --------------|------------|------------| |
1359 | JSON 1.x | 55.260 | 34.971 | |
1356 | JSON::DWIW/DS | 1647.927 | 2673.916 | |
1360 | JSON::DWIW | 825.228 | 1082.513 | |
1357 | JSON::DWIW/FJ | 1630.249 | 2596.128 | |
1361 | JSON::PC | 3571.444 | 2394.829 | |
|
|
1362 | JSON::PP | 210.987 | 32.574 | |
1358 | JSON::PP | 400.640 | 62.311 | |
1363 | JSON::Syck | 552.551 | 787.544 | |
1359 | JSON::Syck | 1481.040 | 1524.869 | |
1364 | JSON::XS | 5780.463 | 4854.519 | |
1360 | JSON::XS | 20661.596 | 9541.183 | |
1365 | JSON::XS/2 | 3869.998 | 4798.975 | |
1361 | JSON::XS/2 | 10683.403 | 9416.938 | |
1366 | JSON::XS/3 | 5862.880 | 4798.975 | |
1362 | JSON::XS/3 | 20661.596 | 9400.054 | |
1367 | Storable | 4445.002 | 5235.027 | |
1363 | Storable | 19765.806 | 10000.725 | |
1368 | -----------+------------+------------+ |
1364 | --------------+------------+------------+ |
1369 | |
1365 | |
1370 | Again, JSON::XS leads by far (except for Storable which non-surprisingly |
1366 | Again, JSON::XS leads by far (except for Storable which non-surprisingly |
1371 | decodes faster). |
1367 | decodes a bit faster). |
1372 | |
1368 | |
1373 | On large strings containing lots of high Unicode characters, some modules |
1369 | On large strings containing lots of high Unicode characters, some modules |
1374 | (such as JSON::PC) seem to decode faster than JSON::XS, but the result |
1370 | (such as JSON::PC) seem to decode faster than JSON::XS, but the result |
1375 | will be broken due to missing (or wrong) Unicode handling. Others refuse |
1371 | will be broken due to missing (or wrong) Unicode handling. Others refuse |
1376 | to decode or encode properly, so it was impossible to prepare a fair |
1372 | to decode or encode properly, so it was impossible to prepare a fair |
… | |
… | |
1412 | information you might want to make sure that exceptions thrown by JSON::XS |
1408 | information you might want to make sure that exceptions thrown by JSON::XS |
1413 | will not end up in front of untrusted eyes. |
1409 | will not end up in front of untrusted eyes. |
1414 | |
1410 | |
1415 | If you are using JSON::XS to return packets to consumption |
1411 | If you are using JSON::XS to return packets to consumption |
1416 | by JavaScript scripts in a browser you should have a look at |
1412 | by JavaScript scripts in a browser you should have a look at |
1417 | L<http://jpsykes.com/47/practical-csrf-and-json-security> to see whether |
1413 | L<http://blog.archive.jpsykes.com/47/practical-csrf-and-json-security/> to |
1418 | you are vulnerable to some common attack vectors (which really are browser |
1414 | see whether you are vulnerable to some common attack vectors (which really |
1419 | design bugs, but it is still you who will have to deal with it, as major |
1415 | are browser design bugs, but it is still you who will have to deal with |
1420 | browser developers care only for features, not about getting security |
1416 | it, as major browser developers care only for features, not about getting |
1421 | right). |
1417 | security right). |
1422 | |
1418 | |
1423 | |
1419 | |
1424 | =head1 THREADS |
1420 | =head1 THREADS |
1425 | |
1421 | |
1426 | This module is I<not> guaranteed to be thread safe and there are no |
1422 | This module is I<not> guaranteed to be thread safe and there are no |
1427 | plans to change this until Perl gets thread support (as opposed to the |
1423 | plans to change this until Perl gets thread support (as opposed to the |
1428 | horribly slow so-called "threads" which are simply slow and bloated |
1424 | horribly slow so-called "threads" which are simply slow and bloated |
1429 | process simulations - use fork, it's I<much> faster, cheaper, better). |
1425 | process simulations - use fork, it's I<much> faster, cheaper, better). |
1430 | |
1426 | |
1431 | (It might actually work, but you have been warned). |
1427 | (It might actually work, but you have been warned). |
|
|
1428 | |
|
|
1429 | |
|
|
1430 | =head1 THE PERILS OF SETLOCALE |
|
|
1431 | |
|
|
1432 | Sometimes people avoid the Perl locale support and directly call the |
|
|
1433 | system's setlocale function with C<LC_ALL>. |
|
|
1434 | |
|
|
1435 | This breaks both perl and modules such as JSON::XS, as stringification of |
|
|
1436 | numbers no longer works correctly (e.g. C<$x = 0.1; print "$x"+1> might |
|
|
1437 | print C<1>, and JSON::XS might output illegal JSON as JSON::XS relies on |
|
|
1438 | perl to stringify numbers). |
|
|
1439 | |
|
|
1440 | The solution is simple: don't call C<setlocale>, or use it for only those |
|
|
1441 | categories you need, such as C<LC_MESSAGES> or C<LC_CTYPE>. |
|
|
1442 | |
|
|
1443 | If you need C<LC_NUMERIC>, you should enable it only around the code that |
|
|
1444 | actually needs it (avoiding stringification of numbers), and restore it |
|
|
1445 | afterwards. |
1432 | |
1446 | |
1433 | |
1447 | |
1434 | =head1 BUGS |
1448 | =head1 BUGS |
1435 | |
1449 | |
1436 | While the goal of this module is to be correct, that unfortunately does |
1450 | While the goal of this module is to be correct, that unfortunately does |