to_dict
Project description
jsoncodable
Description
Easily create object from any dict and dict from any object
Install
pip install jsoncodable
# or
pip3 install jsoncodable
Usage
from jsoncodable import *
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))
# or
test2.jsonprint()
#
# both will print:
#
# {
# "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))
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
jsoncodable-0.0.10.tar.gz
(3.3 kB
view hashes)
Built Distribution
Close
Hashes for jsoncodable-0.0.10-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4e5b4ae71c3355af950f1a04adf50d3e0054209e2284fdbd86d38f8080abdab0 |
|
MD5 | 95ad3e11febb58c1249c0aedb3201530 |
|
BLAKE2b-256 | a5f091a6d697c6d7893adc83818218a9b0925c08e7e685c7663846e2ff3d90f7 |