Skip to main content

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)

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

jsoncls-1.0.0.tar.gz (4.6 kB view details)

Uploaded Source

Built Distribution

jsoncls-1.0.0-py2.py3-none-any.whl (4.7 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file jsoncls-1.0.0.tar.gz.

File metadata

  • Download URL: jsoncls-1.0.0.tar.gz
  • Upload date:
  • Size: 4.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.9.19

File hashes

Hashes for jsoncls-1.0.0.tar.gz
Algorithm Hash digest
SHA256 7abbf7d7740b6b96509a5aa1be5acfa296f120ba960d7e37a30a0eae7571e572
MD5 56d912c5e205b0781700106d711c0a38
BLAKE2b-256 ca9983ad1bfa58dbc3c072d50abbe65c3b9d4fcc4435691172723dc1471315a2

See more details on using hashes here.

File details

Details for the file jsoncls-1.0.0-py2.py3-none-any.whl.

File metadata

  • Download URL: jsoncls-1.0.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 4.7 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.9.19

File hashes

Hashes for jsoncls-1.0.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 7c266bc28de3655a97d467425446e38a993612bb643482b270a10a2a5585deb8
MD5 19f19bf75eeb48915dc4dd90c7c2ae6e
BLAKE2b-256 7be22c06efa64b0d446edb9f6f6e2ecd2650846c1dc7473205a005a735ca66e7

See more details on using hashes here.

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