--- JSON-XS/README 2008/03/27 06:37:35 1.24 +++ JSON-XS/README 2008/07/15 11:29:29 1.27 @@ -34,7 +34,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 + be 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 @@ -59,7 +59,7 @@ * round-trip integrity - When you serialise a perl data structure using only datatypes + When you serialise a perl data structure using only data types 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 *are* exceptions @@ -80,12 +80,12 @@ * simple to use This module has both a simple functional interface as well as an - objetc oriented interface interface. + object oriented interface interface. * 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 @@ -103,7 +103,7 @@ $json_text = JSON::XS->new->utf8->encode ($perl_scalar) - except being faster. + Except being faster. $perl_scalar = decode_json $json_text The opposite of "encode_json": expects an UTF-8 (binary) string and @@ -114,7 +114,7 @@ $perl_scalar = JSON::XS->new->utf8->decode ($json_text) - except being faster. + Except being faster. $is_boolean = JSON::XS::is_bool $scalar Returns true if the passed scalar represents either JSON::XS::true @@ -154,7 +154,7 @@ doesn't exist. 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. @@ -400,6 +400,21 @@ JSON::XS->new->allow_nonref->encode ("Hello, World!") => "Hello, World!" + $json = $json->allow_unknown ([$enable]) + $enabled = $json->get_allow_unknown + If $enable is true (or missing), then "encode" will *not* throw an + exception when it encounters values it cannot represent in JSON (for + example, filehandles) but instead will encode a JSON "null" value. + Note that blessed objects are not included here and are handled + separately by c. + + If $enable is false (the default), then "encode" will throw an + exception when it encounters anything it cannot encode as JSON. + + This option does not affect "decode" in any way, and it is + recommended to leave it off unless you know your communications + partner. + $json = $json->allow_blessed ([$enable]) $enabled = $json->get_allow_blessed If $enable is true (or missing), then the "encode" method will not @@ -543,9 +558,9 @@ $json = $json->max_depth ([$maximum_nesting_depth]) $max_depth = $json->get_max_depth Sets the maximum nesting level (default 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 @@ -555,9 +570,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 "max_depth" 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. @@ -566,14 +584,12 @@ $max_size = $json->get_max_size Set the maximum length a JSON text may have (in bytes) where decoding is being attempted. The default is 0, meaning no limit. - When "decode" is called on a string longer then this number of - characters it will not attempt to decode the string but throw an + When "decode" 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 "encode" (yet). - The argument to "max_size" will be rounded up to the next highest - power of two (so may be more than requested). If no argument is - given, the limit check will be deactivated (same as when 0 is - specified). + If no argument is given, the limit check will be deactivated (same + as when 0 is specified). See SECURITY CONSIDERATIONS, below, for more info on why this is useful. @@ -609,19 +625,25 @@ => ([], 3) INCREMENTAL PARSING - [This section and the API it details 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 "decode_prefix" 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). + much more efficient (and can be implemented with a minimum of method + calls). - The following two methods deal with this. + 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 parenthese mismatches. + 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. "max_size") to ensure the parser will stop parsing + in the presence if syntax errors. + + The following methods implement this incremental parser. [void, scalar or list context] = $json->incr_parse ([$string]) This is the central parsing function. It can both append new text @@ -671,6 +693,14 @@ parser state is left unchanged, to skip the text parsed so far and to reset the parse state. + $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 ot repeatedly parse JSON objects and want + to ignore any trailing data, which means you have to reset the + parser after each successful decode. + LIMITATIONS All options that affect decoding are supported, except "allow_nonref". The reason for this is that it cannot be made to work sensibly: JSON @@ -899,7 +929,7 @@ can also use "JSON::XS::false" and "JSON::XS::true" to improve readability. - encode_json [\0,JSON::XS::true] # yields [false,true] + encode_json [\0, JSON::XS::true] # yields [false,true] JSON::XS::true, JSON::XS::false These special values become JSON true and JSON false values, @@ -1099,8 +1129,9 @@ single-line JSON string (also available at ). - {"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, + true, false]} It shows the number of encodes/decodes per second (JSON::XS uses the functional interface, while JSON::XS/2 uses the OO interface with @@ -1203,9 +1234,8 @@ 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.