Hologram is a Python library that generates JSON schemas from standard Python dataclasses. It provides a `JsonSchemaMixin` that, when added to a dataclass, enables automatic schema generation and object parsing from JSON data. The current version is 0.0.16, with releases being infrequent.
pip install hologramVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to define dataclasses with `JsonSchemaMixin`, including optional fields, lists, unions with `FieldSpec`, and nested models. It then shows how to generate a JSON schema from the dataclass and parse a Python dictionary into a dataclass instance, leveraging Hologram's validation capabilities.
Review the GitHub repository's issues and PRs for current activity and known compatibility problems with newer Python versions or type hints before relying on it for critical projects. Consider contributing or forking if you require active maintenance.
Always use `FieldSpec` for `Union`, `Optional` with default values where custom metadata is desired, and any other complex type requiring specific JSON schema attributes. Refer to the official documentation for advanced `FieldSpec` usage.
If `mashumaro` is desired, consult `hologram`'s GitHub README or source code for examples on how to integrate `mashumaro`'s `DataClassDictMixin` with `JsonSchemaMixin` for your dataclasses.
Downgrade the `jsonschema` package to a version less than 4.0 by running `pip install 'jsonschema<4.0'`.
Use the `to_dict()` or `to_json()` methods provided by `hologram.JsonSchemaMixin` to serialize the dataclass instance: `my_instance.to_dict()` for a dictionary, or `my_instance.to_json()` for a JSON string.
Ensure that the input JSON string includes all fields marked as required in your `hologram` dataclass definition. Alternatively, if the field is optional, update your dataclass definition to use `typing.Optional` and provide a default value (e.g., `field: Optional[str] = None`).
Correct the type of the value for the specified field in your input JSON data to match the type annotation in your dataclass (e.g., if `field: int`, ensure the JSON provides an integer). If the JSON's type is correct and the dataclass's type hint is wrong, update the dataclass's type hint accordingly.