Skip to main content

Python package for making your classes easy encodable to JSON string and vice-versa

Project description

Lint & Test PyPI version Downloads count

jsonabler

Python package for making your classes easy encodable to JSON string and vice-versa.

Getting started

Requirements

  • Python >= 3.8

Installation

pip install jsonabler

Usage

Making a Jsonable

Make your class extends the Jsonable interface and implements get_jsonable_data and from_jsonable_data methods with the encoding/decoding logic.

from jsonabler import Jsonable, jsonabled 

@jsonabled
class Foo(Jsonable):
    def __init__(self, bar: str):
        self.__bar = bar
    
    def get_jsonable_data(self) -> dict:
        return {
            'bar': self.__bar,
        }

    @classmethod
    def from_jsonable_data(cls, data: dict) -> Jsonable:
        return cls(data['bar'])

Registering a Jsonable

For decoding your Jsonable classes, you need to register it.

Decorating your classes with the @jsonabled decorator

from jsonabler import Jsonable, jsonabled 

@jsonabled
class Foo(Jsonable):
    ...

Calling register_jsonables method passing class types

from jsonabler import Jsonable, register_jsonables 

class Foo(Jsonable):
    ...

if __name__ == '__main__':
    register_jsonables({Foo})

Encoding a Jsonable

Call dumps method passing a Jsonable object.

from jsonabler import dumps

def upload_foo(foo: Foo) -> None:    
    json_string = dumps(foo)
    
    # transmit your JSON string
    ...

Encoded Foo

[
  'Foo',
  {
    'bar': "abc",
  }
]

Decoding a Jsonable

Call loads method passing the JSON string.

from jsonabler import loads, JsonableDecodeError, JsonableNotRegisteredError
from json import JSONDecodeError

def download_foo() -> Foo:    
    # receive JSON string encoded Foo object
    ...

    try:
        return loads(json_string)
    
    # not a valid encoded JSON string
    except JSONDecodeError:  
        ...
    
    # the Jsonable type of the encoded object was not registered
    except JsonableNotRegisteredError:  
        ...
    
    # something went wrong while decoding the object
    except JsonableDecodeError:  
        ...

License

Distributed under the MIT License. See LICENSE file for more information.

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

jsonabler-0.2.2.tar.gz (4.3 kB view details)

Uploaded Source

Built Distribution

jsonabler-0.2.2-py3-none-any.whl (4.6 kB view details)

Uploaded Python 3

File details

Details for the file jsonabler-0.2.2.tar.gz.

File metadata

  • Download URL: jsonabler-0.2.2.tar.gz
  • Upload date:
  • Size: 4.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.9.12

File hashes

Hashes for jsonabler-0.2.2.tar.gz
Algorithm Hash digest
SHA256 1bd92833c41a52e73ba4668bf43e8fcffebc3d59902d02921ea86a8c1be1156f
MD5 a11e20cfd8785c3e4e68b62658b03c43
BLAKE2b-256 acd70e1dc02a58f9732943030294b36ff28dd312f1de32bed8bb147d7e352262

See more details on using hashes here.

File details

Details for the file jsonabler-0.2.2-py3-none-any.whl.

File metadata

  • Download URL: jsonabler-0.2.2-py3-none-any.whl
  • Upload date:
  • Size: 4.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.9.12

File hashes

Hashes for jsonabler-0.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 df90b1c41b75996b843db8bddad34d79d21cf8717fa4288ceee0d55bccbf90d2
MD5 0f50a6a566026a3350f43fb1f006cf4b
BLAKE2b-256 9eff704569fbad617d10589dbdd4cd776da779f3805e2e514e833ad700b36c67

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