Skip to main content

A JSON data binding library for Python

Project description

Introduction

This library supports two-way data binding between JSON and Python class.

Turorial

Here is a simple example to use jsonalize:

from jsonalize import *


# Define a class
class MyData(JSONObject):
    def __init__(self):
        JSONObject.__init__(self)
        self.id = JSONString()
        self.name = JSONString()
        self.age = JSONInt()
        self.weight = JSONFloat()


# Create an object of MyData
my = MyData()
my.id = "20190101"
my.name = "Stanley"
my.age = 28
my.weight = 60

# jsonalize the object
json_str = my.to_json()
print(json_str)

# restore the object from json
my2 = JSONObject.from_json(MyData, json_str)
print(my2.to_json())

This example should output the following message:

{"age": 28, "id": "20190101", "weight": 60.0, "name": "Stanley"}
{"age": 28, "id": "20190101", "weight": 60.0, "name": "Stanley"}

Another example shows how to prevent creating real objects for attributes when constructing a new JSONObject instance.

class MyData(JSONObject):
    def __init__(self):
        JSONObject.__init__(self)
        self.id = JSONNoneInt
        self.name = JSONNoneString
        self.attr = JSONObject.NoneValue(Attr)


class Attr(JSONObject):
    def __init__(self):
        JSONObject.__init__(self)
        self.value = JSONString()
        print("Attr object created")


my = MyData()
print(my.id is None, my.name is None, my.attr is None)

my.id = 10
my.name = "Stanley"
my.attr = Attr()
my.attr.value = "attr"
print(my.to_json())

# Outputs:
# (True, True, True)
# Attr object created
# {"attr": {"value": "attr"}, "name": "Stanley", "id": 10}

Key points from this tutorial

  • A serializable class should inherit the JSONObject class
  • Don't forget to invoke the __init__ method in your class
  • The serializable class attributes should be set as JSON** types

List of supported JSON types

  • JSONInt
  • JSONLong (Only in Python 2)
  • JSONFloat
  • JSONComplex
  • JSONBool
  • JSONString
  • JSONList
  • JSONSet
  • JSONDict
  • JSONObject

Most of the types can be initialized with an initial value, for example:

a_string = JSONString("hello jsonalize")

Remarks for JSONBool

You can't test the value of a JSONBool object with the is keyword, because is will compare the instance id of two objects, but an object of JSONBool is never an instance of True or False.

You can do the test as follows:

a_bool = JSONBool(True)
print(a_bool is True, a_bool == True)
# False, True
print(a_bool.true(), a_bool.true() is True, a_bool.equals(True))
# True, True, True

A more complex example

You can have an object of JSONObject in another JSONObject class:

class Monitor(JSONObject):
    def __init__(self):
        JSONObject.__init__(self)
        self.size = JSONFloat()
        self.power = JSONFloat()
        self.color = JSONString()


class Computer(JSONObject):
    def __init__(self):
        JSONObject.__init__(self)
        self.brand = JSONString()
        self.monitor = Monitor()


computer = Computer()
computer.brand = "Lenovo"
computer.monitor.size = 23.0
computer.monitor.power = 25.0

json_str = computer.to_json()
print(json_str)
#{"brand": "Lenovo", "monitor": {"color": "", "power": 25.0, "size": 23.0}}

computer2 = JSONObject.from_json(Computer, json_str)
print(computer2.to_json())
#{"brand": "Lenovo", "monitor": {"color": "", "power": 25.0, "size": 23.0}}

A list of JSONObject objects?

Look at the following example:

class Student(JSONObject):
    def __init__(self):
        JSONObject.__init__(self)
        self.id = JSONString()
        self.name = JSONString()


class School(JSONObject):
    def __init__(self):
        JSONObject.__init__(self)
        self.address = JSONString()
        self.students = JSONList()


stu1 = Student()
stu1.id = "20190202"
stu1.name = "Stanley"

stu2 = Student()
stu2.id = "20190203"
stu2.name = "Cyrus"

school = School()
school.address = "Central Street No.23"
school.students.append(stu1)
school.students.append(stu2)

json_str = school.to_json()
print(json_str)
#{"students": [{"id": "20190202", "name": "Stanley"}, {"id": "20190203", "name": "Cyrus"}], "address": "Central Street No.23"}

school2 = JSONObject.from_json(School, json_str)
print(type(school2.students[0]), school2.students[0])
#(<type 'dict'>, {u'id': u'20190202', u'name': u'Stanley'})

As you can see here, the deserializing of the School object is incorrect. Because any type of object could be appended to school.students, so jsonalize don't know what to restore when deserializing.

This problem is currently not resolved, maybe I can add a type mapping structure in the future.

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

jsonalize-0.2.tar.gz (5.3 kB view details)

Uploaded Source

Built Distribution

jsonalize-0.2-py2-none-any.whl (5.9 kB view details)

Uploaded Python 2

File details

Details for the file jsonalize-0.2.tar.gz.

File metadata

  • Download URL: jsonalize-0.2.tar.gz
  • Upload date:
  • Size: 5.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.7.2

File hashes

Hashes for jsonalize-0.2.tar.gz
Algorithm Hash digest
SHA256 5c7d7df8294569ca3104ddb5f9185e6d176a4c6242741d65b0388ec5a31e6b4f
MD5 99e5cb43f71293538663bacd02d1c3d9
BLAKE2b-256 ccf35593107d9a75bd591ddf1562a3f9a0275ead1756b1fa771633c1e44b8b9b

See more details on using hashes here.

File details

Details for the file jsonalize-0.2-py2-none-any.whl.

File metadata

  • Download URL: jsonalize-0.2-py2-none-any.whl
  • Upload date:
  • Size: 5.9 kB
  • Tags: Python 2
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.7.2

File hashes

Hashes for jsonalize-0.2-py2-none-any.whl
Algorithm Hash digest
SHA256 13e926eb2583a27b3103032c471f1a32bb006ce6dfb991f9a534f689bb71d9bf
MD5 a36805edcafb4cb1e340d318c5f73c60
BLAKE2b-256 54150af6071746ba36a2863eee0a325973ce092f992e8c0fd70dcd58b940a023

See more details on using hashes here.

Supported by

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