… | |
… | |
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.27'; |
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 to_json from_json); |
110 | |
110 | |
111 | sub to_json($) { |
111 | sub to_json($) { |
… | |
… | |
432 | If C<$enable> is true (or missing), then the C<encode> method will output JSON objects |
432 | 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. |
433 | by sorting their keys. This is adding a comparatively high overhead. |
434 | |
434 | |
435 | If C<$enable> is false, then the C<encode> method will output key-value |
435 | 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 |
436 | pairs in the order Perl stores them (which will likely change between runs |
437 | of the same script). |
437 | of the same script, and can change even within the same run from 5.18 |
|
|
438 | onwards). |
438 | |
439 | |
439 | This option is useful if you want the same data structure to be encoded as |
440 | 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, |
441 | 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, |
442 | the same hash might be encoded differently even if contains the same data, |
442 | as key-value pairs have no inherent ordering in Perl. |
443 | as key-value pairs have no inherent ordering in Perl. |
… | |
… | |
713 | calls). |
714 | calls). |
714 | |
715 | |
715 | JSON::XS will only attempt to parse the JSON text once it is sure it |
716 | 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 |
717 | 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 |
718 | 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 |
719 | 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 |
720 | 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 |
721 | 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 |
722 | to set resource limits (e.g. C<max_size>) to ensure the parser will stop |
722 | parsing in the presence if syntax errors. |
723 | parsing in the presence if syntax errors. |
723 | |
724 | |
724 | The following methods implement this incremental parser. |
725 | The following methods implement this incremental parser. |
… | |
… | |
750 | otherwise. For this to work, there must be no separators between the JSON |
751 | 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 |
752 | 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 |
753 | 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 |
754 | case. Note that in this case, any previously-parsed JSON texts will be |
754 | lost. |
755 | lost. |
|
|
756 | |
|
|
757 | Example: Parse some JSON arrays/objects in a given string and return |
|
|
758 | them. |
|
|
759 | |
|
|
760 | my @objs = JSON::XS->new->incr_parse ("[5][7][1,2]"); |
755 | |
761 | |
756 | =item $lvalue_string = $json->incr_text |
762 | =item $lvalue_string = $json->incr_text |
757 | |
763 | |
758 | This method returns the currently stored JSON fragment as an lvalue, that |
764 | 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 |
765 | is, you can manipulate it. This I<only> works when a preceding call to |
… | |
… | |
989 | Numbers containing a fractional or exponential part will always be |
995 | Numbers containing a fractional or exponential part will always be |
990 | represented as numeric (floating point) values, possibly at a loss of |
996 | represented as numeric (floating point) values, possibly at a loss of |
991 | precision (in which case you might lose perfect roundtripping ability, but |
997 | precision (in which case you might lose perfect roundtripping ability, but |
992 | the JSON number will still be re-encoded as a JSON number). |
998 | the JSON number will still be re-encoded as a JSON number). |
993 | |
999 | |
|
|
1000 | Note that precision is not accuracy - binary floating point values cannot |
|
|
1001 | represent most decimal fractions exactly, and when converting from and to |
|
|
1002 | floating point, JSON::XS only guarantees precision up to but not including |
|
|
1003 | the leats significant bit. |
|
|
1004 | |
994 | =item true, false |
1005 | =item true, false |
995 | |
1006 | |
996 | These JSON atoms become C<JSON::XS::true> and C<JSON::XS::false>, |
1007 | 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 |
1008 | 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 |
1009 | C<1> and C<0>. You can check whether a scalar is a JSON boolean by using |
… | |
… | |
1085 | |
1096 | |
1086 | You can not currently force the type in other, less obscure, ways. Tell me |
1097 | 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 |
1098 | if you need this capability (but don't forget to explain why it's needed |
1088 | :). |
1099 | :). |
1089 | |
1100 | |
|
|
1101 | Note that numerical precision has the same meaning as under Perl (so |
|
|
1102 | binary to decimal conversion follows the same rules as in Perl, which |
|
|
1103 | can differ to other languages). Also, your perl interpreter might expose |
|
|
1104 | extensions to the floating point numbers of your platform, such as |
|
|
1105 | infinities or NaN's - these cannot be represented in JSON, and it is an |
|
|
1106 | error to pass those in. |
|
|
1107 | |
1090 | =back |
1108 | =back |
1091 | |
1109 | |
1092 | |
1110 | |
1093 | =head1 ENCODING/CODESET FLAG NOTES |
1111 | =head1 ENCODING/CODESET FLAG NOTES |
1094 | |
1112 | |
… | |
… | |
1238 | well - using C<eval> naively simply I<will> cause problems. |
1256 | well - using C<eval> naively simply I<will> cause problems. |
1239 | |
1257 | |
1240 | Another problem is that some javascript implementations reserve |
1258 | Another problem is that some javascript implementations reserve |
1241 | some property names for their own purposes (which probably makes |
1259 | some property names for their own purposes (which probably makes |
1242 | them non-ECMAscript-compliant). For example, Iceweasel reserves the |
1260 | them non-ECMAscript-compliant). For example, Iceweasel reserves the |
1243 | C<__proto__> property name for it's own purposes. |
1261 | C<__proto__> property name for its own purposes. |
1244 | |
1262 | |
1245 | If that is a problem, you could parse try to filter the resulting JSON |
1263 | If that is a problem, you could parse try to filter the resulting JSON |
1246 | output for these property strings, e.g.: |
1264 | output for these property strings, e.g.: |
1247 | |
1265 | |
1248 | $json =~ s/"__proto__"\s*:/"__proto__renamed":/g; |
1266 | $json =~ s/"__proto__"\s*:/"__proto__renamed":/g; |
… | |
… | |
1302 | that difficult or long) and finally make YAML compatible to it, and |
1320 | that difficult or long) and finally make YAML compatible to it, and |
1303 | educating users about the changes, instead of spreading lies about the |
1321 | educating users about the changes, instead of spreading lies about the |
1304 | real compatibility for many I<years> and trying to silence people who |
1322 | real compatibility for many I<years> and trying to silence people who |
1305 | point out that it isn't true. |
1323 | point out that it isn't true. |
1306 | |
1324 | |
1307 | Addendum/2009: the YAML 1.2 spec is still incomaptible with JSON, even |
1325 | Addendum/2009: the YAML 1.2 spec is still incompatible with JSON, even |
1308 | though the incompatibilities have been documented (and are known to |
1326 | 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 |
1327 | 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 |
1328 | of JSON. It would be so easy to fix, but apparently, bullying people and |
1311 | corrupting userdata is so much easier. |
1329 | corrupting userdata is so much easier. |
1312 | |
1330 | |
1313 | =back |
1331 | =back |
1314 | |
1332 | |
1315 | |
1333 | |
… | |
… | |
1324 | a very short single-line JSON string (also available at |
1342 | a very short single-line JSON string (also available at |
1325 | L<http://dist.schmorp.de/misc/json/short.json>). |
1343 | L<http://dist.schmorp.de/misc/json/short.json>). |
1326 | |
1344 | |
1327 | {"method": "handleMessage", "params": ["user1", |
1345 | {"method": "handleMessage", "params": ["user1", |
1328 | "we were just talking"], "id": null, "array":[1,11,234,-5,1e5,1e7, |
1346 | "we were just talking"], "id": null, "array":[1,11,234,-5,1e5,1e7, |
1329 | true, false]} |
1347 | 1, 0]} |
1330 | |
1348 | |
1331 | It shows the number of encodes/decodes per second (JSON::XS uses |
1349 | It shows the number of encodes/decodes per second (JSON::XS uses |
1332 | the functional interface, while JSON::XS/2 uses the OO interface |
1350 | the functional interface, while JSON::XS/2 uses the OO interface |
1333 | with pretty-printing and hashkey sorting enabled, JSON::XS/3 enables |
1351 | with pretty-printing and hashkey sorting enabled, JSON::XS/3 enables |
1334 | shrink). Higher is better: |
1352 | shrink. JSON::DWIW/DS uses the deserialise function, while JSON::DWIW::FJ |
|
|
1353 | uses the from_json method). Higher is better: |
1335 | |
1354 | |
1336 | module | encode | decode | |
1355 | module | encode | decode | |
1337 | -----------|------------|------------| |
1356 | --------------|------------|------------| |
1338 | JSON 1.x | 4990.842 | 4088.813 | |
1357 | JSON::DWIW/DS | 86302.551 | 102300.098 | |
1339 | JSON::DWIW | 51653.990 | 71575.154 | |
1358 | JSON::DWIW/FJ | 86302.551 | 75983.768 | |
1340 | JSON::PC | 65948.176 | 74631.744 | |
1359 | JSON::PP | 15827.562 | 6638.658 | |
1341 | JSON::PP | 8931.652 | 3817.168 | |
1360 | JSON::Syck | 63358.066 | 47662.545 | |
1342 | JSON::Syck | 24877.248 | 27776.848 | |
1361 | JSON::XS | 511500.488 | 511500.488 | |
1343 | JSON::XS | 388361.481 | 227951.304 | |
1362 | JSON::XS/2 | 291271.111 | 388361.481 | |
1344 | JSON::XS/2 | 227951.304 | 218453.333 | |
1363 | JSON::XS/3 | 361577.931 | 361577.931 | |
1345 | JSON::XS/3 | 338250.323 | 218453.333 | |
1364 | Storable | 66788.280 | 265462.278 | |
1346 | Storable | 16500.016 | 135300.129 | |
|
|
1347 | -----------+------------+------------+ |
1365 | --------------+------------+------------+ |
1348 | |
1366 | |
1349 | That is, JSON::XS is about five times faster than JSON::DWIW on encoding, |
1367 | 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 |
1368 | 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 |
1369 | faster than JSON's pure perl implementation. It also compares favourably |
1352 | favourably to Storable for small amounts of data. |
1370 | to Storable for small amounts of data. |
1353 | |
1371 | |
1354 | Using a longer test string (roughly 18KB, generated from Yahoo! Locals |
1372 | Using a longer test string (roughly 18KB, generated from Yahoo! Locals |
1355 | search API (L<http://dist.schmorp.de/misc/json/long.json>). |
1373 | search API (L<http://dist.schmorp.de/misc/json/long.json>). |
1356 | |
1374 | |
1357 | module | encode | decode | |
1375 | module | encode | decode | |
1358 | -----------|------------|------------| |
1376 | --------------|------------|------------| |
1359 | JSON 1.x | 55.260 | 34.971 | |
1377 | JSON::DWIW/DS | 1647.927 | 2673.916 | |
1360 | JSON::DWIW | 825.228 | 1082.513 | |
1378 | JSON::DWIW/FJ | 1630.249 | 2596.128 | |
1361 | JSON::PC | 3571.444 | 2394.829 | |
|
|
1362 | JSON::PP | 210.987 | 32.574 | |
1379 | JSON::PP | 400.640 | 62.311 | |
1363 | JSON::Syck | 552.551 | 787.544 | |
1380 | JSON::Syck | 1481.040 | 1524.869 | |
1364 | JSON::XS | 5780.463 | 4854.519 | |
1381 | JSON::XS | 20661.596 | 9541.183 | |
1365 | JSON::XS/2 | 3869.998 | 4798.975 | |
1382 | JSON::XS/2 | 10683.403 | 9416.938 | |
1366 | JSON::XS/3 | 5862.880 | 4798.975 | |
1383 | JSON::XS/3 | 20661.596 | 9400.054 | |
1367 | Storable | 4445.002 | 5235.027 | |
1384 | Storable | 19765.806 | 10000.725 | |
1368 | -----------+------------+------------+ |
1385 | --------------+------------+------------+ |
1369 | |
1386 | |
1370 | Again, JSON::XS leads by far (except for Storable which non-surprisingly |
1387 | Again, JSON::XS leads by far (except for Storable which non-surprisingly |
1371 | decodes faster). |
1388 | decodes a bit faster). |
1372 | |
1389 | |
1373 | On large strings containing lots of high Unicode characters, some modules |
1390 | 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 |
1391 | (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 |
1392 | 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 |
1393 | to decode or encode properly, so it was impossible to prepare a fair |
… | |
… | |
1429 | process simulations - use fork, it's I<much> faster, cheaper, better). |
1446 | process simulations - use fork, it's I<much> faster, cheaper, better). |
1430 | |
1447 | |
1431 | (It might actually work, but you have been warned). |
1448 | (It might actually work, but you have been warned). |
1432 | |
1449 | |
1433 | |
1450 | |
|
|
1451 | =head1 THE PERILS OF SETLOCALE |
|
|
1452 | |
|
|
1453 | Sometimes people avoid the Perl locale support and directly call the |
|
|
1454 | system's setlocale function with C<LC_ALL>. |
|
|
1455 | |
|
|
1456 | 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 |
|
|
1458 | print C<1>, and JSON::XS might output illegal JSON as JSON::XS relies on |
|
|
1459 | perl to stringify numbers). |
|
|
1460 | |
|
|
1461 | 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>. |
|
|
1463 | |
|
|
1464 | If you need C<LC_NUMERIC>, you should enable it only around the code that |
|
|
1465 | actually needs it (avoiding stringification of numbers), and restore it |
|
|
1466 | afterwards. |
|
|
1467 | |
|
|
1468 | |
1434 | =head1 BUGS |
1469 | =head1 BUGS |
1435 | |
1470 | |
1436 | While the goal of this module is to be correct, that unfortunately does |
1471 | While the goal of this module is to be correct, that unfortunately does |
1437 | not mean it's bug-free, only that I think its design is bug-free. If you |
1472 | not mean it's bug-free, only that I think its design is bug-free. If you |
1438 | keep reporting bugs they will be fixed swiftly, though. |
1473 | keep reporting bugs they will be fixed swiftly, though. |