|
| 1 | +- Start Date: 2017-03-27 |
| 2 | +- RFC PR: (leave this empty) |
| 3 | +- Refract Issue: (leave this empty) |
| 4 | + |
| 5 | +# Refract Full JSON Serialisation |
| 6 | + |
| 7 | +## Summary |
| 8 | + |
| 9 | +Full Refract serialisation is not clearly defined in the Refract specification |
| 10 | +and there are a few ambiguities that may occur when parsing a Refract document. |
| 11 | + |
| 12 | +## Motivation |
| 13 | + |
| 14 | +It should be clear to consumers how to serialise and de-serialise refracted |
| 15 | +elements. |
| 16 | + |
| 17 | +At the moment there are no formal rules for full serialisation and some |
| 18 | +implementations are inconsistent. Many Refract serialisers support embedding |
| 19 | +both refracted and raw JSON data in a Refract document, however many |
| 20 | +de-serialisers and consumers of Refract do not support both forms in all |
| 21 | +places. |
| 22 | + |
| 23 | +Since an element does not need to be refracted, that leads to unrefracted |
| 24 | +elements being used in some cases causing ambiguity and leads to consumers |
| 25 | +having to know details about a Refract namespace to be able to truely parse it. |
| 26 | + |
| 27 | +It should be possible for a Refract consumer to consume a Refract document |
| 28 | +without knowing about specific elements from a namespace. There should be no |
| 29 | +ambiguities when parsing a Refract document. |
| 30 | + |
| 31 | +## Detailed design |
| 32 | + |
| 33 | +We should provide a full specification on how Refract elements can be |
| 34 | +serailised as JSON and recommendations on how to consume Refract elements in |
| 35 | +JSON. |
| 36 | + |
| 37 | +### JSON Serialisation |
| 38 | + |
| 39 | +A Refract element MUST be serialised as JSON as follows: |
| 40 | + |
| 41 | +- element (required, string) |
| 42 | +- meta (optional) |
| 43 | +- attributes (optional) |
| 44 | +- content (optional) |
| 45 | + |
| 46 | +The name of the element is always required, however other JSON values may be |
| 47 | +omitted, for example if there is no meta keys then it SHOULD be omitted from |
| 48 | +the JSON object. |
| 49 | + |
| 50 | +#### Content |
| 51 | + |
| 52 | +The content inside JSON Serialisation MUST always be a Refracted element, an |
| 53 | +array of Refracted element or a primitive type such as string, number, boolean |
| 54 | +or none. The only exceptions to this rule is for any element types described |
| 55 | +within the Refract specification. |
| 56 | + |
| 57 | +These same rules would apply to meta and attribute values of an Element. They |
| 58 | +must also always be Refracted. |
| 59 | + |
| 60 | +For example, it is NOT possible to serialise an arbitrary object inside the |
| 61 | +content value for elements not defined within the base Refract specification. |
| 62 | +Otherwise it can be ambiguous for consumers whether the JSON object is another |
| 63 | +element or JSON data. |
| 64 | + |
| 65 | +As example, the following is NOT permitted: |
| 66 | + |
| 67 | +```json |
| 68 | +{ |
| 69 | + "element": "custom", |
| 70 | + "content": { |
| 71 | + "content": "abc" |
| 72 | + } |
| 73 | +} |
| 74 | +``` |
| 75 | + |
| 76 | +Instead, a `custom` element with the content of an object with `content` = |
| 77 | +`abc` should be serialised as follows where the content is a Refract element: |
| 78 | + |
| 79 | +```json |
| 80 | +{ |
| 81 | + "element": "custom", |
| 82 | + "content": { |
| 83 | + "element": "object", |
| 84 | + "content": [ |
| 85 | + { |
| 86 | + "element": "member", |
| 87 | + "content": { |
| 88 | + "key": { |
| 89 | + "element": "string", |
| 90 | + "content": "content" |
| 91 | + }, |
| 92 | + "value": { |
| 93 | + "element": "string", |
| 94 | + "content": "abc" |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + ] |
| 99 | + } |
| 100 | +} |
| 101 | +``` |
| 102 | + |
| 103 | +Another example of a serialisation that is NOT permitted would be an array of a |
| 104 | +primitive type. For example: |
| 105 | + |
| 106 | +```json |
| 107 | +{ |
| 108 | + "element": "custom", |
| 109 | + "content": [ |
| 110 | + "abc" |
| 111 | + ] |
| 112 | +} |
| 113 | +``` |
| 114 | + |
| 115 | +Instead, a `custom` element with the content of a array of primitive types |
| 116 | +should be an array of Refracted elements as follows: |
| 117 | + |
| 118 | +```json |
| 119 | +{ |
| 120 | + "element": "custom", |
| 121 | + "content": [ |
| 122 | + { |
| 123 | + "element": "string", |
| 124 | + "content": "abc" |
| 125 | + } |
| 126 | + ] |
| 127 | +} |
| 128 | +``` |
| 129 | + |
| 130 | +## Drawbacks |
| 131 | + |
| 132 | +Forcing all elements to be fully refracted can lead to bloated documents and |
| 133 | +increase document sizes for Refract. We should offer compact Refract |
| 134 | +serialisation when size is important. |
| 135 | + |
| 136 | +## Alternatives |
| 137 | + |
| 138 | +Another design in https://github.com/refractproject/rfcs/pull/17 has been |
| 139 | +proposed, this RFC is similar except it has addressed some feedback from other |
| 140 | +Refract developers and does not cover compact Refract serialisation. |
0 commit comments