Skip to main content

Simple decorator for making classes easily convertable to JSON

Project description

JSONIFABLE

A very small library, consisting of one decorator, which adds to_json method, that converts your class to JSON.

Installation:

pip install jsonifable

Example:

from jsonifable import Jsonifable

# it is not required to use dataclasses
# using them will just make this example shorter
from dataclasses import dataclass


@Jsonifable
@dataclass
class Person:

    name: str
    surname: str


person = Person("Avery", "Oliwa")
jsonified = person.to_json()
print(jsonified)

Will result in: {"name": "Avery", "surname": "Oliwa"}

You can also use it with nested classes!

from jsonifable import Jsonifable
from dataclasses import dataclass


@Jsonifable
@dataclass
class Animal:

    name: str
    species: str


# Notice how you're not required to add @Jsonifable decorator if you don't need the class instance to be manually converted using to_json
@dataclass
class Address:

    street_no: int
    street_name: str
    city: str


@Jsonifable
@dataclass
class Person:

    name: str
    surname: str
    address: Address
    animal: Animal


person = Person("Avery", "Oliwa", Address(20, "STREET", "London"), Animal("Guido", "dog"))
jsonified = person.to_json()
print(jsonified)

Will result in: {"name": "Avery", "surname": "Oliwa", "address": {"street_no": 20, "street_name": "STREET", "city": "London"}, "animal": {"name": "Guido", "species": "dog"}}

It's because Jsonifable forces nested classes to be converted to JSON too.

CAUTION:

Versions 0.0.1 and 0.0.2 do not work, do not install them, only install the newest version.

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

jsonifable-0.1.0.tar.gz (3.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

jsonifable-0.1.0-py3-none-any.whl (3.5 kB view details)

Uploaded Python 3

File details

Details for the file jsonifable-0.1.0.tar.gz.

File metadata

  • Download URL: jsonifable-0.1.0.tar.gz
  • Upload date:
  • Size: 3.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.26.0 requests-toolbelt/0.9.1 urllib3/1.26.7 tqdm/4.62.3 importlib-metadata/4.8.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.1

File hashes

Hashes for jsonifable-0.1.0.tar.gz
Algorithm Hash digest
SHA256 a75819eb9f076aa9650800e3d39d538b785cccffa075ee6330c31b3ba9c3d63b
MD5 5968b360b644cdcefb90829c12664d1a
BLAKE2b-256 8e1c171ceb54b1e98a39c18c052d015ff087c443b926fcdc1ffc179b4f653567

See more details on using hashes here.

File details

Details for the file jsonifable-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: jsonifable-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 3.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.26.0 requests-toolbelt/0.9.1 urllib3/1.26.7 tqdm/4.62.3 importlib-metadata/4.8.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.1

File hashes

Hashes for jsonifable-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d2a7ef3398ed9a1d17a3572fafa19758bd7b7b5849f4df8b5c69eb04a09ea897
MD5 8b115002a69e179827b071f19f334ad8
BLAKE2b-256 09dac4ec93845949d0963c4819448cf98053f12fedce90ec3677c670c12f8c04

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