to_dict
Project description
jsoncodable
Description
Easily create object from any dict/jsonstr/jsonfile and dict/jsonstr/jsonfile from any object
From v0.1.0 it is based on jsonpickle
Install
pip install jsoncodable
# or
pip3 install jsoncodable
Usage
from jsoncodable import JSONCodable, CompressionAlgorithm
class BirthDay(JSONCodable):
def __init__(
self,
month: int,
day: int
):
self.month = month
self.day = day
class Person(JSONCodable):
def __init__(
self,
name: str,
birth_month: int,
birth_day: int
):
self.name = name
self.birth_day = BirthDay(birth_month, birth_day)
person = Person(
name='John',
birth_month=7,
birth_day=7
)
person.jsonprint()
# prints
#
# {
# "name": "John",
# "birth_day": {
# "month": 7,
# "day": 7
# }
# }
Person.load(person.json).jsonprint()
# prints
#
# {
# "name": "John",
# "birth_day": {
# "month": 7,
# "day": 7
# }
# }
import os
# Save with compression
json_file_path = 'test.json'
for c in CompressionAlgorithm:
compressed_file_path = person.save(json_file_path, compression=c)
# returns a file path which has the compressed extension if not present at the end of your provided path
# also prints a message to let you know, that the path had been modified
Person.load(compressed_file_path).jsonprint()
# prints
#
# {
# "name": "John",
# "birth_day": {
# "month": 7,
# "day": 7
# }
# }
# Cleaning up
os.remove(compressed_file_path)
Dependencies
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.1.6.tar.gz
(5.1 kB
view hashes)
Built Distribution
Close
Hashes for jsoncodable-0.1.6-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | fb02ad0c7e45db41f67f56b2f039750327322516a63d1fb5e6260d35240da049 |
|
MD5 | beafec761570fafeb5a3468c2e24675f |
|
BLAKE2b-256 | 01fd4cca4e907a56e56dca6c6d8e3709b15049241fb959529403cef656244eb2 |