Skip to content

Commit

Permalink
[update] automatically convert dataclasses in JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
Animenosekai committed Aug 15, 2023
1 parent 2faf3b3 commit e7f60be
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
3 changes: 3 additions & 0 deletions nasse/utils/json.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
import json
import json.encoder
import typing
import dataclasses

from nasse import utils

Expand Down Expand Up @@ -323,6 +324,8 @@ def floatstr(o, allow_nan=self.allow_nan,
self.skipkeys, _one_shot)(o, 0)

def default(self, o: typing.Any) -> typing.Any:
if dataclasses.is_dataclass(o):
o = dataclasses.asdict(o)
if isinstance(o, str):
return json.encoder.py_encode_basestring(o)
elif isinstance(o, list): # some classes inheriting from list might have implemented methods to recognize them as unpackable
Expand Down
4 changes: 2 additions & 2 deletions nasse/utils/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
class LimitedString(str):
"""
A string with conditions
Variables to set: UPPER, LOWER, SPACES, THROW, REGEX, DEFAULT, LIMIT
"""
UPPER = False
Expand Down Expand Up @@ -52,7 +52,7 @@ def __new__(self, value: str, **kw):
class StringEnum(str):
"""
A string enum, only accepting certain values
Variables to set: ACCEPTED, DEFAULT, UPPER, LOWER
"""
ACCEPTED = tuple()
Expand Down

0 comments on commit e7f60be

Please sign in to comment.