Skip to content

adhaamehab/jsonabc

Repository files navigation

JSONABC

Json Abstract Base Class

JSONABC is a minimal, pythonic JSON to class serializer. It has zero dependencies which makes it very lightweight. It provide a very minimal API with few options and play nicely with python type checking.

Install

pip install jsonabc

Examples

example 1 (simple)

from jsonabc import JSONABC

# All you need to do is define JSONABC as your metaclass
class Response(metaclass=JSONABC):
    args: dict
    headers: dict
    origin: str
    url: str

resp = requests.get("https://httpbin.org/get")
json_data = resp.json()

# Your class can now take a single dict and automatically validate and parse it.
obj = Response(response)

# obj attributes now have their correspondent values from our json data.
# you can also convert your class back to its json form.
# JSONABC will preserve the same names for the original json keys.
print(obj.json())

example 2 (composite)

from jsonabc import JSONABC

# You can also define composite values instead of using dicts
class Headers(metaclass=JSONABC):
    accept: str
    accept_encoding: str

class Response(metaclass=JSONABC):
    args: dict
    headers: Headers
    origin: str
    url: str

resp = requests.get("https://httpbin.org/get")
json_data = resp.json()

obj = Response(response)

# headers attr is now an instance of Headers with the same behavior as Response
obj.headers 

About

Minimal, pythonic JSON to class serializer with zero dependencies.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy