Skip to content

Commit 6a9baed

Browse files
Merge pull request #65 from laugengebaeck/fix/enum-encoding
Fix broken enum encoding in JSON export
2 parents 9da1715 + 48218ad commit 6a9baed

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

yaramo/base_element.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
import simplejson as json
55

6+
from yaramo.utils.enum_encoder import EnumEncoder
7+
68

79
class BaseElement(object):
810
def __init__(self, uuid: str = None, name: str = None, **kwargs) -> None:
@@ -27,4 +29,4 @@ def to_serializable(self) -> Tuple[dict, dict]:
2729
return self.__dict__, {}
2830

2931
def to_json(self) -> str:
30-
return json.dumps(self.to_serializable()[0], iterable_as_array=True)
32+
return json.dumps(self.to_serializable()[0], iterable_as_array=True, cls=EnumEncoder)

yaramo/utils/enum_encoder.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from enum import Enum
2+
3+
import simplejson as json
4+
5+
class EnumEncoder(json.JSONEncoder):
6+
def default(self, o):
7+
if isinstance(o, Enum):
8+
return str(o)
9+
return super().default(o)

0 commit comments

Comments
 (0)