Created
June 9, 2019 04:52
-
-
Save nackjicholson/a66a441961c58e0266f88e464e5cdf63 to your computer and use it in GitHub Desktop.
json encoder for dataclass instances and enum and times.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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