… | |
… | |
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 |
… | |
… | |
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.28'; |
106 | our $VERSION = '2.32'; |
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 to_json from_json); |
110 | |
110 | |
111 | sub to_json($) { |
111 | sub to_json($) { |
… | |
… | |
713 | calls). |
713 | calls). |
714 | |
714 | |
715 | JSON::XS will only attempt to parse the JSON text once it is sure it |
715 | 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 |
716 | 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 |
717 | 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 |
718 | 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 |
719 | 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 |
720 | 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 |
721 | to set resource limits (e.g. C<max_size>) to ensure the parser will stop |
722 | parsing in the presence if syntax errors. |
722 | parsing in the presence if syntax errors. |
723 | |
723 | |
724 | The following methods implement this incremental parser. |
724 | The following methods implement this incremental parser. |
… | |
… | |
994 | Numbers containing a fractional or exponential part will always be |
994 | Numbers containing a fractional or exponential part will always be |
995 | represented as numeric (floating point) values, possibly at a loss of |
995 | represented as numeric (floating point) values, possibly at a loss of |
996 | precision (in which case you might lose perfect roundtripping ability, but |
996 | precision (in which case you might lose perfect roundtripping ability, but |
997 | the JSON number will still be re-encoded as a JSON number). |
997 | the JSON number will still be re-encoded as a JSON number). |
998 | |
998 | |
|
|
999 | Note that precision is not accuracy - binary floating point values cannot |
|
|
1000 | represent most decimal fractions exactly, and when converting from and to |
|
|
1001 | floating point, JSON::XS only guarantees precision up to but not including |
|
|
1002 | the leats significant bit. |
|
|
1003 | |
999 | =item true, false |
1004 | =item true, false |
1000 | |
1005 | |
1001 | These JSON atoms become C<JSON::XS::true> and C<JSON::XS::false>, |
1006 | These JSON atoms become C<JSON::XS::true> and C<JSON::XS::false>, |
1002 | respectively. They are overloaded to act almost exactly like the numbers |
1007 | respectively. They are overloaded to act almost exactly like the numbers |
1003 | C<1> and C<0>. You can check whether a scalar is a JSON boolean by using |
1008 | C<1> and C<0>. You can check whether a scalar is a JSON boolean by using |
… | |
… | |
1090 | |
1095 | |
1091 | You can not currently force the type in other, less obscure, ways. Tell me |
1096 | You can not currently force the type in other, less obscure, ways. Tell me |
1092 | if you need this capability (but don't forget to explain why it's needed |
1097 | if you need this capability (but don't forget to explain why it's needed |
1093 | :). |
1098 | :). |
1094 | |
1099 | |
|
|
1100 | Note that numerical precision has the same meaning as under Perl (so |
|
|
1101 | binary to decimal conversion follows the same rules as in Perl, which |
|
|
1102 | can differ to other languages). Also, your perl interpreter might expose |
|
|
1103 | extensions to the floating point numbers of your platform, such as |
|
|
1104 | infinities or NaN's - these cannot be represented in JSON, and it is an |
|
|
1105 | error to pass those in. |
|
|
1106 | |
1095 | =back |
1107 | =back |
1096 | |
1108 | |
1097 | |
1109 | |
1098 | =head1 ENCODING/CODESET FLAG NOTES |
1110 | =head1 ENCODING/CODESET FLAG NOTES |
1099 | |
1111 | |
… | |
… | |
1243 | well - using C<eval> naively simply I<will> cause problems. |
1255 | well - using C<eval> naively simply I<will> cause problems. |
1244 | |
1256 | |
1245 | Another problem is that some javascript implementations reserve |
1257 | Another problem is that some javascript implementations reserve |
1246 | some property names for their own purposes (which probably makes |
1258 | some property names for their own purposes (which probably makes |
1247 | them non-ECMAscript-compliant). For example, Iceweasel reserves the |
1259 | them non-ECMAscript-compliant). For example, Iceweasel reserves the |
1248 | C<__proto__> property name for it's own purposes. |
1260 | C<__proto__> property name for its own purposes. |
1249 | |
1261 | |
1250 | If that is a problem, you could parse try to filter the resulting JSON |
1262 | If that is a problem, you could parse try to filter the resulting JSON |
1251 | output for these property strings, e.g.: |
1263 | output for these property strings, e.g.: |
1252 | |
1264 | |
1253 | $json =~ s/"__proto__"\s*:/"__proto__renamed":/g; |
1265 | $json =~ s/"__proto__"\s*:/"__proto__renamed":/g; |
… | |
… | |
1307 | that difficult or long) and finally make YAML compatible to it, and |
1319 | that difficult or long) and finally make YAML compatible to it, and |
1308 | educating users about the changes, instead of spreading lies about the |
1320 | educating users about the changes, instead of spreading lies about the |
1309 | real compatibility for many I<years> and trying to silence people who |
1321 | real compatibility for many I<years> and trying to silence people who |
1310 | point out that it isn't true. |
1322 | point out that it isn't true. |
1311 | |
1323 | |
1312 | Addendum/2009: the YAML 1.2 spec is still incomaptible with JSON, even |
1324 | Addendum/2009: the YAML 1.2 spec is still incompatible with JSON, even |
1313 | though the incompatibilities have been documented (and are known to |
1325 | though the incompatibilities have been documented (and are known to Brian) |
1314 | Brian) for many years and the spec makes explicit claims that YAML is a |
1326 | for many years and the spec makes explicit claims that YAML is a superset |
1315 | superset of JSON. It would be so easy to fix, but apparently, bullying and |
1327 | of JSON. It would be so easy to fix, but apparently, bullying people and |
1316 | corrupting userdata is so much easier. |
1328 | corrupting userdata is so much easier. |
1317 | |
1329 | |
1318 | =back |
1330 | =back |
1319 | |
1331 | |
1320 | |
1332 | |