Skip to main content

Serializing JSON data into Python object with minimal effort.

Project description

Serial-J

Serializing JSON data into Python object with minimal effort.

Example

Let's first see a basic example.

from serial_j import SerialJ

class FruitBucket(SerialJ):
    # define how our data should look like using `schema`.
    schema = [
        {'name': 'apple','optional': False,'nullable': False,'is_compound': False,},
        {'name': 'orange','optional': False,'nullable': False,'is_compound': False,},
        {'name': 'pineapple','optional': False,'nullable': False,'is_compound': False,},
    ]

# test data for FruitBucket 
test1 = dict(
    apple="good apple",
    orange="very good orange",
    pineapple="nice pineapple",
)

# serialize `test1` into `FruitBucket` object
fruits = FruitBucket(test1)

# `fruits` is a proper python object , which means that you can use 
# `fruits.apple` syntax to retrieve the value of `apple`.
print(fruits.apple)
>>> good apple

# ...and other fruits too.
print(fruits.orange)
>>> very good orange
print(fruits.pineapple)
>>> nice pineapple

# you can get the JSON formatted string back too.
print(fruits)
>>> {"apple": "good apple", "orange": "very good orange", "pineapple": "nice pineapple"}

# interested to get the python dictionary back?
fruits_data = fruits.as_dict()
print(fruits_data)
>>> {'apple': 'good apple', 'orange': 'very good orange', 'pineapple': 'nice pineapple'}

That's ok. But, we can do more than just that. Let's see how we can serialize more complex data structure into python object with another example.

from serial_j import SerialJ
class Snack(SerialJ):
    schema = [
        # cheese is nice but is optional.
        {'name': 'cheese','optional': True,'nullable': False,'is_compound': False,},
        # chocolate is a MUST have.
        {'name': 'chocolate','optional': False,'nullable': False,'is_compound': False,},
        # chips is a must but we have to decide which kind later, 
        # so its value can be None, False, "", {}, [].
        {'name': 'chips','optional': False,'nullable': True,'is_compound': False,},
    ]

class NestedBucket(SerialJ):
    schema = [
        {'name': 'apple','optional': False,'nullable': False,'is_compound': False,},
        {'name': 'orange','optional': False,'nullable': False,'is_compound': False,},
        {'name': 'pineapple','optional': False,'nullable': False,'is_compound': False,},
        {'name': 'snack','optional': False,'nullable': False,'is_compound': True,'compound_serializer': Snack,}
    ]

# test data for NestedBucket
test2 = dict(
    apple="good apple",
    orange="very good orange",
    pineapple="nice pineapple",
    snack=dict(
        chocolate="Ferrero Rocher",
        chips=[] # yeah its a list of chips!
    ),
)
my_snacks = NestedBucket(test2)
print(my_snacks)
>>> {"apple": "good apple", "orange": "very good orange", "pineapple": "nice pineapple", 
>>>  "snack": {"chocolate": "Ferrero Rocher", "chips": []}}

alternatively, you put them together like this...

from serial_j import SerialJ

class SnackBucket(SerialJ):
    schema = [
        {'name': 'apple', 'optional': False, 'nullable': False, 'is_compound': False,},
        {'name': 'orange','optional': False, 'nullable': False, 'is_compound': False,},
        {'name': 'pineapple','optional': False, 'nullable': False, 'is_compound': False,},
        {'name': 'snack', 'optional': False, 'nullable': False, 'is_compound': True, 
            'compound_schema': [
                {'name': 'cheese','optional': True, 'nullable': False, 'is_compound': False,},
                {'name': 'chocolate','optional': False,'nullable': False,'is_compound': False,},
                {'name': 'chips','optional': False,'nullable': True,'is_compound': False,},
            ],
        },
    ]

test3 = dict(
    apple="good apple",
    orange="very good orange",
    pineapple="nice pineapple",
    snack=[
        dict(
            cheese="Feta",
            chocolate="Ferrero Rocher",
            chips=[] 
        ),
        dict(
            chocolate="Swiss milk chocolate",
            chips=["Cheetos", "Lays Classic Potato Chips", "Cool Ranch Doritos"] 
        ),
    ]
)
mysnacks = SnackBucket(test3)
print(mysnacks)
>>> {"apple": "good apple", "orange": "very good orange", "pineapple": "nice pineapple", 
>>> "snack": [{"cheese": "Feta", "chocolate": "Ferrero Rocher", "chips": []}, 
>>>           {"chocolate": "Swiss milk chocolate", "chips": 
>>>                ["Cheetos", "Lays Classic Potato Chips", "Cool Ranch Doritos"]}]}

To be continued...

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

serial-j-0.1.1.tar.gz (3.7 kB view hashes)

Uploaded Source

Built Distribution

serial_j-0.1.1-py3-none-any.whl (4.5 kB view hashes)

Uploaded 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