API Reference¶
Top-level package¶
Load and save JSON files easily across files.
Public API: - Classes: JSONFile, JsonSerializationSettings - Functions: load, sync, reset, close - Defaults: DEFAULT_SERIALIZATION_SETTINGS
- class singlejson.JSONFile(path: str | PathLike[str], default_data: Any = None, *, encoding: str = 'utf-8', settings: JsonSerializationSettings | None = None, auto_save: bool = True, **kwargs: Any)[source]¶
Bases:
objectA .json file on the disk.
- json: Any¶
- reload() None[source]¶
Reload from disk, recovering to default on invalid JSON. Raises FileAccessError on permission issues.
- save(settings: JsonSerializationSettings | None = None) None[source]¶
Save the data to the disk
- Parameters:
settings – JsonSerializationSettings object
- class singlejson.JsonSerializationSettings(indent: 'int' = 4, sort_keys: 'bool' = True, ensure_ascii: 'bool' = False)[source]¶
Bases:
object- ensure_ascii: bool = False¶
- indent: int = 4¶
- sort_keys: bool = True¶
- singlejson.close(path: str | PathLike[str] | None = None, *, save: bool = True) None[source]¶
Close one file (by path) or all files, optionally saving first. If you wish to adjust settings, change the default or change the JsonFile.settings property.
- Parameters:
path – The path of the file to close.
save – Whether to save the file or not.
File utilities¶
Utils for handling IO and JSON operations.
- exception singlejson.fileutils.FileAccessError[source]¶
Bases:
ExceptionRaised when the file cannot be accessed due to permissions or IO errors.
- class singlejson.fileutils.JSONFile(path: str | PathLike[str], default_data: Any = None, *, encoding: str = 'utf-8', settings: JsonSerializationSettings | None = None, auto_save: bool = True, **kwargs: Any)[source]¶
Bases:
objectA .json file on the disk.
- json: Any¶
- reload() None[source]¶
Reload from disk, recovering to default on invalid JSON. Raises FileAccessError on permission issues.
- save(settings: JsonSerializationSettings | None = None) None[source]¶
Save the data to the disk
- Parameters:
settings – JsonSerializationSettings object
- class singlejson.fileutils.JsonSerializationSettings(indent: 'int' = 4, sort_keys: 'bool' = True, ensure_ascii: 'bool' = False)[source]¶
Bases:
object- ensure_ascii: bool = False¶
- indent: int = 4¶
- sort_keys: bool = True¶
Pooling¶
The main files handling the file pool.
- singlejson.pool.close(path: str | PathLike[str] | None = None, *, save: bool = True) None[source]¶
Close one file (by path) or all files, optionally saving first. If you wish to adjust settings, change the default or change the JsonFile.settings property.
- Parameters:
path – The path of the file to close.
save – Whether to save the file or not.
- singlejson.pool.load(path: str | PathLike[str], default_data: dict[str, JSONSerializable] | list[JSONSerializable] | str | int | float | bool | None = None, **kwargs: Any) JSONFile[source]¶
Open a JSONFile (synchronously) with pooling per absolute path. Backward-compat: accepts legacy “default” keyword.