Commits

Chris Mutel  committed d0b49c2

Add JSONSanitizer

  • Participants
  • Parent commits 72e9fc9
  • Branches bw2package

Comments (0)

Files changed (1)

File bw2data/serialization.py

             return json.loads(data)
 
 
+class JSONSanitizer(object):
+    @classmethod
+    def sanitize(cls, data):
+        if isinstance(data, tuple):
+            return {
+                '__tuple__': True,
+                'data': [cls.sanitize(x) for x in data]
+            }
+        elif isinstance(data, dict):
+            return {
+                '__dict__': True,
+                'keys': [cls.sanitize(x) for x in data.keys()],
+                'values': [cls.sanitize(x) for x in data.values()]
+            }
+        elif isinstance(data, list):
+            return [cls.sanitize(x) for x in data]
+        else:
+            return data
+
+    @classmethod
+    def load(cls, data):
+        if isinstance(data, dict):
+            if "__tuple__" in data:
+                return tuple([cls.load(x) for x in data['data']])
+            elif "__dict__" in data:
+                return dict(zip(
+                    [cls.load(x) for x in data['keys']],
+                    [cls.load(x) for x in data['values']]
+                ))
+            else:
+                raise ValueError
+        elif isinstance(data, list):
+            return [cls.load(x) for x in data]
+        else:
+            return data
+
+
 class SerializedDict(object):
     """Base class for dictionary that can be serlialized to of unserialized from disk. Uses JSON as its storage format. Has most of the methods of a dictionary.