A python module that attempts to let classes be serializable without any work
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
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