Skip to main content

jsoncodable

Install

pip install jsoncodable
# or
pip3 install jsoncodable

Usage

from jsoncodable import JSONCodable
import json

# ---------------------------------------- Any object to json dict ---------------------------------------- #

class Test1:
    def __init__(self, value: int):
        self.value1 = value
        self.value2 = value * 2
        self.values = [self.value1, self.value2]

class Test2(JSONCodable):
    def __init__(self, value: int):
        self.test1 = Test1(value)

test2 = Test2(5)
print(test2.dict)
# prints:
# 
# {'test1': <__main__.Test1 object at 0x1018199d0>}

print(json.dumps(test2.json, indent=4))
# prints:
# 
# {
#     "test1": {
#         "value1": 5,
#         "value2": 10,
#         "values": [
#             5,
#             10
#         ]
#     }
# }

# ---------------------------------------- Any json dict to object ---------------------------------------- #

class Test3(JSONCodable):
    pass

json_str = '{"name": "John Smith", "hometown": {"name": "New York", "id": 123}}'

print(Test3.from_json(json_str))
# prints:
# 
# JSONCodable(name='John Smith', hometown=JSONCodable(name='New York', id=123))

print(Test3.from_json(json.loads(json_str)))
# prints:
# 
# JSONCodable(name='John Smith', hometown=JSONCodable(name='New York', id=123))

Metadata

Release files for to-dict 0.0.7

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

Source distribution (sdist)

Source distribution for to-dict 0.0.7
File Size Uploaded
to_dict-0.0.7.tar.gz 2.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for to-dict 0.0.7
File Interpreter ABI Platform
to_dict-0.0.7-py3-none-any.whl Python 3 none any Details

Total release size: 5.7 kB

Release files / to_dict-0.0.7.tar.gz

Download URL to_dict-0.0.7.tar.gz
Size 2.3 kB
Tags Source
SHA-256 checksum
How to use checksums
bd275b6bdd49506da06203e28ad9b9794e63e8b7770e036a7bfd4793663f6dc3
BLAKE2b-256 checksum
How to use checksums
5a04f2b5a96908aba73b1c5cd6b3de47f49e7dbaf58fb544cc13fdf0f03f8c1a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/47.1.1 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.7.6

Release files / to_dict-0.0.7-py3-none-any.whl

Download URL to_dict-0.0.7-py3-none-any.whl
Size 3.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ff2c0abcbc48440d8a145bfffd906cbeb31f497faa0f3daa69adfeb65b17dcb2
BLAKE2b-256 checksum
How to use checksums
3611a611c3ce82b2756d9aea50381719797927a33f1b60368c3d7d3a622a50fd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/47.1.1 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.7.6

Release history Release notifications | RSS feed

This release

0.0.7 This release

2 release files

0.0.6

2 release files

0.0.5

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

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