--- JSON-XS/XS.pm 2008/03/26 01:43:14 1.97 +++ JSON-XS/XS.pm 2011/02/21 15:38:06 1.134 @@ -1,9 +1,9 @@ =head1 NAME -=encoding utf-8 - JSON::XS - JSON serialising/deserialising, done correctly and fast +=encoding utf-8 + JSON::XS - 正しくて高速な JSON シリアライザ/デシリアライザ (http://fleur.hio.jp/perldoc/mix/lib/JSON/XS.html) @@ -39,7 +39,7 @@ Beginning with version 2.0 of the JSON module, when both JSON and JSON::XS are installed, then JSON will fall back on JSON::XS (this can be -overriden) with no overhead due to emulation (by inheritign constructor +overridden) with no overhead due to emulation (by inheriting constructor and methods). If JSON::XS is not available, it will fall back to the compatible JSON::PP module as backend, so using JSON instead of JSON::XS gives you a portable JSON API that can be fast when you need and doesn't @@ -51,8 +51,6 @@ their maintainers are unresponsive, gone missing, or not listening to bug reports for other reasons. -See COMPARISON, below, for a comparison to some other JSON modules. - See MAPPING, below, on how JSON::XS maps perl values to JSON values and vice versa. @@ -67,11 +65,11 @@ =item * round-trip integrity -When you serialise a perl data structure using only datatypes supported -by JSON, the deserialised data structure is identical on the Perl level. -(e.g. the string "2.0" doesn't suddenly become "2" just because it looks -like a number). There minor I exceptions to this, read the MAPPING -section below to learn about those. +When you serialise a perl data structure using only data types supported +by JSON and Perl, the deserialised data structure is identical on the Perl +level. (e.g. the string "2.0" doesn't suddenly become "2" just because +it looks like a number). There I minor exceptions to this, read the +MAPPING section below to learn about those. =item * strict checking of JSON correctness @@ -86,13 +84,13 @@ =item * simple to use -This module has both a simple functional interface as well as an objetc +This module has both a simple functional interface as well as an object oriented interface interface. =item * reasonably versatile output formats You can choose between the most compact guaranteed-single-line format -possible (nice for simple line-based protocols), a pure-ascii format +possible (nice for simple line-based protocols), a pure-ASCII format (for when your transport is not 8-bit clean, still supports the whole Unicode range), or a pretty-printed format (for when you want to read that stuff). Or you can combine those features in whatever way you like. @@ -103,9 +101,9 @@ package JSON::XS; -use strict; +use common::sense; -our $VERSION = '2.1'; +our $VERSION = '2.3'; our @ISA = qw(Exporter); our @EXPORT = qw(encode_json decode_json to_json from_json); @@ -139,7 +137,7 @@ $json_text = JSON::XS->new->utf8->encode ($perl_scalar) -except being faster. +Except being faster. =item $perl_scalar = decode_json $json_text @@ -151,7 +149,7 @@ $perl_scalar = JSON::XS->new->utf8->decode ($json_text) -except being faster. +Except being faster. =item $is_boolean = JSON::XS::is_bool $scalar @@ -199,7 +197,7 @@ exist. =item 4. A "Unicode String" is simply a string where each character can be -validly interpreted as a Unicode codepoint. +validly interpreted as a Unicode code point. If you have UTF-8 encoded data, it is no longer a Unicode string, but a Unicode string encoded in UTF-8, giving you a binary string. @@ -445,6 +443,8 @@ This setting has no effect when decoding JSON texts. +This setting has currently no effect on tied hashes. + =item $json = $json->allow_nonref ([$enable]) =item $enabled = $json->get_allow_nonref @@ -465,6 +465,22 @@ JSON::XS->new->allow_nonref->encode ("Hello, World!") => "Hello, World!" +=item $json = $json->allow_unknown ([$enable]) + +=item $enabled = $json->get_allow_unknown + +If C<$enable> is true (or missing), then C will I throw an +exception when it encounters values it cannot represent in JSON (for +example, filehandles) but instead will encode a JSON C value. Note +that blessed objects are not included here and are handled separately by +c. + +If C<$enable> is false (the default), then C will throw an +exception when it encounters anything it cannot encode as JSON. + +This option does not affect C in any way, and it is recommended to +leave it off unless you know your communications partner. + =item $json = $json->allow_blessed ([$enable]) =item $enabled = $json->get_allow_blessed @@ -614,9 +630,9 @@ =item $max_depth = $json->get_max_depth Sets the maximum nesting level (default C<512>) accepted while encoding -or decoding. If the JSON text or Perl data structure has an equal or -higher nesting level then this limit, then the encoder and decoder will -stop and croak at that point. +or decoding. If a higher nesting level is detected in JSON text or a Perl +data structure, then the encoder and decoder will stop and croak at that +point. Nesting level is defined by number of hash- or arrayrefs that the encoder needs to traverse to reach a given point or the number of C<{> or C<[> @@ -626,9 +642,12 @@ Setting the maximum depth to one disallows any nesting, so that ensures that the object is only a single hash/object or array. -The argument to C will be rounded up to the next highest power -of two. If no argument is given, the highest possible setting will be -used, which is rarely useful. +If no argument is given, the highest possible setting will be used, which +is rarely useful. + +Note that nesting is implemented by recursion in C. The default value has +been chosen to be as large as typical operating systems allow without +crashing. See SECURITY CONSIDERATIONS, below, for more info on why this is useful. @@ -638,13 +657,12 @@ Set the maximum length a JSON text may have (in bytes) where decoding is being attempted. The default is C<0>, meaning no limit. When C -is called on a string longer then this number of characters it will not +is called on a string that is longer then this many bytes, it will not attempt to decode the string but throw an exception. This setting has no effect on C (yet). -The argument to C will be rounded up to the next B -power of two (so may be more than requested). If no argument is given, the -limit check will be deactivated (same as when C<0> is specified). +If no argument is given, the limit check will be deactivated (same as when +C<0> is specified). See SECURITY CONSIDERATIONS, below, for more info on why this is useful. @@ -685,19 +703,25 @@ =head1 INCREMENTAL PARSING -[This section is still EXPERIMENTAL] - In some cases, there is the need for incremental parsing of JSON texts. While this module always has to keep both JSON text and resulting Perl data structure in memory at one time, it does allow you to parse a JSON stream incrementally. It does so by accumulating text until it has a full JSON object, which it then can decode. This process is similar to -using C to see if a full JSON object is available, but is -much more efficient (JSON::XS will only attempt to parse the JSON text -once it is sure it has enough text to get a decisive result, using a very -simple but truly incremental parser). +using C to see if a full JSON object is available, but +is much more efficient (and can be implemented with a minimum of method +calls). + +JSON::XS will only attempt to parse the JSON text once it is sure it +has enough text to get a decisive result, using a very simple but +truly incremental parser. This means that it sometimes won't stop as +early as the full parser, for example, it doesn't detect mismatched +parentheses. The only thing it guarantees is that it starts decoding as +soon as a syntactically valid JSON text has been seen. This means you need +to set resource limits (e.g. C) to ensure the parser will stop +parsing in the presence if syntax errors. -The following two methods deal with this. +The following methods implement this incremental parser. =over 4 @@ -729,6 +753,11 @@ case. Note that in this case, any previously-parsed JSON texts will be lost. +Example: Parse some JSON arrays/objects in a given string and return +them. + + my @objs = JSON::XS->new->incr_parse ("[5][7][1,2]"); + =item $lvalue_string = $json->incr_text This method returns the currently stored JSON fragment as an lvalue, that @@ -745,10 +774,23 @@ =item $json->incr_skip -This will reset the state of the incremental parser and will remove the -parsed text from the input buffer. This is useful after C -died, in which case the input buffer and incremental parser state is left -unchanged, to skip the text parsed so far and to reset the parse state. +This will reset the state of the incremental parser and will remove +the parsed text from the input buffer so far. This is useful after +C died, in which case the input buffer and incremental parser +state is left unchanged, to skip the text parsed so far and to reset the +parse state. + +The difference to C is that only text until the parse error +occured is removed. + +=item $json->incr_reset + +This completely resets the incremental parser, that is, after this call, +it will be as if the parser had never parsed anything. + +This is useful if you want to repeatedly parse JSON objects and want to +ignore any trailing data, which means you have to reset the parser after +each successful decode. =back @@ -954,6 +996,11 @@ precision (in which case you might lose perfect roundtripping ability, but the JSON number will still be re-encoded as a JSON number). +Note that precision is not accuracy - binary floating point values cannot +represent most decimal fractions exactly, and when converting from and to +floating point, JSON::XS only guarantees precision up to but not including +the leats significant bit. + =item true, false These JSON atoms become C and C, @@ -999,7 +1046,7 @@ C<1>, which get turned into C and C atoms in JSON. You can also use C and C to improve readability. - encode_json [\0,JSON::XS::true] # yields [false,true] + encode_json [\0, JSON::XS::true] # yields [false,true] =item JSON::XS::true, JSON::XS::false @@ -1050,6 +1097,13 @@ if you need this capability (but don't forget to explain why it's needed :). +Note that numerical precision has the same meaning as under Perl (so +binary to decimal conversion follows the same rules as in Perl, which +can differ to other languages). Also, your perl interpreter might expose +extensions to the floating point numbers of your platform, such as +infinities or NaN's - these cannot be represented in JSON, and it is an +error to pass those in. + =back @@ -1151,6 +1205,71 @@ =back +=head2 JSON and ECMAscript + +JSON syntax is based on how literals are represented in javascript (the +not-standardised predecessor of ECMAscript) which is presumably why it is +called "JavaScript Object Notation". + +However, JSON is not a subset (and also not a superset of course) of +ECMAscript (the standard) or javascript (whatever browsers actually +implement). + +If you want to use javascript's C function to "parse" JSON, you +might run into parse errors for valid JSON texts, or the resulting data +structure might not be queryable: + +One of the problems is that U+2028 and U+2029 are valid characters inside +JSON strings, but are not allowed in ECMAscript string literals, so the +following Perl fragment will not output something that can be guaranteed +to be parsable by javascript's C: + + use JSON::XS; + + print encode_json [chr 0x2028]; + +The right fix for this is to use a proper JSON parser in your javascript +programs, and not rely on C (see for example Douglas Crockford's +F parser). + +If this is not an option, you can, as a stop-gap measure, simply encode to +ASCII-only JSON: + + use JSON::XS; + + print JSON::XS->new->ascii->encode ([chr 0x2028]); + +Note that this will enlarge the resulting JSON text quite a bit if you +have many non-ASCII characters. You might be tempted to run some regexes +to only escape U+2028 and U+2029, e.g.: + + # DO NOT USE THIS! + my $json = JSON::XS->new->utf8->encode ([chr 0x2028]); + $json =~ s/\xe2\x80\xa8/\\u2028/g; # escape U+2028 + $json =~ s/\xe2\x80\xa9/\\u2029/g; # escape U+2029 + print $json; + +Note that I: the above only works for U+2028 and +U+2029 and thus only for fully ECMAscript-compliant parsers. Many existing +javascript implementations, however, have issues with other characters as +well - using C naively simply I cause problems. + +Another problem is that some javascript implementations reserve +some property names for their own purposes (which probably makes +them non-ECMAscript-compliant). For example, Iceweasel reserves the +C<__proto__> property name for its own purposes. + +If that is a problem, you could parse try to filter the resulting JSON +output for these property strings, e.g.: + + $json =~ s/"__proto__"\s*:/"__proto__renamed":/g; + +This works because C<__proto__> is not valid outside of strings, so every +occurence of C<"__proto__"\s*:> must be a string used as property name. + +If you know of other incompatibilities, please let me know. + + =head2 JSON and YAML You often hear that JSON is a subset of YAML. This is, however, a mass @@ -1168,12 +1287,12 @@ This will I generate JSON texts that also parse as valid YAML. Please note that YAML has hardcoded limits on (simple) object key lengths that JSON doesn't have and also has different and incompatible -unicode handling, so you should make sure that your hash keys are -noticeably shorter than the 1024 "stream characters" YAML allows and that -you do not have characters with codepoint values outside the Unicode BMP -(basic multilingual page). YAML also does not allow C<\/> sequences in -strings (which JSON::XS does not I generate, but other JSON -generators might). +unicode character escape syntax, so you should make sure that your hash +keys are noticeably shorter than the 1024 "stream characters" YAML allows +and that you do not have characters with codepoint values outside the +Unicode BMP (basic multilingual page). YAML also does not allow C<\/> +sequences in strings (which JSON::XS does not I generate, but +other JSON generators might). There might be other incompatibilities that I am not aware of (or the YAML specification has been changed yet again - it does so quite often). In @@ -1202,6 +1321,12 @@ real compatibility for many I and trying to silence people who point out that it isn't true. +Addendum/2009: the YAML 1.2 spec is still incomaptible with JSON, even +though the incompatibilities have been documented (and are known to +Brian) for many years and the spec makes explicit claims that YAML is a +superset of JSON. It would be so easy to fix, but apparently, bullying and +corrupting userdata is so much easier. + =back @@ -1216,50 +1341,50 @@ a very short single-line JSON string (also available at L). - {"method": "handleMessage", "params": ["user1", "we were just talking"], \ - "id": null, "array":[1,11,234,-5,1e5,1e7, true, false]} + {"method": "handleMessage", "params": ["user1", + "we were just talking"], "id": null, "array":[1,11,234,-5,1e5,1e7, + 1, 0]} It shows the number of encodes/decodes per second (JSON::XS uses the functional interface, while JSON::XS/2 uses the OO interface with pretty-printing and hashkey sorting enabled, JSON::XS/3 enables -shrink). Higher is better: +shrink. JSON::DWIW/DS uses the deserialise function, while JSON::DWIW::FJ +uses the from_json method). Higher is better: - module | encode | decode | - -----------|------------|------------| - JSON 1.x | 4990.842 | 4088.813 | - JSON::DWIW | 51653.990 | 71575.154 | - JSON::PC | 65948.176 | 74631.744 | - JSON::PP | 8931.652 | 3817.168 | - JSON::Syck | 24877.248 | 27776.848 | - JSON::XS | 388361.481 | 227951.304 | - JSON::XS/2 | 227951.304 | 218453.333 | - JSON::XS/3 | 338250.323 | 218453.333 | - Storable | 16500.016 | 135300.129 | - -----------+------------+------------+ - -That is, JSON::XS is about five times faster than JSON::DWIW on encoding, -about three times faster on decoding, and over forty times faster -than JSON, even with pretty-printing and key sorting. It also compares -favourably to Storable for small amounts of data. + module | encode | decode | + --------------|------------|------------| + JSON::DWIW/DS | 86302.551 | 102300.098 | + JSON::DWIW/FJ | 86302.551 | 75983.768 | + JSON::PP | 15827.562 | 6638.658 | + JSON::Syck | 63358.066 | 47662.545 | + JSON::XS | 511500.488 | 511500.488 | + JSON::XS/2 | 291271.111 | 388361.481 | + JSON::XS/3 | 361577.931 | 361577.931 | + Storable | 66788.280 | 265462.278 | + --------------+------------+------------+ + +That is, JSON::XS is almost six times faster than JSON::DWIW on encoding, +about five times faster on decoding, and over thirty to seventy times +faster than JSON's pure perl implementation. It also compares favourably +to Storable for small amounts of data. Using a longer test string (roughly 18KB, generated from Yahoo! Locals search API (L). - module | encode | decode | - -----------|------------|------------| - JSON 1.x | 55.260 | 34.971 | - JSON::DWIW | 825.228 | 1082.513 | - JSON::PC | 3571.444 | 2394.829 | - JSON::PP | 210.987 | 32.574 | - JSON::Syck | 552.551 | 787.544 | - JSON::XS | 5780.463 | 4854.519 | - JSON::XS/2 | 3869.998 | 4798.975 | - JSON::XS/3 | 5862.880 | 4798.975 | - Storable | 4445.002 | 5235.027 | - -----------+------------+------------+ + module | encode | decode | + --------------|------------|------------| + JSON::DWIW/DS | 1647.927 | 2673.916 | + JSON::DWIW/FJ | 1630.249 | 2596.128 | + JSON::PP | 400.640 | 62.311 | + JSON::Syck | 1481.040 | 1524.869 | + JSON::XS | 20661.596 | 9541.183 | + JSON::XS/2 | 10683.403 | 9416.938 | + JSON::XS/3 | 20661.596 | 9400.054 | + Storable | 19765.806 | 10000.725 | + --------------+------------+------------+ Again, JSON::XS leads by far (except for Storable which non-surprisingly -decodes faster). +decodes a bit faster). On large strings containing lots of high Unicode characters, some modules (such as JSON::PC) seem to decode faster than JSON::XS, but the result @@ -1305,11 +1430,11 @@ If you are using JSON::XS to return packets to consumption by JavaScript scripts in a browser you should have a look at -L to see whether -you are vulnerable to some common attack vectors (which really are browser -design bugs, but it is still you who will have to deal with it, as major -browser developers care only for features, not about getting security -right). +L to +see whether you are vulnerable to some common attack vectors (which really +are browser design bugs, but it is still you who will have to deal with +it, as major browser developers care only for features, not about getting +security right). =head1 THREADS @@ -1325,9 +1450,8 @@ =head1 BUGS While the goal of this module is to be correct, that unfortunately does -not mean it's bug-free, only that I think its design is bug-free. It is -still relatively early in its development. If you keep reporting bugs they -will be fixed swiftly, though. +not mean it's bug-free, only that I think its design is bug-free. If you +keep reporting bugs they will be fixed swiftly, though. Please refrain from using rt.cpan.org or any other bug reporting service. I put the contact address into my modules for a reason.