A simple converter from complex objects to JSON
Project description
SerialiJSON
serialiJSON
is a tool that allows you to convert complex objects to JSON only by inheriting the BaseSerializable class.
Installation
python pip -m install serialiJSON
Usage
Import BaseSerializable
class
from serialiJSON import BaseSerializable
Define your own class inheriting BaseSerializable
class myClass(BaseSerializable):
...
myObject = myClass()
Convert to JSON
myObject.toJson()
An optional indent
can be passed into toJson()
Property | Description | type | default |
---|---|---|---|
indent | set the indent to pretty-printed | Int | None (most compact) |
Compatible Types
Type | List mode |
---|---|
str | [str] |
int | [int] |
bool | [bool] |
float | [float] |
Real Example
from serialiJSON import BaseSerializable
class Item(BaseSerializable):
def __init__(self, itemName, isAlive):
self.itemName = itemName
self.isAliva = isAlive
class Pet(BaseSerializable):
def __init__(self, name, age, favItems):
self.name = name
self.age = age
self.favItems = favItems
class Human(BaseSerializable):
def __init__(self, name, age, pets):
self.name = name
self.age = age
self.pets = pets
items = [
Item("snow ball", False),
Item("tree", True)
]
pets = [
Pet("Cat", 12, items),
Pet("Dog", 4, items)
]
human = Human("name", 90, pets)
print(human.toJson(indent=4))
pretty-printed
{
"name": "name",
"age": 90,
"pets": [
{
"name": "Cat",
"age": 12,
"favItems": [
{
"itemName": "snow ball",
"isAliva": false
},
{
"itemName": "tree",
"isAliva": true
}
]
},
{
"name": "Dog",
"age": 4,
"favItems": [
{
"itemName": "snow ball",
"isAliva": false
},
{
"itemName": "tree",
"isAliva": true
}
]
}
]
}
non pretty-printed
{"name": "name", "age": 90, "pets": [{"name": "Cat", "age": 12, "favItems": [{"itemName": "snow ball", "isAliva": false}, {"itemName": "tree", "isAliva": true}]}, {"name": "Dog", "age": 4, "favItems": [{"itemName": "snow ball", "isAliva": false}, {"itemName": "tree", "isAliva": true}]}]}
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
serialiJSON-0.0.2.tar.gz
(3.3 kB
view hashes)
Built Distribution
Close
Hashes for serialiJSON-0.0.2-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 652dd45a94daded23c83cdafd4fc15a2090e1a79a6b1d3f8dd6683a56878de74 |
|
MD5 | 1bab5f9ad2395b6fc17336f5d93625b5 |
|
BLAKE2b-256 | 6928917927c9420dc1564479e9f25543ae44602e12da9a89562aa43c1acb8147 |