Skip to content

Instantly share code, notes, and snippets.

@nackjicholson
Created June 9, 2019 04:52
Show Gist options
  • Save nackjicholson/a66a441961c58e0266f88e464e5cdf63 to your computer and use it in GitHub Desktop.
Save nackjicholson/a66a441961c58e0266f88e464e5cdf63 to your computer and use it in GitHub Desktop.
json encoder for dataclass instances and enum and times.
class EnhancedJSONEncoder(json.JSONEncoder):
def default(self, o):
if is_dataclass(o):
return asdict(o)
if isinstance(o, Enum):
return o.name
if isinstance(o, time):
return o.strftime("%H:%M")
return super().default(o)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment