Git Product home page Git Product logo

jsonifable's Introduction

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.

jsonifable's People

Contributors

maciejoliwa avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.