Coder Social home page Coder Social logo

autojson's Introduction

AutoJson

A python module that attempts to let classes be serializable without any work

Usage

from JsonSerialize import *
import json

@JsonSerializable()
class TestClass: 
    def __init__(self, id : int) -> None:
        self.id = id

dump = json.dumps(TestClass(155), default=encoder)
load = json.loads(dump, object_hook=decoder)
print(load.id) # should be 155

You can directly use the encoder/decoder functions

from JsonSerialize import JsonSerializable, encoder, decoder

@JsonSerializable()
class TestClass: 
    def __init__(self, id : int) -> None:
        self.id = id

dump = encoder(TestClass(155))
load = decoder(dump)
print(load.id) # should be 155

Example

from JsonSerialize import JsonSerializable, encoder, decoder
import json

@JsonSerializable() # dont forget the ()
class Mail:
    def __init__(self, content = None):
        self.content = content
    def __repr__(self) -> str: return f"mail: {self.content}"

@JsonSerializable(IGNORE_ATTRIBUTES=[]) # You can provide a list of attributes to ignore when serializing
class User:
    def __init__(self, name, email, inbox = None):
        self.name = name
        self.email = email
        self.inbox : list[Mail] = inbox or []

    def __repr__(self) -> str:
        return f"Name: {self.name} - Email: {self.email}\nInbox: {self.inbox}"

TestUser = User("Test", "[email protected]")
TestUser.inbox.append(Mail(content="hello"))
TestUser.inbox.append(Mail(content="hi!")  )

# dumping object....
Dump : str = json.dumps(TestUser, default=encoder, indent=4) #make sure to include the default=encoder
print(Dump) # now just a string, custom classes in json format contain __class_type__
#

# loading object....
Loaded : User = json.loads(Dump, object_hook=decoder) # make sure to include the object_hook=decoder
print(Loaded) # now back to a custom object

note: if you want it to encode to a dictionary instead so you can save it to something mongoDB just do encoder(obj, DictForm=True)

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.