ubjson / universal-binary-json

Community workspace for the Universal Binary JSON Specification.
115 stars 12 forks source link

Allow to use Char type in object keys #52

Closed dmitry-ra closed 10 years ago

dmitry-ra commented 10 years ago

Proposal

Allow to use Char type in object keys (names) when key is 1 symbol from ASCII range.

String:

[{]
    [i][1][x][i][1]
[}]

Char:

[{]
    [C][x][i][1]
[}]

Currently this feature implemented in online demo: [http://dmitry-ra.github.io/ubjson-test-suite/json-converter.html#{"long name": 1, "x": 2}](http://dmitry-ra.github.io/ubjson-test-suite/json-converter.html#{"long name": 1, "x": 2})

When reader reading the name, it should expect numeric type or char type. So it would be string in first case and 1-symbol name in second case.

kxepal commented 10 years ago

This feature was eventually deprecated and removed from spec somewhere between Draft 9 and Draft 10. See #46 for related discussion and backlog.

ghost commented 10 years ago

@dmitry-ra We definitely understand the desire to do this optimization, but we determined that it introduces ambiguity into the spec at the savings of 1 byte and we didn't think it worth it.

The fight against ambiguity is to make things more consistent and easier on impl providers to not put bugs into the code - the spec needs to be clear and absolute, with little to no room for error or misinterpretation and we didn't think the [C] win was big enough in this case to do that.

As Alex mentions, #46 has most of the backstory, but thank you for the suggestion!

dmitry-ra commented 10 years ago

Now Char automatically used only in object values and array items. It's correct implementation? Or library users should explicitly set Char type (call writeChar function instead of writeString, for example)?

=== Test case 1 (keys) ===

JSON:
{
  "abc":3,
  "ab":2,
  "a":1,
  "":0,
  "я":"non-ascii key"
}

UBJSON:
[{]
    [i][3][abc][i][3]
    [i][2][ab][i][2]
    [i][1][a][i][1]
    [i][0][i][0]
    [i][2][я][S][i][13][non-ascii key]
[}]

Interactive Test case 1

=== Test case 2 (values) ===

JSON:
{
  "str2":"ab",
  "ascii1-as-char":"a",
  "non-ascii-symbol":"я",
  "empty":"",
  "arr":[
    "",
    "a",
    "ab",
    "я"
  ]
}

UBJSON:
[{]
    [i][4][str2][S][i][2][ab]
    [i][14][ascii1-as-char][C][a]
    [i][16][non-ascii-symbol][S][i][2][я]
    [i][5][empty][S][i][0]
    [i][3][arr][[]
        [S][i][0]
        [C][a]
        [S][i][2][ab]
        [S][i][2][я]
    []]
[}]

Interactive Test case 2

ghost commented 10 years ago

@dmitry-ra It depends on the implementation you are referring to - both are supported as a valid value but are seen as different types (e.g. in a strongly typed array of type [S] you can't inject a [C] value)

In the Java API the OutputStream impl let's you call whichever method you want regardless of UBJSON structure - it is up to the caller (or another abstraction layer ontop of that OutputStream impl) to enforce those kinds of compatibility concerns.