Created
May 12, 2019 13:14
-
-
Save eruvanos/07f0cf098504b7d9de4e2350be51427c to your computer and use it in GitHub Desktop.
Enhance JSONEncoder supporting dataclasses
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
from flask import Flask | |
from flask.json import JSONEncoder | |
class EnhancedJSONEncoder(JSONEncoder): | |
def default(self, o): | |
if dataclasses.is_dataclass(o): | |
return dataclasses.asdict(o) | |
return super().default(o) | |
app = Flask(__name__) | |
app.json_encoder = EnhancedJSONEncoder |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment