Json Config
Layered JSON configuration with typed values. Define your settings as a pydantic model, stack any number of JSON files on top of each other (defaults → workspace → user, ...), and read the merged result as a typed object. Edits are written back to a single layer, and only the values that actually differ from the layers below it are saved.
Installation
pip install git+https://github.com/sowwic/json-config.git
Requires Python 3.12+.
Core concepts
| Concept | What it does |
|---|---|
ConfigValues |
Pydantic model describing your settings and their defaults. |
ConfigLayer |
One named layer, optionally backed by a JSON file. depends_on lists the layers it overrides. |
ConfigLayerManager |
Owns the layers and deep-merges them in dependency order. Managers are named singletons. |
LayeredConfig |
The object you use in your app. Binds a ConfigValues class to a manager and exposes resolve(), save() and revert_value(). |
Quick start
import pathlib
from json_config.api import (
ConfigLayer,
ConfigValues,
LayeredConfig,
get_layer_manager,
)
class AppValues(ConfigValues):
theme: str = "light"
font_size: int = 12
recent_files: list[str] = []
class AppConfig(LayeredConfig[AppValues]):
VALUES_CLASS = AppValues
NAME = "my_app" # required: selects the layer manager
# Register layers once at startup. Order of registration doesn't matter,
# `depends_on` decides which layer overrides which.
manager = get_layer_manager("my_app")
manager.register(ConfigLayer("defaults", file_path=pathlib.Path("config/defaults.json")))
manager.register(
ConfigLayer(
"user",
depends_on=["defaults"],
file_path=pathlib.Path("~/.my_app/user.json").expanduser(),
)
)
config = AppConfig(layer_filter="user") # work on the "user" layer
config.resolve() # load files and merge the layers
print(config.values.theme) # "light" unless a layer overrides it
config.values.theme = "dark"
config.save() # writes only {"theme": "dark"} to user.json
NAME is mandatory on every LayeredConfig subclass. AppConfig() looks the
manager up by that name (creating it if it doesn't exist yet), so every config
class that shares a NAME shares the same layers.
Use cases
Defaults, workspace and user overrides
Ship a defaults.json, let a project override it, and let each user override
that. Higher layers only contain what they change:
config/defaults.json {"theme": "light", "font_size": 12}
config/project.json {"font_size": 14}
~/.my_app/user.json {"theme": "dark"}
manager = get_layer_manager("my_app")
manager.register(ConfigLayer("defaults", file_path=defaults_path))
manager.register(ConfigLayer("project", depends_on=["defaults"], file_path=project_path))
manager.register(ConfigLayer("user", depends_on=["project"], file_path=user_path))
config = AppConfig(layer_filter="user")
config.resolve()
config.values.theme # "dark" (user)
config.values.font_size # 14 (project)
Previewing what a lower layer provides
layer_filter limits resolution to a layer and the layers beneath it, which is
useful for settings dialogs with a "scope" selector:
config = AppConfig(layer_filter="project")
config.resolve()
config.values.theme # "light" (user layer is ignored)
config.layer_filter = None # back to the root layer
Use config.layers to list the available layer names in dependency order.
Saving only real overrides
save() compares the current values with what the layer's dependencies
already provide and writes only the differences. If you set a value back to
what is inherited, it disappears from the file.
config = AppConfig(layer_filter="user")
config.resolve()
config.values.font_size = 16
config.save() # user.json -> {"theme": "dark", "font_size": 16}
config.values.font_size = 14 # same as the project layer
config.save() # user.json -> {"theme": "dark"}
On first save, root layers whose files don't exist yet are created and seeded
with the defaults from your ConfigValues class.
Reverting a value
revert_value() removes a field's override from the current layer and
restores the inherited value (or the default). Nested values can be addressed
with a dotted path. Call save() afterwards to persist the change.
config.revert_value("theme")
config.revert_value("window.width") # only this leaf, siblings are kept
config.save()
Nested categories
Group related settings with nested ConfigValues classes. A nested field
must declare an alias, which becomes its key in the JSON file:
from pydantic import Field
class WindowValues(ConfigValues):
width: int = 800
height: int = 600
class AppValues(ConfigValues):
theme: str = "light"
window: WindowValues = Field(default_factory=WindowValues, alias="window")
{
"theme": "dark",
"window": {"width": 1024}
}
Layers are deep-merged, so a layer can override window.width without
repeating window.height.
Loading layers from a directory tree
register_from_paths_tree builds a chain of layers from a nested dict. Each
key is a JSON file path, the layer name is the file stem, and nesting defines
depends_on:
manager.register_from_paths_tree(
{
"config/defaults.json": {
"config/team.json": {
"config/user.json": {},
},
},
}
)
# defaults (no parent)
# team (depends_on=["defaults"])
# user (depends_on=["team"])
Several independent configs
Each NAME gets its own manager, so unrelated parts of an application don't
share layers:
class EditorConfig(LayeredConfig[EditorValues]):
VALUES_CLASS = EditorValues
NAME = "editor"
class NetworkConfig(LayeredConfig[NetworkValues]):
VALUES_CLASS = NetworkValues
NAME = "network"
You can also pass a manager explicitly, e.g. in tests:
AppConfig(layer_manager=my_manager) (the class still needs a NAME).
Call ConfigLayerManager.clear() to drop all managers between tests.
Migrating or cleaning data with hooks
ConfigValues has hooks that run when values are serialised (to_dict, used
by save()) and deserialised (replace, used by resolve()):
class RecentValues(ConfigValues):
recent_files: list[str] = []
def pre_deserialize(self, data):
# drop files that no longer exist when loading
files = data.get("recent_files", [])
return {**data, "recent_files": [f for f in files if pathlib.Path(f).exists()]}
def pre_serialize(self):
# keep only the 10 most recent entries when saving
self.recent_files = self.recent_files[:10]
Available hooks: pre_serialize, post_serialize, pre_deserialize,
post_deserialize.
Development
uv sync
uv run pytest
Metadata
Release files for sowwic-json-config 1.0.7
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| sowwic_json_config-1.0.7.tar.gz | 14.9 kB | Without provenance | Details for sowwic_json_config-1.0.7.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| sowwic_json_config-1.0.7-py3-none-any.whl | Python 3 | none | any | Details for sowwic_json_config-1.0.7-py3-none-any.whl |
Total release size: 32.7 kB
Release files / sowwic_json_config-1.0.7.tar.gz
| Download URL | sowwic_json_config-1.0.7.tar.gz |
|---|---|
| Size | 14.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
a4abaf9f44a1bbb7975e002f45ec626f203f678dae2cc35cf851aa60ba44d7f1
|
|
BLAKE2b-256 checksum How to use checksums |
54b369c0522b3602b35de2a9c4d7d572f082e1f3b92afaa0cb35643a221865f7
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.13.8 {"installer":{"name":"uv","version":"0.13.8","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
|
Release files / sowwic_json_config-1.0.7-py3-none-any.whl
| Download URL | sowwic_json_config-1.0.7-py3-none-any.whl |
|---|---|
| Size | 17.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
21f16bdb4a0ca7ef31202a5495078a645164b7b3e4bf36d84717d03ec71294a6
|
|
BLAKE2b-256 checksum How to use checksums |
e34c50bd364a9c38d290332ff3198a3de49cd398a54bad5f293fc9d2e554f783
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.13.8 {"installer":{"name":"uv","version":"0.13.8","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
|
Security for sowwic-json-config 1.0.7
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.