Skip to main content

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 json_handler_caramajau.json_handler import JSONHandler
from typing import Mapping

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

json_handler_caramajau-0.1.0.tar.gz (6.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

json_handler_caramajau-0.1.0-py3-none-any.whl (4.2 kB view details)

Uploaded Python 3

File details

Details for the file json_handler_caramajau-0.1.0.tar.gz.

File metadata

  • Download URL: json_handler_caramajau-0.1.0.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

Hashes for json_handler_caramajau-0.1.0.tar.gz
Algorithm Hash digest
SHA256 239e2dfda8cbbab5657c5fe970083aa23607befe720799c34969b6b7bcd1f5c3
MD5 86bbe03f21c6a1d11ec943a9c97485f4
BLAKE2b-256 caa1101ae0c1f13974d7e4cdea5077596fce1053a62eab3867dd6939227c8c30

See more details on using hashes here.

File details

Details for the file json_handler_caramajau-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for json_handler_caramajau-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 bfc488e6b42d115a6f7db20ef7a7335b3621b83c3b1f57c05905ce7300293cae
MD5 1b1a4b4cf22ecb72986c2164d39e30e6
BLAKE2b-256 43501ea4a6f71ece3b48b3ef5eb768b173d769d40c187f8a163074887fefa543

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page