Fixing JSON

Datetime:2016-08-22 22:58:42         Topic:         Share        Original >>
Here to See The Original Article!!!

I’ve edit­ed a cou­ple of the JSON RFCs, and am work­ing on the de­sign of a fair­ly com­plex DSL, so I think I can claim to have dug deep­er in the JSON mines than most. We can eas­i­ly agree on what’s wrong with JSON, and I can’t help won­der­ing if it’d be worth fix­ing it.

Ma­jor ir­ri­tan­t: Com­mas · Hand-editing JSON may not be the most im­por­tant way of in­ter­act­ing with it, but it shouldn’t be as hard as it is. In par­tic­u­lar, when I’m mov­ing things around in a chunk of JSON I can nev­er, as in NEVER , get the com­mas right.

The fix is easy: Just re­move them. They’re inessen­tial to the gram­mar, just there for JavaScript com­pat­i­bil­i­ty.

Al­ter­na­tive­ly, you could make them op­tion­al, or you could al­low a com­ma af­ter the last mem­ber of an ar­ray or ob­jec­t. But, in In­ter­net pro­to­col­s, less is more. Just nuk­ing the com­mas and re­quir­ing whites­pace sep­a­ra­tors is the best way for­ward. Like so:

  "IDs": [116 943 234 38793]
  "Settings": { "Aperture": 2.8 "Shutter speed": "1/250" }

Irritant: Timestamps · JSON is chiefly used, these days, in HTTP requests and results. Among the RESTful APIs that I can think of, exactly zero don’t have timestamps.

This one is easy to fix: Just introduce them already. Use the well-established well-debugged RFC 3339 format, insisting on capital T and Z.

Strictly speaking you don’t need any syntax because grammatically a timestamp can’t be a number and a number can’t be a timestamp. However, I think a syntactic signal might be nice: I was thinking of prefixing timestamps with “@”, like so:

{ "Capture Time": @2016-08-01T18:15:00Z }

Here’s the canonical example of a JSON object, originally due to Doug Crockford, as modified in RFC 7159, and slightly extended to illustrate the cleanups I’m talking about.

     "Image": {
         "Width":  800
         "Height": 600
         "Title":  "View from 15th Floor"
         "Thumbnail": {
             "Url":    ""
             "Height": 125
             "Width":  100
         "Animated" : false
         "Capture Time": @2016-08-01T18:15:00Z
         "IDs": [ 116 943 234 38793 ]

Major irritant: Schemas · Specifying a JSON DSL is a major pain in the ass. JSON Schema will get you part of the way there, but if you use its modularity features, centered around “anyOf” , it becomes very hard for implementations to generate helpful high-quality error messages. The world has room for something considerably better, and I may be driven to a proposal myself.

But those syntax-irritant fruits around commas and timestamps hang so low that it’d be lovely to grab ’em. I raised the idea in the IETF JSON working group and the consensus seemed to be “It’s not that terrible, there are too many markup-language proposals already, just live with it.” Feaugh.

Put your ads here, just $200 per month.