Skip to main content

A lightweight library for transferring json to nested objects.

Project description

ojextends

ojextends allows you to extend the object class and convert JSON Documents to nested object parsing.

ojextends provides eight key methods to handle transformations.

  • objectToDict
  • objectToStr
  • objectsToList
  • objectsToStr
  • objectFromDict
  • objectFromStr
  • objectsFromStr
  • objectsFromList

Getting Started

To install using pip, simply run

pip install ojextends

Dependencies

ojextends only uses the json library provided by the python system.

Usage

The code below defines some simple models, and its natural mapping to json.

    from ojextends import JsonSerializable

    @JsonSerializable
    class Student(object):
        name = str
        age = int
        books = list

    @JsonSerializable
    class Teacher(object):
        name = str
        students = list([Student])

    @JsonSerializable
    class School(object):
        name = str
        teachers = list([Teacher])

    @JsonSerializable
    class Area(object):
        name = str
        schools = list([School])

Example of transformations to parse Area lookup response for item:

    import json
    import requests
    from ojextends import JsonSerializable

    def get_areas(areaid):
        url = 'https://127.0.0.1/area/lookup?id={}'
        return requests.get(url.format(area_id)).json()

    areajson = get_areas(518000)
    print(areajson)

    area = Area.objectToDict(areajson)
    print(area.schools)
    school = area.schools[0] if len(area.schools) else School()
    print(school.name)

The code above produces next result:

    {
    "name":"shenzhen",
    "student":{
        "name":"Bob",
        "age":20
    },
    "schools":[
        {
            "name":"Shenzhen university",
            "teachers":[
                {
                    "name":"Mike",
                    "students":[
                        {
                            "name":"Lily",
                            "age":18,
                            "books":[
                                "book1",
                                "book2"
                            ]
                        },
                        {
                            "name":"Stone",
                            "age":21
                        }
                    ]
                }
            ]
        },
        {
            "name":"Shenzhen normal university",
            "teachers":[
                {
                    "name":"Linda1",
                    "students":[
                        {
                            "name":"Bob",
                            "age":20,
                            "books":[]
                        },
                        {
                            "name":"Tom",
                            "age":23
                        }
                    ]
                }
            ]
        }
    ]
}

See tests.py for more examples.

Tests

Getting the tests running looks like:

# Install dependencies
$ pip install -r requirements.txt
# Run the test suites
$ python tests.py

License

The MIT License (MIT)

Contributed by Bob Wu

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

ojextends-0.1.2.tar.gz (4.8 kB view hashes)

Uploaded Source

Built Distribution

ojextends-0.1.2-py2-none-any.whl (4.3 kB view hashes)

Uploaded Python 2

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