aboutsummaryrefslogtreecommitdiff
path: root/gn_auth/json_encoders_decoders.py
diff options
context:
space:
mode:
authorFrederick Muriuki Muriithi2023-08-07 08:40:50 +0300
committerFrederick Muriuki Muriithi2023-08-07 09:26:13 +0300
commita112c99cae0c5422a38e3a35e843a82db764316a (patch)
treef0823fd1fc4d8ec52dc0a12b8d987dd4325b043b /gn_auth/json_encoders_decoders.py
parent6d9c61dc0072b96b12153e64940b465306f25bfb (diff)
downloadgn-auth-a112c99cae0c5422a38e3a35e843a82db764316a.tar.gz
Add missing modules
Copy over missing modules and functions to completely disconnect gn-auth from GN3.
Diffstat (limited to 'gn_auth/json_encoders_decoders.py')
-rw-r--r--gn_auth/json_encoders_decoders.py31
1 files changed, 31 insertions, 0 deletions
diff --git a/gn_auth/json_encoders_decoders.py b/gn_auth/json_encoders_decoders.py
new file mode 100644
index 0000000..be15b34
--- /dev/null
+++ b/gn_auth/json_encoders_decoders.py
@@ -0,0 +1,31 @@
+"""Custom json encoders for various purposes."""
+import json
+from uuid import UUID
+from datetime import datetime
+
+__ENCODERS__ = {
+ UUID: lambda obj: {"__type": "UUID", "__value": str(obj)},
+ datetime: lambda obj: {"__type": "DATETIME", "__value": obj.isoformat()}
+}
+
+class CustomJSONEncoder(json.JSONEncoder):
+ """
+ A custom JSON encoder to handle cases where the default encoder fails.
+ """
+ def default(self, obj):# pylint: disable=[arguments-renamed]
+ """Return a serializable object for `obj`."""
+ if type(obj) in __ENCODERS__:
+ return __ENCODERS__[type(obj)](obj)
+ return json.JSONEncoder.default(self, obj)
+
+
+__DECODERS__ = {
+ "UUID": UUID,
+ "DATETIME": datetime.fromisoformat
+}
+
+def custom_json_decoder(obj_dict):
+ """Decode custom types"""
+ if "__type" in obj_dict:
+ return __DECODERS__[obj_dict["__type"]](obj_dict["__value"])
+ return obj_dict