Jump to content

Requests for comment/More compact JSON API output

From mediawiki.org
Request for comment (RFC)
More compact JSON API output
Component General
CreationĀ date
Author(s) MaxSem
Document status declined
This was superseded by the "utf8" parameter which was added in March 2013 by Kevin Israel (79f80cc49571e286b89fc1666c034262611fac7f) -- Tim Starling (talk) 03:34, 18 September 2013 (UTC)[reply]

Problem

[edit]

Sample piece of action=parse output on zhwiki: <b>\u5973\u6027<\/b>\u662f\u6307<a href=\"\/wiki\/%E9%9B%8C%E6%80%A7\" title=\"\u96cc\u6027\">\u96cc\u6027<\/a>\u7684<a href=\"\/wiki\/%E4%BA%BA\" title=\"\u4eba\">\u4eba\u985e<\/a>\uff0c\u8207<a href=\"\/wiki\/%E7%94%B7%E6%80%A7\" title=\"\u7537\u6027\">\u7537\u6027<\/a>\uff0c\u4e5f\u5c31\u662f<a href=\"\/wiki\/%E9%9B%84%E6%80%A7\" title=\"\u96c4\u6027\">\u96c4\u6027<\/a>\u4eba\u985e\u6210\u5c0d\u6bd4\u3002\u5973\u6027\u9019\u500b\u540d\u8a5e\u662f\u7528\u4f86\u8868\u793a<a href=\"\/wiki\/%E7%94%9F%E7%89%A9%E5%AD%A6\" title=\"\u751f\u7269\u5b66\">\u751f\u7269\u5b78<\/a>

And list=allpages query on en:

{
	"pageid": 5878274,
	"ns": 0,
	"title": "!"
},
{
	"pageid": 3632887,
	"ns": 0,
	"title": "!!"
},
{
	"pageid": 600744,
	"ns": 0,
	"title": "!!!"
},

We can see that even though it is a valid JSON, it is bulky if compared to what JavaScript aloows it to be.

Proposed solution

[edit]

I propose to add a new format, jsoncompact, or, for compactness, jsonc, whose output should be much shorter for some use cases as the price of being a valid JavaScript but not necessarily valid JSON. A few things that can be done:

  • Example 1, don't escape Unicode: {"foo":"\u043f\u0440\u0435\u0432\u0435\u0434"} ā†’ {"foo":"ŠæрŠµŠ²ŠµŠ“"} (24 bytes less).
  • Example 2, don't quote keys if possible: {"foo":"bar"} ā†’ {foo:"bar"} (2 bytes less).

The second example is much less tolerated by different parsers so I'm not proposing to use it, though even it can save 3000 bytes

Compatibility matrix

[edit]
Parser Unquoted keys Unescaped UTF-8
JS eval() + +
jQuery.parseJSON() - +
PHP json_decode() - +