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 details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

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

Uploaded Python 2

File details

Details for the file ojextends-0.1.2.tar.gz.

File metadata

  • Download URL: ojextends-0.1.2.tar.gz
  • Upload date:
  • Size: 4.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.35.0 CPython/2.7.16

File hashes

Hashes for ojextends-0.1.2.tar.gz
Algorithm Hash digest
SHA256 73cb132e892468b9b052ebaf8b89bc6347347a6c1d1696806cc9708e341e5b36
MD5 0ced447811224476db7ca6e8f68734b3
BLAKE2b-256 f69f745c538ef1dddec5f91d52cb9678042d5c99cd9c03f6b033be210694a77c

See more details on using hashes here.

File details

Details for the file ojextends-0.1.2-py2-none-any.whl.

File metadata

  • Download URL: ojextends-0.1.2-py2-none-any.whl
  • Upload date:
  • Size: 4.3 kB
  • Tags: Python 2
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.35.0 CPython/2.7.16

File hashes

Hashes for ojextends-0.1.2-py2-none-any.whl
Algorithm Hash digest
SHA256 18f4c1ac0d95483e6876f47d23b4b0f33f075f4a1aa9021fdd6ea8a896b4a0ce
MD5 6a0d0438811eead1688562448e4ef38e
BLAKE2b-256 f23ae42802a803636bd367dbcd6345490c3295a92359fdfb990287443ff4d204

See more details on using hashes here.

Supported by

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