aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorThéo de la Hogue2023-10-17 17:55:03 +0200
committerThéo de la Hogue2023-10-17 17:55:03 +0200
commit86264748b88700ae7a197bfee7004c5114b17225 (patch)
tree5bd3eda10d2b546860d803c7d7462af50c824806 /src
parent322fa8af22f8880d58506fc18f4205ac4d3f937a (diff)
downloadargaze-86264748b88700ae7a197bfee7004c5114b17225.zip
argaze-86264748b88700ae7a197bfee7004c5114b17225.tar.gz
argaze-86264748b88700ae7a197bfee7004c5114b17225.tar.bz2
argaze-86264748b88700ae7a197bfee7004c5114b17225.tar.xz
Fixing JsonEncoder to handle class with numpy attributes.
Diffstat (limited to 'src')
-rw-r--r--src/argaze/DataStructures.py25
1 files changed, 23 insertions, 2 deletions
diff --git a/src/argaze/DataStructures.py b/src/argaze/DataStructures.py
index 08a7d2c..b5101b2 100644
--- a/src/argaze/DataStructures.py
+++ b/src/argaze/DataStructures.py
@@ -45,6 +45,15 @@ def as_dict(dataclass_object) -> dict:
# Copy fields values
return {name: vars(dataclass_object)[name] for name in fields_names}
+def module_path(obj):
+ """
+ Get object module path.
+
+ Returns:
+ module path
+ """
+ return obj.__class__.__module__
+
class JsonEncoder(json.JSONEncoder):
"""Specific ArGaze JSON Encoder."""
@@ -55,10 +64,10 @@ class JsonEncoder(json.JSONEncoder):
if isinstance(obj, numpy.integer):
return int(obj)
- if isinstance(obj, numpy.floating):
+ elif isinstance(obj, numpy.floating):
return float(obj)
- if isinstance(obj, numpy.ndarray):
+ elif isinstance(obj, numpy.ndarray):
return obj.tolist()
# default case
@@ -73,7 +82,19 @@ class JsonEncoder(json.JSONEncoder):
public_dict = {}
for k, v in vars(obj).items():
+
if not k.startswith('_'):
+
+ # numpy cases
+ if isinstance(v, numpy.integer):
+ v = int(v)
+
+ elif isinstance(v, numpy.floating):
+ v = float(v)
+
+ elif isinstance(v, numpy.ndarray):
+ v = v.tolist()
+
public_dict[k] = v
return public_dict