A very simple serialization and deserialization python library.
- Python 100%
|
|
||
|---|---|---|
| mirror | ||
| .gitignore | ||
| CODEOWNERS | ||
| DEVELOPER.md | ||
| LICENSE.md | ||
| pyproject.toml | ||
| README.md | ||
Mirror
Mirror is a very simple serialization and deserialization library.
Usage
Use mirror.serialize(anything) on any object to create a json string representing that object's data and hierarchy.
Use mirror.deserialize(Into, text) to deserialize json text into the specified object.
Example
import dataclasses
import inspect
import pathlib
import textwrap
@dataclasses.dataclass
class Gamma:
x: str = "x"
y: str = "y"
@dataclasses.dataclass
class Beta:
d: int = 3
g: Gamma = dataclasses.field(default_factory=lambda: Gamma())
class Alpha:
def __init__(self):
self.__a: int = 1
self.__b: int = 2
self.__c: int = 3
def __str__(self):
out = f"""
a : {type(self.__a).__qualname__} = {self.__a}
b : {type(self.__b).__qualname__} = {self.__b}
c : {type(self.__c).__qualname__} = {self.__c}
d : {type(self.d).__qualname__} = {self.d}
t : {type(self.t).__qualname__} = {self.t}
"""
return inspect.cleandoc(out)
@property
def d(self) -> float:
return getattr(self, "__d", 4.0)
@d.setter
def d(self, value: float):
setattr(self, "__d", float(value))
@property
def t(self) -> Beta:
return getattr(self, "__t", Beta())
@t.setter
def t(self, value: Beta):
setattr(self, "__t", value)
file = pathlib.Path("temp.json")
print("\n")
thing = Alpha()
print(f"thing =\n{textwrap.indent(str(thing), " - ")}\n")
thing.d = 3.14159
print(f"thing =\n{textwrap.indent(str(thing), " - ")}\n")
thing.t = Beta(d=10)
print(f"thing =\n{textwrap.indent(str(thing), " - ")}\n")
file.write_text(mirror.serialize(thing))
out = mirror.deserialize(file.read_text(), Alpha)
print(f"out =\n{textwrap.indent(str(out), " - ")}\n")
Legal
Copyright © 2026 Billy Ray Fortenbury (br410bury)
See LICENSE.md.