-
An issue with JSON is it can't serialize python's datetime classes very well. YAML doesn't play well with brython.
-
I needed a way to send
datetime.date
anddatetime.time
to the client. This serializer might not be the best thing for this, but it does what is needed. -
I also send quite a bit of data from the server that is has multiple reused strings and numbers, this was written to assist in making the transfer time smaller.
-
Any python structure, nested or not can be passed.
-
It does use json under the hood to facilitate the transferring of the data between server and client.
You can
pip install ujson
if you want the server to encode/decode faster.
from datetime import date
import serializer
serialized = serializer.dump(date.today())
# '{"&0": {"int": 2021}, "&1": {"int": 2}, "&2": {"int": 25}, "&3": {"tuple": ["&0", "&1", "&2"]}, "date": "&3"}'
serialized.loads(serialized)
# datetime.date(2021, 2, 25)
type | comment |
---|---|
bytes | |
str | |
int | |
float | |
dict | |
list | |
tuple | |
set | |
datetime | |
date | |
time | |
Decimal | Some values transfer poorly. |
None | |
True | |
False |