|
| 1 | +### Problem |
| 2 | + |
| 3 | +When sending metadata along with arbitrary JSON objects, |
| 4 | +a collision of property names may occur. For example, the |
| 5 | +driver system can't place a "type" property on an arbitrary |
| 6 | +response coming from a driver because that might also be |
| 7 | +the name of a property in the response. |
| 8 | + |
| 9 | + |
| 10 | +#### Example: |
| 11 | +```json |
| 12 | +{ |
| 13 | + "type": "api:thing", |
| 14 | + "version": "v1.0.0", |
| 15 | + "some": "info" |
| 16 | +} |
| 17 | +``` |
| 18 | + |
| 19 | +#### Awful Solution |
| 20 | + |
| 21 | +Reserved words. Drivers need to know their response can't have |
| 22 | +keys like `type` or `version`. If we'd like to add more meta |
| 23 | +keys in the future we need to verify that no existing drivers |
| 24 | +use the new key we'd like to reserve. If we have have such features |
| 25 | +as user-submitted drivers this will be impossibe. |
| 26 | +A `meta` key as a single reserved word could work, which is one |
| 27 | +of the solutions discussed below. |
| 28 | + |
| 29 | +#### Obvious Solution: |
| 30 | + |
| 31 | +The obvious solution is to return an object with a |
| 32 | +`head` property and a `body` propery: |
| 33 | + |
| 34 | +```json |
| 35 | +{ |
| 36 | + "head": { |
| 37 | + "type": "api:thing", |
| 38 | + "version": "v1.0.0" |
| 39 | + }, |
| 40 | + "body": { |
| 41 | + "some": "info" |
| 42 | + } |
| 43 | +} |
| 44 | +``` |
| 45 | + |
| 46 | +I don't mind this solution. I've come up with some alternatives though, |
| 47 | +because this solution has a couple drawbacks: |
| 48 | +- it looks a little verbose |
| 49 | +- it's not backwards-compatible with arbitrary JSON-object responses |
| 50 | + |
| 51 | +## Solutions |
| 52 | + |
| 53 | +### Dollar-Sign Convention |
| 54 | + |
| 55 | +- Objects have two classes of keys: |
| 56 | + - "meta" keys begin with "$" |
| 57 | + - other keys must validate against the |
| 58 | + usual identifier rules: `/[A-Za-z_][A-Za-z0-9_]*/` |
| 59 | +- The meta key `$` indicates the schema or class of |
| 60 | + the object. |
| 61 | +- Example: |
| 62 | + ```json |
| 63 | + { |
| 64 | + "$": "api:thing", |
| 65 | + "$version": "v1.0.0", |
| 66 | + |
| 67 | + "some": "info" |
| 68 | + } |
| 69 | + ``` |
| 70 | +- what sucks about it: |
| 71 | + - `$` might be surprising or confusing |
| 72 | + - response is a subset of valid JSON keys |
| 73 | + (those not including `$`) |
| 74 | +- what's nice about it: |
| 75 | + - backwards-compatible with arbitrary JSON-object responses |
| 76 | + which don't already use `$` |
| 77 | + |
| 78 | +### Underscore Convention |
| 79 | +- Same as above, but `_` instead of `$` |
| 80 | + ```json |
| 81 | + { |
| 82 | + "_": "api:thing", |
| 83 | + "_version": "v1.0.0", |
| 84 | + |
| 85 | + "some": "info" |
| 86 | + } |
| 87 | + ``` |
| 88 | +- what sucks about it: |
| 89 | + - `_` might be confusing |
| 90 | + - response is a subset of valid JSON keys |
| 91 | + (those not including `_`) |
| 92 | +- what's nice about it: |
| 93 | + - `_` is conventionally used for private property names, |
| 94 | + so this might be a little less surprising |
| 95 | + - backwards-compatible with arbitrary JSON-object responses |
| 96 | + which don't already use `_` |
| 97 | + |
| 98 | +### Nesting Convention, simplified |
| 99 | + |
| 100 | +- Similar to the "obvious solution" except |
| 101 | + metadata fields are lifted up a level. |
| 102 | + It's relatively inconsequential if meta keys |
| 103 | + have reserved words compared to value keys. |
| 104 | + ```json |
| 105 | + { |
| 106 | + "type": "api:thing", |
| 107 | + "version": "v1.0.0", |
| 108 | + "value": { |
| 109 | + "some": "info" |
| 110 | + } |
| 111 | + } |
| 112 | + ``` |
| 113 | + |
| 114 | +### Modified Dollar/Underscore convention |
| 115 | +- Using `_` in this example, but instead of prefixing |
| 116 | + meta properties they all go under one key. |
| 117 | + ```json |
| 118 | + { |
| 119 | + "_": { |
| 120 | + "type": "api:thing", |
| 121 | + "version": "v1.0.0" |
| 122 | + }, |
| 123 | + |
| 124 | + "some": "info" |
| 125 | + } |
| 126 | + ``` |
| 127 | +- what sucks about it: |
| 128 | + - `_` might be confusing |
| 129 | + - response is a subset of valid JSON keys |
| 130 | + (those not **exactly** `_`) |
| 131 | +- what's nice about it: |
| 132 | + - `_` is conventionally used for private property names, |
| 133 | + so this might be a little less surprising |
| 134 | + - backwards-compatible with arbitrary JSON-object responses |
| 135 | + which don't already use `_` as an exact key |
| 136 | + - only one reserved key |
0 commit comments