Skip to content

Instantly share code, notes, and snippets.

@eruvanos
Created May 12, 2019 13:14
Show Gist options
  • Save eruvanos/07f0cf098504b7d9de4e2350be51427c to your computer and use it in GitHub Desktop.
Save eruvanos/07f0cf098504b7d9de4e2350be51427c to your computer and use it in GitHub Desktop.
Enhance JSONEncoder supporting dataclasses
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