However, when Google's Chrome web browser read a Bookmarks file which
I wrote with BSJSONAdditions, it garbled any non-ASCII characters.
When it writes such characters, it encodes them as \uXXXX. Thus, in
Google's implementation, *may* is *shall*.
So I did the TODO. The diff'ed code supports up to two-byte
characters, i.e. those in the Basic Multilingual Plane, Plane 0, U
+0000 to U+FFFF. Since Google Chrome encodes bookmark names
internally as C++ wchar, which are 16-bit, this seems to be sufficient
for my purposes. And my Mac doesn't seem to have a font for
displaying anything outside the Basic Multilingual Plane anyhow. But,
just for the record, three-byte Unicodes in planes 1-17 are still
TODO.
Instead of changing the code as I did, or forking BSJSONAdditions, one
might want a parameter added on the encoding methods, something like
(BOOL)backslashEncodeNonAsciiCharacters. However, since the standard
specifies that any character *may* be encoded, the output of my
revised code is legal; the only disadvantage is that some non-ASCII
characters may take a few more bytes because of the \u.
By the way, just to clarify, BSJSONAdditions already supports
*decoding* \uXXXX substitutions in the JSON-to-NSDictionary
direction. What I'm writing about in this note is the NSDictionary-to-
JSON direction.
All changes are in NSString+BSJSONAdditions.m. The diff is below.
Code has been tested on about a dozen JSON files and is less than one
day old. Pretty simple, but let me know if you find any bugs.
32a33,34
> if (nextChar < 128) {
> // It's ASCII
58,62d59
< /* TODO: Find and encode unicode characters here?
< case '\u':
< [jsonString appendString:@"\\n"];
< break;
< */
66a64,72
> } else {
> // It's non-ASCII
> [jsonString appendFormat:@"\\u%04x", nextChar] ;
> // This supports up to two-byte Unicode characters, i.e. those
> // in the range U+0000 to U+FFFF, i.e. those in the Basic
> // Multilingual Plane, Plane 0.
> // TODO: Support for three-byte Unicode characters, i.e. those
> // at code points > U+FFFF, i.e. those in Planes 1-16.
> }