File tree Expand file tree Collapse file tree 2 files changed +29
-0
lines changed
Expand file tree Collapse file tree 2 files changed +29
-0
lines changed Original file line number Diff line number Diff line change 1+ Add SNPITJsonEncoder to utils.py
Original file line number Diff line number Diff line change 22
33import os
44import numbers
5+ import datetime
6+ import json
7+ import uuid
58import collections .abc
9+
610import numpy as np
711
812
@@ -57,3 +61,27 @@ def parse_bool(text):
5761def env_as_bool ( varname ):
5862 """Parse an environment variable as a boolean."""
5963 return parse_bool ( os .getenv (varname ) )
64+
65+
66+ class SNPITJsonEncoder ( json .JSONEncoder ):
67+ """Some specific encodings we need for the JSON use.
68+
69+ We want to know how to encode UUIDs to strings.
70+
71+ We want to be able to encoded numpy stuff.
72+
73+ Encode datetime to isoformat strings.
74+
75+ """
76+
77+ def default ( self , obj ):
78+ if isinstance ( obj , uuid .UUID ):
79+ return str
80+ if isinstance ( obj , np .floating ):
81+ return float ( obj )
82+ if isinstance ( obj , np .bool_ ):
83+ return bool ( obj )
84+ if isinstance ( obj , np .ndarray ):
85+ return obj .tolist ()
86+ if isinstance (obj , datetime ):
87+ return obj .isoformat ()
You can’t perform that action at this time.
0 commit comments