Skip to main content

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


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)

Uploaded Source

Built Distribution

serialiJSON-0.0.2-py2.py3-none-any.whl (3.6 kB view hashes)

Uploaded Python 2 Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page