|
| 1 | +# JRef Specification |
| 2 | + |
| 3 | +## Introduction |
| 4 | +This specification defines the JSON Reference (JRef) media type. JRef is an |
| 5 | +extension of the [JSON](https://www.rfc-editor.org/rfc/rfc8259) media type that |
| 6 | +adds the "reference" type. A "reference" type represents a web-style |
| 7 | +uni-directional link to a location in either the current document or a different |
| 8 | +resource. |
| 9 | + |
| 10 | +NOTE: This document is written as a spec and therefore doesn't have commentary |
| 11 | +on why I made the decisions I made or what impacts they would have for the |
| 12 | +specifications that might be interested in adopting this spec. For that type of |
| 13 | +information see https://github.com/json-schema-org/referencing/issues/7. |
| 14 | + |
| 15 | +## Media Type |
| 16 | +The JRef media type is identified as `application/reference+json` and uses the |
| 17 | +`jref` file extension. |
| 18 | + |
| 19 | +### Fragments |
| 20 | +A URI [fragment](https://www.rfc-editor.org/rfc/rfc3986#section-3.5) that is |
| 21 | +empty or starts with a `/` MUST be interpreted as a [JSON |
| 22 | +Pointer](https://www.rfc-editor.org/rfc/rfc6901). A JSON Pointer fragment |
| 23 | +represents a secondary resource that is the result of applying the JSON Pointer |
| 24 | +to the full document. Implementations MUST raise an error if the JSON Pointer |
| 25 | +does not successfully resolve to a location within the document. |
| 26 | + |
| 27 | +Specifications that extend this media type MAY define semantics for fragments |
| 28 | +that are not JSON Pointers, but if the fragment semantics are not understood, |
| 29 | +implementations MUST raise an error. |
| 30 | + |
| 31 | +### Profiles |
| 32 | +This media type allows the "profile" media type parameter as defined by [RFC |
| 33 | +6906](https://www.rfc-editor.org/rfc/rfc6906). |
| 34 | + |
| 35 | +## The "reference" Type |
| 36 | + |
| 37 | +### Syntax |
| 38 | +A "reference" type is represented with the same syntax as a JSON object with a |
| 39 | +`$href` property whose value is string. Specifications that extend this media |
| 40 | +type MAY define semantics for additional properties, but implementations MUST |
| 41 | +raise an error if they encounter a property that is not defined. |
| 42 | + |
| 43 | +```json |
| 44 | +{ "$href": "https://example.com/example.json#/foo/bar" } |
| 45 | +``` |
| 46 | + |
| 47 | +Although the "reference" type has syntactic overlap with the JSON "object" type, |
| 48 | +it MUST NOT be interpreted as a JSON object. The "reference" type is a scalar |
| 49 | +value that can not be indexed into like a JSON object. |
| 50 | + |
| 51 | +A JSON object is considered of type "reference" rather than type "object" if it |
| 52 | +has a `$href` property and the value of that property is a string. |
| 53 | + |
| 54 | +### Following References |
| 55 | +The value of `$href` in a reference MUST be interpreted as a |
| 56 | +[URI-reference](https://www.rfc-editor.org/rfc/rfc3986#section-4.1). The process |
| 57 | +for determining the base URI for the document is defined by [RFC 3986 Section |
| 58 | +5.1](https://www.rfc-editor.org/rfc/rfc3986#section-5.1). Specifications that |
| 59 | +extend this media type MAY add keywords that effect the base URI, but if they |
| 60 | +do, they MUST do so in a way that fits into the framework expressed in [RFC 3986 |
| 61 | +Section 5.1](https://www.rfc-editor.org/rfc/rfc3986#section-5.1). |
| 62 | + |
| 63 | +Following a reference may result in media types other than JRef being returned. |
| 64 | +While implementations are not limited to only handling JRef responses, they MUST |
| 65 | +raise an error if they encounter a media type they do not support. |
| 66 | + |
| 67 | +When following a URI that uses a protocol where the response is self describing |
| 68 | +of its media type, implementations MUST respect the declared media type. For |
| 69 | +example, an HTTP response for `https://example.com/example` that has |
| 70 | +`Content-Type: application/json` should be treated as plain JSON document rather |
| 71 | +than as JRef document. That means that any objects with a `$href` would be of |
| 72 | +type "object" rather than type "reference". |
| 73 | + |
| 74 | +When following a URI using a protocol where the response is not self describing |
| 75 | +of its media type (such as file system access), implementations MAY use whatever |
| 76 | +heuristic they deem necessary to determine the media type of the response |
| 77 | +including context hints from the referring document. If implementations use file |
| 78 | +extensions as a heuristic, they SHOULD use the [IANA media types |
| 79 | +registry](https://www.iana.org/assignments/media-types/media-types.xhtml) to |
| 80 | +determine which file extensions map to which media types. |
| 81 | + |
| 82 | +A reference may point to a secondary resource within the referred to document as |
| 83 | +defined by the [fragment](https://www.rfc-editor.org/rfc/rfc3986#section-3.5) |
| 84 | +semantics of the media type of the referred to document. |
| 85 | + |
| 86 | +## Values |
| 87 | +The "value" of a reference is the JSON-compatible value of the result of |
| 88 | +following the reference. Implementations MAY define how non-JSON media types |
| 89 | +translate to a JSON-compatible value. Implementations MAY provide an API for |
| 90 | +working with media types that do not translate to a JSON compatible value. |
0 commit comments