Skip to main content

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

  1. Import the TypedJSON class and the DeserializeException exception:

    from typed_json import TypedJSON, DeserializeException
    
  2. 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
    
  3. Create a TypedJSON instance with the target type:

    typed_json = TypedJSON(Person)
    
  4. Use the load method 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)}")
    
  5. Use the dumps method 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.

Metadata

Release files for json-typed 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for json-typed 0.1.1
File Size Uploaded
json-typed-0.1.1.tar.gz 2.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for json-typed 0.1.1
File Interpreter ABI Platform
json_typed-0.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 4.0 kB

Release files / json-typed-0.1.1.tar.gz

Download URL json-typed-0.1.1.tar.gz
Size 2.0 kB
Tags Source
SHA-256 checksum
How to use checksums
595d36a3db273aba1d666577dd4a63456ceebc8e1113cfb56c48b801a437ebd2
BLAKE2b-256 checksum
How to use checksums
65a4ec348127b5eb770a7e8cd101c0b3ebeab61deed2f9283ec5fa64aa97ca33
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.6

Release files / json_typed-0.1.1-py3-none-any.whl

Download URL json_typed-0.1.1-py3-none-any.whl
Size 2.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
8d9f7f18712f9e516683aa75b10c62c36bd7d6140e4265c89cc4db254b07cd89
BLAKE2b-256 checksum
How to use checksums
ac3a52c5628aebef29869942f9189d1cfa9b7b7f7df70eff67b2722462122858
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.6

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page