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.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.1.tar.gz
(4.5 kB
view details)
Built Distribution
File details
Details for the file jsoncls-1.0.1.tar.gz
.
File metadata
- Download URL: jsoncls-1.0.1.tar.gz
- Upload date:
- Size: 4.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.9.19
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | bf260c52cbb11f46336374879b73e6ab1ef2cdbf8c4302553e4c3ed8148bcf0a |
|
MD5 | dcca66a468e33f48f089c3d3b230e37e |
|
BLAKE2b-256 | 019aa51fe2e5e08bea208d47fa8f2ebb5ff4853abcbb01432185ade1d3d8767d |
File details
Details for the file jsoncls-1.0.1-py2.py3-none-any.whl
.
File metadata
- Download URL: jsoncls-1.0.1-py2.py3-none-any.whl
- Upload date:
- Size: 4.8 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.9.19
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | dd6196a694a3a8d4c60e1274c09632e21172e968810f40590e68ddf4a4a6502a |
|
MD5 | c5f3512ddb6a743d37fa226f786a4cfc |
|
BLAKE2b-256 | d7725f5b0d4f6b8fb7aecb11990c29e3e3a87502d25bb7f74d4e71a096e53c62 |