--- JSON-XS/XS.pm 2009/02/17 23:41:20 1.116 +++ JSON-XS/XS.pm 2010/01/06 08:02:18 1.126 @@ -101,10 +101,9 @@ package JSON::XS; -no warnings; -use strict; +use common::sense; -our $VERSION = '2.232'; +our $VERSION = '2.27'; our @ISA = qw(Exporter); our @EXPORT = qw(encode_json decode_json to_json from_json); @@ -444,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 @@ -1211,7 +1212,8 @@ 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. +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: @@ -1220,32 +1222,20 @@ print JSON::XS->new->ascii->encode ([chr 0x2028]); -And if you are concerned about the size of the resulting JSON text, you -can run some regexes to only escape U+2028 and U+2029: - - use JSON::XS; +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; -This works because U+2028/U+2029 are not allowed outside of strings and -are not used for syntax, so replacing them unconditionally just works. - -Note, however, that fixing the broken JSON parser is better than working -around it in every other generator. The above regexes should work well in -other languages, as long as they operate on UTF-8. It is equally valid to -replace all occurences of U+2028/2029 directly by their \\u-escaped forms -in unicode texts, so they can simply be used to fix any parsers relying on -C by first applying the regexes on the encoded texts. - -Note also that the above only works for U+2028 and U+2029 and thus -only for fully ECMAscript-compliant parsers. Many existing javascript -implementations misparse other characters as well. Best rely on a good -JSON parser, such as Douglas Crockfords F, which escapes the -above and many more problematic characters properly before passing them -into C. +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 @@ -1280,12 +1270,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 @@ -1314,6 +1304,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