Skip to main content

Jsonizable

Jsonizable is a python library that allows to parse json objects into python classes and vice versa. This is really helpful to deal with complex APIs that return long JSON objects.

The Jsonizable class allows the user to insert logic in JSONs recieved by external servers and it's really helpful in the data integrity check with only two methods write and read.

Guide:

Quickstart

The recommended way to use this package is defining data schemas for each class. To do this we will need to implement a Metaclass inside our object and there define a schema, which is a dictionary containing the name and type of our JSON object.

class Car(Jsonizable):
    class Meta:
        schema = dict(
            name=str,
            model=str,
            year=int,
            max_speed=float,
            is_new=bool,
        )

By doing this we will be able to parse and serialize the Car object into JSON.

json = {
    "name": 'Tesla',
    "model": 'v6',
    "year": 2017,
    "max_speed": 186.63,
    "is_new": True,
}

# Parse the JSON object into a car
my_car = Car(json=json)
print(my_car.name)
>> Tesla

print(my_car.year)
>> 2017

#Serialize the Car into a JSON object
my_car.write()
>> { "name": "Tesla", "model": "v6", "year": 2017, "max_speed": 186.63, "is_new": True }

Collections

It is also posible to define collections for our Jsonized classes. We can define dictionaries, tuples and lists with generic types:

class MyJsonizableClass(Jsonizable):
    class Meta:
        schema = dict(
            some_list=list,
            some_tuple=tuple,
            some_dict=dict,
        )

json = {
    some_list: [1, 2, "Forest", 4],
    some_tuple: [1, 2.23, 3, "hello"],
    some_dict: {
        "name": "John Doe",
        4: "Martin Lee",
    },
}

obj = MyJsonizableClass(json=json)

And again we will receive an object with it's inner parameters.

But, in the most of the cases we will want to provide the collection a type, that will be checked. We can force the types as follows:

class Lottery(Jsonizable):
    class Meta:
        schema=dict(
            first_prize=int,
            second_prizes=[int],
            third_prizes=(int, ),
        )

json = {
    "first_prize": 24666123,
    "second_prizes": [1244123, 4551235512, 32341234],
    "third_prizes": [2344123, 412555123, 234444512, 12333415],
}

obj = Lottery(json=json)

Optional Parameters

Sometimes our objects will have some optional parameters, that obviously cannot be sent as Noneto the API because the validator inmediatly explodes. In this case we will probably want to define optional parameters, to do so we will end the parameter name with a question marker my_param? as the example below:

class MyQuanticClass(Jsonizable):
    class Meta:
        schema={
            "name": str, # This is mandatory parameter
            "state?": int, # This is an optional parameter
        }

json1 = {
    "name": "Electron",
}

class1 = MyQuanticClass(json=json1)

json2 = {
    "name": "Proton",
    "state": 3,
}
 class2 = MyQuanticClass(json=json2)

Works like charm ;).

Nested Objects

Ok, it was easy until now, simple objects and simple interfaces. What happens when we deal with nasty hard nested objects. To do so you can simply define a hierarchy of objects and tell the parent the type of the son. (Better with an example, huh?)

# Define a parent class
class PetShop(Jsonizable):
    class Meta:
        schema={
            "name": str,
            "pets": [Pet],
        }

# Define the subclass
class Pet(Jsonizable):
    class Meta:
        schema={
            "species": str,
            "name": str,
            "price": float,
        }

json={
    "name": "Funny puppies",
    "pets": [
        {
            "species": "dog",
            "name": "Tobby",
            "price": 150,
        },
        {
            "species": "bird",
            "name": "Mr. Tingles",
            "price": 20,
        }
    ]
}

pet_shop = PetShop(json=json)
print(pet_shop.pets[1].name)
>> Mr. Tingles

Metadata

Release files for jsonizable 1.0.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for jsonizable 1.0.4
File Size Uploaded Provenance
jsonizable-1.0.4.tar.gz 5.0 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for jsonizable 1.0.4
File Interpreter ABI Platform
jsonizable-1.0.4-py3-none-any.whl Python 3 none any Details

Total release size: 11.6 kB

Release files / jsonizable-1.0.4.tar.gz

Download URL jsonizable-1.0.4.tar.gz
Size 5.0 kB
Tags Source
SHA-256 checksum
How to use checksums
5cb96872a04d6910d64ca411d4f3ff430243ffe37fe2bfc6e555d606274cc847
BLAKE2b-256 checksum
How to use checksums
7841d5f8e21b7b57c0b7343c73617141f9b72e1913b97ce4e7c449b4138291a9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.15.0 pkginfo/1.5.0.1 requests/2.23.0 setuptools/41.1.0 requests-toolbelt/0.9.1 tqdm/4.44.1 CPython/2.7.17

Release files / jsonizable-1.0.4-py3-none-any.whl

Download URL jsonizable-1.0.4-py3-none-any.whl
Size 6.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
531e3eba10ba1c144a2930bf0763eebbbd3756c9fe08c2bb0d555d7b4e37591a
BLAKE2b-256 checksum
How to use checksums
5e6c5107b6b1f0c741d91daffb8b337db2b8c269df41edb57acb967951c5a43d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.15.0 pkginfo/1.5.0.1 requests/2.23.0 setuptools/41.1.0 requests-toolbelt/0.9.1 tqdm/4.44.1 CPython/2.7.17

Security for jsonizable 1.0.4

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page