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
Release history Release notifications | RSS feed
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)
Built Distribution
Close
Hashes for ojextends-0.1.2-py2-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 18f4c1ac0d95483e6876f47d23b4b0f33f075f4a1aa9021fdd6ea8a896b4a0ce |
|
MD5 | 6a0d0438811eead1688562448e4ef38e |
|
BLAKE2b-256 | f23ae42802a803636bd367dbcd6345490c3295a92359fdfb990287443ff4d204 |