Code to make it easier to read and write to a JSON file
Project description
JSON Handler
Overview
Code to make it easier to read and write to a JSON file. It provides an abstraction layer, since you don't need to specify the details of how the JSON is read or written (such as file encoding, etc.). This eliminates the ability to customize how the JSON is read or written. However, most instances will probably not require such customization, so this works for most cases.
NOTE: The code uses utf-8 encoding only, since it is a very common encoding.
Usage
To use the JSON handler, create an instance of the JSONHandler class and specify the file path in the constructor. You can then read and write to the file using the read_json and write_json methods.
The class utilizes generics, allowing you to specify the type of data you want to read or write. Make sure the data type is serializable to JSON, since the file will not be created if it is not.
Note: Type hints in Python are not enforced at runtime. You are responsible for ensuring that the data you pass matches the expected type and is serializable.
Example code
from collections.abc import Mapping
from json_handler_caramajau.json_handler import JSONHandler
handler: JSONHandler[str] = JSONHandler("path/to/your/file")
data: Mapping[str, str] = {"key": "value"}
handler.write_json(data)
read_data: Mapping[str, str] = handler.read_json()
print(read_data) # Output: {'key': 'value'}
Requirements
- Python 3.12 or higher
Installation
You can install the package using pip:
pip install json-handler-caramajau
License
This project is licensed under the MIT License. See the LICENSE file for details.
Contributing
If you want to contribute to this project, feel free to open an issue or a pull request. Contributions are welcome, but since the project is so simple, I expect that there probably won't be many.
Author
This project is maintained by Caramajau. If you have any questions or suggestions, feel free to reach out.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file json_handler_caramajau-0.1.1.tar.gz.
File metadata
- Download URL: json_handler_caramajau-0.1.1.tar.gz
- Upload date:
- Size: 6.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.12.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
983781b3ca7dd24fd793084d9e879055fa2496ca37cff38e4c2cf8591a0167d0
|
|
| MD5 |
9f9759ee42d548afd6444ef3830f95c9
|
|
| BLAKE2b-256 |
cf7c077f44da2437e764e6801033fd876e498137b19d1cc09ef9f1b86b57948a
|
File details
Details for the file json_handler_caramajau-0.1.1-py3-none-any.whl.
File metadata
- Download URL: json_handler_caramajau-0.1.1-py3-none-any.whl
- Upload date:
- Size: 4.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.12.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d636242e85e42327452c3ab09c37c4960279cc25f9091ad17920eed615b06aae
|
|
| MD5 |
51f6ece46958d63f00825e2a64bd2b4b
|
|
| BLAKE2b-256 |
dce2f0bfa14607774c324ebd56f1dee5c07caf074343553150299baee9ec1aba
|