A tool to convert any class to json and convert json to object
Project description
Tool to convert Class to Json and Json to Object (SmartJson)
SmartJson is a simple tool to convert any class to JSON and convert json to Object.
Usage
Requirements
Python 3 must be installed.
How to run
- use code from github or
pip install superjson
Parameters
cls
: Class you want to convert to json
Project structure:
scripts
- source code of a packageexample.py
- working examples
Contribute
- If unsure, open an issue for a discussion
- Create a fork
- Make your change
- Make a pull request
- Happy contribution!
EXAMPLE
Class
import datetime
from scripts.__smart_json__ import SmartJson
class Pull:
def __init__(self):
self.id = 2
self.title = "Iam pull"
self.author = "Joel O."
self.subPull = Pull.SubPull()
class SubPull:
def __init__(self):
self.subId = 3
self.subTitle = "I am sub title"
self.subAuthor = "OKJ."
class Jobs:
def __init__(self):
self.name = 'John'
self.url = "5444"
self.id = 1
self.job = Jobs.Job()
def name(self, set=None):
if set != None:
self.name = set
return self.name
class Job:
def __init__(self):
self.job_name = 'Test'
self.job_url = "_blank"
self.date = datetime.datetime.now()
self.date2 = datetime.datetime.now()
self.item = Jobs.Item()
self.pull = Pull()
class Item:
def __init__(self):
self.item_name = 'item 1'
self.item_boof = datetime.datetime.now()
self.mylist = [1, 2, 3]
self.another = Jobs.Item.Another()
class Another:
def __init__(self):
self.age = 26
self.precision = 99.56
self.ville = "Lille"
self.meteo = Jobs.Item.Another.Meteo()
class Meteo:
def __init__(self):
self.pluie = True
self.complex = complex(12, 78)
self.tuple = [((1, 'a'), (2, 'b'))]
self.none = None
jb = Jobs()
smart_json = SmartJson(jb)
# Disable pretty print
serialize = smart_json.serialize(pretty=False)
print(serialize)
# Use pretty print
pretty = SmartJson(Jobs()).serialize()
print(pretty)
Output:
{
"id": 1,
"job": {
"date": "2019-09-30 13:57:36.340899",
"date2": "2019-09-30 13:57:36.340899",
"item": {
"another": {
"age": 26,
"meteo": {
"complex": "(12+78j)",
"none": "",
"pluie": true,
"tuple": [
[
[
1,
"a"
],
[
2,
"b"
]
]
]
},
"precision": 99.56,
"ville": "Lille"
},
"item_boof": "2019-09-30 13:57:36.340899",
"item_name": "item 1",
"mylist": [
1,
2,
3
]
},
"job_name": "Test",
"job_url": "_blank",
"pull": {
"author": "Joel O.",
"id": 2,
"subPull": {
"subAuthor": "OKJ.",
"subId": 3,
"subTitle": "I am sub title"
},
"title": "Iam pull"
}
},
"name": "John",
"url": "5444"
}
Json to Object
objFromFile = smart_json.toObjectFromFile("jobs.json")
obj = smart_json.toObject('{"people":[{"name":"Scott", "website":"stackabuse.com", "from":"Nebraska"}]}')
obj2 = smart_json.toObject({'item': 'Beer', 'cost': '£4.00'})
print(obj2.item, obj2.cost)
print(objFromFile.job.item.another.precision)
print(obj.people[0].name, obj.people[0].website)
# Beer £4.00
# 99.56
# Scott stackabuse.com
###Author : Koffi Joel O.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
smartjson-0.0.1.tar.gz
(4.2 kB
view hashes)
Built Distribution
Close
Hashes for smartjson-0.0.1-py2-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 99662a257b2c04bd76b007a074e3933178cdd6dcaca52850d3cd7fef110de501 |
|
MD5 | 44ad04a2b0f71d6d0ed2f1d615be6c26 |
|
BLAKE2b-256 | 970993d08e9b9342370e9be469178e8293a9b4ee684861fa235515d1a12f5170 |