Loading ...
Sorry, an error occurred while loading the content.

1587Re: JSON and the Unicode Standard

Expand Messages
  • johne_ganz
    Feb 25, 2011
      --- In json@yahoogroups.com, "Douglas Crockford" <douglas@...> wrote:
      > --- In json@yahoogroups.com, "johne_ganz" <john.engelhart@> wrote:
      > >
      > > --- In json@yahoogroups.com, "Douglas Crockford" <douglas@> wrote:
      > > >
      > > > A receiver can do what it chooses to with the character codes it receives. If it wants to delete them or reject them, that is its business.
      > >
      > > Not if it's Unicode. It is a common misconception that "Unicode" is just a set of code points, like say ASCII or EBCDIC.
      > For JSON's purpose, Unicode is just a set of code points.

      Not according to RFC 4627 it isn't. Section 3, Encoding, "JSON text SHALL be encoded in Unicode.", where SHALL is interpreted via RFC 2119 (i.e., SHALL is synonymous with MUST).

      I appreciate that your interpretation may have been your original intent, but the scope of the language in the standard is far, far greater than "JSON text SHALL be interpreted as a stream of disjoint Unicode code points.", which is what you are arguing that the standard means.

      Unless you can make a compelling argument with language from the RFC 4627 standard, the standard clearly and plainly says that the JSON text is encoded in Unicode. This means that the text must conform to the Unicode standard, and it's rules for processing and handling text MUST (via the use of SHALL in RFC 4627) be followed.

      > By receiver I mean the program that ultimately receives the message. It can interpret it and process it or damage it or ignore it as it will. What it does with the data is none of my business. The JSON channel itself must do none of those things.

      Surely you realize that in practice, this is not the way that things are done. All of the JSON libraries are effectively "part of the JSON channel".

      There is a clear demarcation point where a piece of text has ceased to be JSON and has (usually) become an instantiated data structure in the host language.

      How and what the "language" does with the data is not relevant to RFC 4627. The "language" may manipulate the JSON data, examining keys, manipulating them in any way it chooses. But at this point, it very clearly has ceased to be "JSON".

      Every JSON implementation that is in the form of a library for a host language that I'm aware of could be interpreted to be "the program that ultimately receives the message". The libraries parse the JSON and transliterate it in to a form useable by the host language. How and what the host language, or program written by someone to enumerate or manipulate the data structure that was instantiated from the original JSON is obviously outside the scope of RFC 4627.

      My pedantic point is: A JSON implementation, in the form of a library that provides bindings between a host language and JSON (of which there are many), MUST NOT arbitrarily delete characters in the original JSON. Furthermore, any such implementation MUST interpret the original JSON text in accordance with the Unicode Standard. Just like RFC 4627 gives a grammar and rules for how to interpret JSON, the Unicode Standard has rules for how to interpret text encoded as Unicode. Unicode is not just a simple set of code points.

      Another issue is normalization. In particular, the way normalization is handled for the "key" portion of an "object" (i.e., {"key": "value"}) can dramatically alter the meaning and contents of the object. For example:

      "\u212b": "one",
      "\u0041\u030a": "two",
      "\u00c5": "three"

      Are these three keys distinct? Should there be a requirement that they MUST be handled and interpreted such that they are distinct? Does that requirement extend past the "channel" demarcation point (i.e., not a JSON library or communication channel used to interchange the JSON between two hosts) to the "host language"?

      In case it is not obvious, under the rules of Unicode NFC (Normalization Form C), all three of the keys above will become "\u00c5" after NFC processing.

      A first order approximation would seem to suggest that a JSON implementation "should" use the precomposed form for keys, and for objects that contain keys with non-precomposed keys that, when converted to their precomposed form are duplicate with other keys, the behavior is undefined.

      Again, this is another point where the use of Unicode introduces an awful lot of non-obvious dependencies. The Unicode standard has a lot to say about what it means for two strings to "compare equal", and since JSON specifies what is essentially a key/value hash table, it is critically important to define what "equal" means for a key. If the keys were ASCII or Binary, this would probably be a non-issue, but its a pretty big one when you're dealing with Unicode.

      > Tell you what. If you ever encounter a real problem, we will deal with that.

      This is a rather snarky comment, and to be blunt, unprofessional and unfair.

      Every point I've raised here is something that an implementor of a JSON library will likely encounter. As an implementor of such a library (for Objective-C), everything I've raised here is something that took an enormous amount of time and consideration.

      In my case, I've had to deal with the subtle nuances of what happens to a Unicode string when I parse it and then hand that parsed string off to another library to instantiate a string object. I have no control over how this external library (a combination of Foundation and Core Foundation) deals with or interprets various aspects of the Unicode Standard. For the sake of argument, if this external library automatically precomposes all strings it instantiates, and I have to uses those instantiated strings as the keys in a NSDictionary (the equivalent of a JSON object), I've got some problems.

      Your snarky comment ignores the real world complexities that one faces when attempting to create a "RFC 4627 compliant" JSON implementation, at least if one is trying to do so "the right way" as opposed to a quick hack JSON implementation.

      For someone who is creating a JSON library or some other form of a JSON implementation, the corner cases are usually far more important than the obvious, common case.
    • Show all 35 messages in this topic