Skip to content

Commit 7db9493

Browse files
committed
Add the quick intro here too.
Needs a bit more elaboration, particularly around creating resources I suppose.
1 parent 598b82a commit 7db9493

File tree

3 files changed

+57
-0
lines changed

3 files changed

+57
-0
lines changed

docs/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Contents
88
:glob:
99
:maxdepth: 2
1010

11+
intro
1112
external-retrieval
1213
schema-packages
1314
api

docs/intro.rst

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
=======================================
2+
Introduction to the ``referencing`` API
3+
=======================================
4+
5+
When authoring JSON documents, it is often useful to be able to reference other JSON documents, or to reference subsections of other JSON documents.
6+
7+
This kind of JSON referencing has historically been defined by various specifications, with slightly differing behavior.
8+
The JSON Schema specifications, for instance, define :kw:`$ref` and :kw:`$dynamicRef` keywords to allow schema authors to combine multiple schemas together for reuse or deduplication as part of authoring JSON schemas.
9+
10+
The `referencing <index>` library was written in order to provide a simple, well-behaved and well-tested implementation of JSON reference resolution in a way which can be used across multiple specifications or specification versions.
11+
12+
13+
Core Concepts
14+
-------------
15+
16+
There are 3 main objects to be aware of:
17+
18+
* `referencing.Registry`, which represents a specific immutable set of resources (either in-memory or retrievable)
19+
* `referencing.Specification`, which represents a specific specification, such as JSON Schema Draft 7, which can have differing referencing behavior from other specifications or even versions of JSON Schema.
20+
JSON Schema-specific specifications live in the `referencing.jsonschema` module and are named like `referencing.jsonschema.DRAFT202012`.
21+
* `referencing.Resource`, which represents a specific resource (often a Python `dict`) *along* with a specific `referencing.Specification` it is to be interpreted under.
22+
23+
As a concrete example, the simple JSON Schema ``{"type": "integer"}`` may be interpreted as a schema under either Draft 2020-12 or Draft 4 of the JSON Schema specification (amongst others); in draft 2020-12, the float ``2.0`` must be considered an integer, whereas in draft 4, it potentially is not.
24+
If you mean the former (i.e. to associate this schema with draft 2020-12), you'd use ``referencing.Resource(contents={"type": "integer"}, specification=referencing.jsonschema.DRAFT202012)``, whereas for the latter you'd use `referencing.jsonschema.DRAFT4`.
25+
26+
A resource may be identified via one or more URIs, either because they identify themselves in a way proscribed by their specification (e.g. an :kw:`$id` keyword in suitable versions of the JSON Schema specification), or simply because you wish to externally associate a URI with the resource, regardless of a specification-specific way to refer to itself.
27+
You could add the aforementioned simple JSON Schema resource to a `referencing.Registry` by creating an empty registry and then identifying it via some URI:
28+
29+
.. testcode::
30+
31+
from referencing import Registry, Resource
32+
from referencing.jsonschema import DRAFT202012
33+
resource = Resource(contents={"type": "integer"}, specification=DRAFT202012)
34+
registry = Registry().with_resource(uri="http://example.com/my/resource", resource=resource)
35+
print(registry)
36+
37+
.. testoutput::
38+
39+
<Registry (1 uncrawled resource)>
40+
41+
.. note::
42+
43+
`referencing.Registry` is an entirely immutable object.
44+
All of its methods which add resources to itself return *new* registry objects containing the added resource.
45+
46+
You could also confirm your resource is in the registry if you'd like, via `referencing.Registry.contents`, which will show you the contents of a resource at a given URI:
47+
48+
.. testcode::
49+
50+
print(registry.contents("http://example.com/my/resource"))
51+
52+
.. testoutput::
53+
54+
{'type': 'integer'}

docs/spelling_wordlist.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
amongst
12
autodetecting
3+
deduplication
24
deserialized
35
discoverability
46
filesystem

0 commit comments

Comments
 (0)