mirror of
https://github.com/long2ice/fastapi-cache.git
synced 2026-03-25 04:57:54 +00:00
Add default config when init
update JsonEncoder
This commit is contained in:
@@ -1,7 +1,40 @@
|
||||
import datetime
|
||||
import json
|
||||
import pickle # nosec:B403
|
||||
from decimal import Decimal
|
||||
from typing import Any
|
||||
|
||||
import dateutil.parser
|
||||
|
||||
CONVERTERS = {
|
||||
"date": dateutil.parser.parse,
|
||||
"datetime": dateutil.parser.parse,
|
||||
"decimal": Decimal,
|
||||
}
|
||||
|
||||
|
||||
class JsonEncoder(json.JSONEncoder):
|
||||
def default(self, obj):
|
||||
if isinstance(obj, datetime.datetime):
|
||||
return {"val": obj.strftime("%Y-%m-%d %H:%M:%S"), "_spec_type": "datetime"}
|
||||
elif isinstance(obj, datetime.date):
|
||||
return {"val": obj.strftime("%Y-%m-%d"), "_spec_type": "date"}
|
||||
elif isinstance(obj, Decimal):
|
||||
return {"val": str(obj), "_spec_type": "decimal"}
|
||||
else:
|
||||
return super().default(obj)
|
||||
|
||||
|
||||
def object_hook(obj):
|
||||
_spec_type = obj.get("_spec_type")
|
||||
if not _spec_type:
|
||||
return obj
|
||||
|
||||
if _spec_type in CONVERTERS:
|
||||
return CONVERTERS[_spec_type](obj["val"])
|
||||
else:
|
||||
raise TypeError("Unknown {}".format(_spec_type))
|
||||
|
||||
|
||||
class Coder:
|
||||
@classmethod
|
||||
@@ -16,11 +49,11 @@ class Coder:
|
||||
class JsonCoder(Coder):
|
||||
@classmethod
|
||||
def encode(cls, value: Any):
|
||||
return json.dumps(value)
|
||||
return json.dumps(value, cls=JsonEncoder)
|
||||
|
||||
@classmethod
|
||||
def decode(cls, value: Any):
|
||||
return json.loads(value)
|
||||
return json.loads(value, object_hook=object_hook)
|
||||
|
||||
|
||||
class PickleCoder(Coder):
|
||||
|
||||
Reference in New Issue
Block a user