Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/kkristof200/py_jsoncodable
py_todict
https://github.com/kkristof200/py_jsoncodable
Last synced: 5 days ago
JSON representation
py_todict
- Host: GitHub
- URL: https://github.com/kkristof200/py_jsoncodable
- Owner: kkristof200
- License: mit
- Created: 2020-06-06T23:05:38.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2021-05-04T16:09:08.000Z (almost 4 years ago)
- Last Synced: 2025-01-24T05:47:19.209Z (27 days ago)
- Language: Python
- Homepage:
- Size: 40 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# 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](https://github.com/jsonpickle/jsonpickle)
## Install
~~~~bash
pip install jsoncodable
# or
pip3 install jsoncodable
~~~~## Usage
~~~~python
from jsoncodable import JSONCodable, CompressionAlgorithmclass BirthDay(JSONCodable):
def __init__(
self,
month: int,
day: int
):
self.month = month
self.day = dayclass 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 modifiedPerson.load(compressed_file_path).jsonprint()
# prints
#
# {
# "name": "John",
# "birth_day": {
# "month": 7,
# "day": 7
# }
# }# Cleaning up
os.remove(compressed_file_path)
~~~~## Dependencies
[jsonpickle](https://pypi.org/project/jsonpickle), [noraise](https://pypi.org/project/noraise)