a library that supports both of python2 and python3, can load a json as an object, and also can dump an object to a json
Project description
jsoncls, a library that supports both of python2 and python3, can load a json as an object, and also can dump an object to a json
A Simple Example
declare an object and load json as it
import json
from numbers import Number
from jsoncls import Field, String, List, Object
from jsoncls import decode, loads
class Address(Object):
country = Field("Country", String)
province = Field("Province", String)
city = Field("City", String)
class Education(Object):
primary = Field("PrimarySchool", String)
middle = Field("MiddleSchool", String)
high = Field("HighSchool", String)
university = Field("University", String)
class Person(Object):
name = Field("Name", String)
age = Field("Age", Number)
nationality = Field("Nationality", String)
married = Field("Married", bool)
education = Field("Education", Education, required=False)
addresses = Field("Addresses", List(Address))
hobbies = Field("Hobbies", List(String), required=False)
if __name__ == '__main__':
person_dict = {
"Name": "ZhangSan",
"Age": 20,
"Nationality": "Korean",
"Married": False,
"Hobbies": ["swimming", "running"],
"Education": {
"PrimarySchool": "First",
"MiddleSchool": "Second",
"HighSchool": "Third",
"University": "Fourth"
},
"Addresses": [{
"Country": "China",
"Province": "ZheJiang",
"City": "HangZhou"
}, {
"Country": "China",
"Province": "JiangXi",
"City": "NanChang"
}]
}
# decode the dict as an object
person = decode(Person, person_dict)
# the value is HangZhou
print(person.addresses[0].city)
person_str = json.dumps(person_dict)
# load the json as an object
person = loads(Person, person_str)
# the value is China
print(person.addresses[1].country)
Changelog
v1.0.2
require six>=1.12.0
v1.0.1
Bug Fixes
failed to install jsoncls with python2 -m pip
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
jsoncls-1.0.2.tar.gz
(4.3 kB
view details)
Built Distribution
File details
Details for the file jsoncls-1.0.2.tar.gz
.
File metadata
- Download URL: jsoncls-1.0.2.tar.gz
- Upload date:
- Size: 4.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.7.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 95ac3a5d038dc37efdec228f7c2ee985d48be086a19cbfc004b5db34de10981d |
|
MD5 | 2af726c36af9fbe0a32e34dd4c73dc55 |
|
BLAKE2b-256 | bed9377910ca733f84dd917e894706ad95fa66c8cce6948bc4aab0ab855916a4 |
File details
Details for the file jsoncls-1.0.2-py2.py3-none-any.whl
.
File metadata
- Download URL: jsoncls-1.0.2-py2.py3-none-any.whl
- Upload date:
- Size: 4.8 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.7.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 82624a22d8d15528ad51303238f6ac8db8ed267d9d2a973893cf767adcf84b91 |
|
MD5 | ecde29450b17e0831955927745bb5eb9 |
|
BLAKE2b-256 | af2e9770425eb71d307f708670aedba753fd9bd0f72f52436bec4d887ec14c98 |