Registry / serialization / json-ref-dict

json-ref-dict

JSON →
library0.7.2pypypi✓ verified 85d ago

json-ref-dict is a Python dict-like object that abstracts the resolution of JSONSchema references. It allows lazy loading of referenced documents, supporting both local filesystem and remote HTTP/HTTPS references. The library is currently at version 0.7.2 and maintains an active release cadence with frequent updates.

pip install json-ref-dict
INSTALL
IMPORT
SIG · JSON-REF-DICT
J
json-ref-dict
serializationpythonv0.7.2
Install
1.6s avg
Import
162ms
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.7.2 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.920 runs
installs and imports cleanly · install 0.0s · import 0.134s · 17.9MB
glibc
py 3.103.920 runs
installs and imports cleanly · install 1.6s · import 0.126s · 18MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

RefDict
from json_ref_dict import RefDict
JsonRefDict
from json_ref_dict import RefDict
from json_ref_dict import JsonRefDict
The `JsonRefDict` alias for `RefDict` was removed in version 0.6.0. Use `RefDict` directly.

This quickstart demonstrates how to create a `RefDict` instance from a local YAML file containing JSON references, including local, remote, and back references. It shows how lazy loading works by accessing properties and includes cleanup of the temporary files created for the example.

import os from json_ref_dict import RefDict # Create dummy YAML files for the example master_yaml_content = """ definitions: foo: type: string local_ref: $ref: '#/definitions/foo' remote_ref: $ref: 'other.yaml#/definitions/bar' backref: $ref: 'other.yaml#/definitions/baz' """ other_yaml_content = """ definitions: bar: type: integer baz: $ref: 'master.yaml#/definitions/foo' """ with open("master.yaml", "w") as f: f.write(master_yaml_content) with open("other.yaml", "w") as f: f.write(other_yaml_content) try: # Initialize RefDict with a reference to a local YAML file and JSON pointer schema = RefDict("master.yaml#/definitions") # Accessing elements triggers lazy resolution print("Type of local_ref:", schema["local_ref"]["type"]) print("Type of remote_ref:", schema["remote_ref"]["type"]) print("Type of backref:", schema["backref"]["type"]) # Materialize the document to a regular dict (optional) # from json_ref_dict import materialize # materialized_schema = materialize(schema) # print("Materialized schema type:", type(materialized_schema)) except Exception as e: print(f"An error occurred: {e}") finally: # Clean up dummy files os.remove("master.yaml") os.remove("other.yaml")
Debug
Known issues
breakingThe internal attribute for accessing the original JSON reference object was renamed from `__json_ref__` to `__reference__` to align with the `jsonref` library conventions.
fix
Update any direct access to `obj.__json_ref__` to `obj.__reference__`.
affects: >=0.7.0
breaking`RefDict` no longer inherits directly from `dict`, but now from `collections.abc.Mapping`. This changes its behavior regarding mutability and can affect `isinstance` checks. `RefDict` instances are now immutable collections.
fix
If you relied on `RefDict` being a mutable `dict`, reassess your logic. For type checks, use `isinstance(obj, collections.abc.Mapping)` instead of `isinstance(obj, dict)`.
affects: >=0.6.0
deprecatedThe `JsonRefDict` alias for `RefDict` has been removed.
fix
Always use `RefDict` directly for imports and class instantiation.
affects: >=0.6.0
Errors
Common errors & fixes
from json_ref_dict import JsonRefDict
The alias `JsonRefDict` for the main `RefDict` class was removed in `json-ref-dict` version 0.6.0.
fix
Always use `RefDict` directly for imports and class instantiation: `from json_ref_dict import RefDict`.
isinstance(my_ref_dict, dict)` returning `False` or calling mutable dict methods like `pop()`
`RefDict` no longer inherits directly from `dict` but from `collections.abc.Mapping` since version 0.6.0, making it an immutable collection.
fix
For type checks, use `isinstance(obj, collections.abc.Mapping)`. To get a mutable dictionary or apply modifications, first call `my_ref_dict.materialize()` to convert it to a regular `dict`.
AttributeError: 'RefDict' object has no attribute '__json_ref__'
The internal attribute for accessing the original JSON reference object was renamed from `__json_ref__` to `__reference__` in `json-ref-dict` version 0.6.0 to align with `jsonref` library conventions.
fix
Update any direct access to `obj.__json_ref__` to `obj.__reference__`.
jsonschema.exceptions.RefResolutionError: Unresolvable JSON pointer
This error, often encountered in the JSON Schema ecosystem, means a JSON reference (`$ref`) within the schema cannot be resolved because the path is incorrect, the referenced document (file/URL) is inaccessible, or its content is malformed.
fix
Ensure all `$ref` pointers are valid, accessible, and point to properly formatted JSON or YAML documents relative to the base URI of the `RefDict`.
TypeError: Object of type RefDict is not JSON serializable
A `RefDict` object is a specialized, lazily resolving dictionary and is not directly serializable by Python's standard `json.dumps()` function, which expects plain Python primitive types or standard collections.
fix
Before serializing, convert the `RefDict` into a standard Python dictionary using its `materialize()` method: `json.dumps(my_ref_dict.materialize())`. If the materialized dict contains other non-serializable types (e.g., `datetime` objects), provide a custom JSONEncoder to `json.dumps()`.
Upgrade
Version history
0.7.2latest on PyPI · released Aug 6, 2023
Audit
Dependencies
PyYAMLoptionalOptional dependency to enable loading of YAML documents; without it, only JSON documents are supported.
Agent activity
6 hits · last 30 days
node
6
Resources
json-ref-dict — pip install json-ref-dict · libregistry