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 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


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.2.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.2-py3-none-any.whl (4.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: json_handler_caramajau-0.1.2.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.2.tar.gz
Algorithm Hash digest
SHA256 5bc04a30531915b698cc6c4acb5e77b01d4128837558f0ad2b273193ce6d05ea
MD5 4ce84a01aed8a77a1fae27720e688d46
BLAKE2b-256 7069cfa7c7e92db5e1c73e6fdaea2f9d29f0f4b045633013555cf65947e0c155

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for json_handler_caramajau-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 4352bfcf6694127aa7d8a2ba59bdc73dde271c1c180983e5532f2d15159b2bf0
MD5 bb36c8c8e0e770970dbabc642e2df272
BLAKE2b-256 ffc3271dbb00c8244fc748523c4bd46d1b22921077990c1b1f628e0004664727

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