Skip to main content

Python Metaclass JSON library

Project description

jsxn

jsxn is a Python library for JSON objects that use a strict schema. Designed for use with REST APIs that return lists of objects that all have the same attributes.

The jsxn object will pragmatically generate a Python class based on a JSON string, Python dictionary, keyword arguments, or an iterable collection of attribute names.

from jsxn import jsxn

# this will create a jsxn class 'dynamic' that has 'schema' and 'key' as attributes
instance = jsxn.dynamic({'schema':100,'key':'value'})

# str representations of jsxn instances will be JSON
print(instance)
# {"schema": 100, "key": "value"}

# jsxn instances can be passed to dict
print(dict(instance))
# {'schema': 100, 'key': 'value'}

# future references of the class will use the generated class
another = jsxn.dynamic('{"schema":200,"key":"something"}')

# previously generated classes can be called without arguments
builder = jsxn.dynamic()
print(builder)
# {"schema": null, "key": null}

# attributes can be accessed directly
builder.schema = 300
# or by indices
builder['key'] = 'populate'
print(builder)
# {"schema": 300, "key": "populate"}

# jsxn instances are callable with JSON strings
builder('{"schema":500}')
# and keywords
builder(key='hello')
print(builder)
# {"schema": 500, "key": "hello"}

# jsxn objects use slots so only attributes defined at creation can be assigned
try:
    builder.not_defined = True
except AttributeError as e:
    print(e)
    # 'dynamic' object has no attribute 'not_defined'

# delete a jsxn class in order to reuse it
del jsxn.dynamic

# a jsxn class can be defined with a list of keys
instance = jsxn.dynamic(['attr1','attr2','attr3'])
# all the variables will be uninitialized
print(instance)
# {"attr1": null, "attr2": null, "attr3": null}

# jsxn classes can also be deleted by indices
del jsxn['dynamic']

# it is possible to create a jsxn class that accept nothing
empty = jsxn.empty()
print(empty)
# {}
# but it is not very useful...

Project details


Release history Release notifications | RSS feed

This version

0.2

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

jsxn-0.2.tar.gz (4.2 kB view details)

Uploaded Source

File details

Details for the file jsxn-0.2.tar.gz.

File metadata

  • Download URL: jsxn-0.2.tar.gz
  • Upload date:
  • Size: 4.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 colorama/0.4.4 importlib-metadata/4.6.4 keyring/23.5.0 pkginfo/1.8.2 readme-renderer/34.0 requests-toolbelt/0.9.1 requests/2.28.2 rfc3986/1.5.0 tqdm/4.57.0 urllib3/1.26.5 CPython/3.10.6

File hashes

Hashes for jsxn-0.2.tar.gz
Algorithm Hash digest
SHA256 588e8122b4f0112907a90b0551e6f5836c8ab11e0bc8898da2b73bf983eb7d94
MD5 a15f3fb5a152c9008c31c42197c57660
BLAKE2b-256 7ce211afe573dffdf23e9e71acaede4418cd20f6b4035bcbfa877adb06230b5f

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