No project description provided
Project description
TypedJSON
TypedJSON is a Python class that enables the deserialization of JSON data into an object of a specified type with type safety and verification.
Features
- Deserialize JSON data into an object of a specified type.
- Verify that JSON data is compatible with the object's initializer signature.
- Handle nested objects and complex data structures.
- Serialize an instance of a class back to a JSON string.
Installation
You can use TypedJSON by including it in your Python project. It has no external dependencies.
Usage
-
Import the
TypedJSONclass and theDeserializeExceptionexception:from typed_json import TypedJSON, DeserializeException
-
Define your Python classes that represent the data structure you want to deserialize from JSON.
class Street: def __init__(self, name: str, pos: int): self.name = name self.pos = pos class Address: def __init__(self, street: Street): self.street = street class Person: def __init__(self, name: str, age: int, add: Address): self.name = name self.age = age self.address = add
-
Create a
TypedJSONinstance with the target type:typed_json = TypedJSON(Person)
-
Use the
loadmethod to deserialize JSON data into an object of the specified type:json_data = '{"name": "John", "age": 30, "add": {"street": {"name": "123 Main St", "pos": 42}}}' try: person_obj = typed_json.load(json_data) print(person_obj.name) print(person_obj.age) except DeserializeException as e: print(f"Deserialization error: {str(e)}")
-
Use the
dumpsmethod to serialize an object back to a JSON string:serialized_json = typed_json.dumps(person_obj) print(serialized_json)
Error Handling
TypedJSON raises a DeserializeException if there are issues during deserialization. You can catch this exception to handle errors gracefully.
Contribution
Contributions are welcome! If you find issues or have suggestions for improvements, please open an issue or create a pull request on the GitHub repository.
License
This project is licensed under the MIT License - see the LICENSE file for details.
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-typed-0.1.1.tar.gz.
File metadata
- Download URL: json-typed-0.1.1.tar.gz
- Upload date:
- Size: 2.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
595d36a3db273aba1d666577dd4a63456ceebc8e1113cfb56c48b801a437ebd2
|
|
| MD5 |
30a509bda5bc20dd1879cf2f9538a11a
|
|
| BLAKE2b-256 |
65a4ec348127b5eb770a7e8cd101c0b3ebeab61deed2f9283ec5fa64aa97ca33
|
File details
Details for the file json_typed-0.1.1-py3-none-any.whl.
File metadata
- Download URL: json_typed-0.1.1-py3-none-any.whl
- Upload date:
- Size: 2.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8d9f7f18712f9e516683aa75b10c62c36bd7d6140e4265c89cc4db254b07cd89
|
|
| MD5 |
eb2d3a3d22ea442e1cb2f6878588a48e
|
|
| BLAKE2b-256 |
ac3a52c5628aebef29869942f9189d1cfa9b7b7f7df70eff67b2722462122858
|