Skip to content

Commit

Permalink
custom json encoder
Browse files Browse the repository at this point in the history
  • Loading branch information
lloydzhou committed Jan 2, 2024
1 parent 022ceb3 commit 869b13f
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions server/model/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,17 +320,20 @@ class ChatGroup(Base):
)


def _default(value):
if isinstance(value, db.Model):
return value.__dict__
elif isinstance(value, Decimal):
return int(value)
elif isinstance(value, datetime):
return value.strftime("%Y-%m-%d %H:%M:%S")
elif isinstance(value, time):
return value.isoformat()
return str(value)


class CustomJsonProvider(DefaultJSONProvider):
def dumps(self, value, **kw):
if isinstance(value, db.Model):
value = value.__dict__
elif isinstance(value, Decimal):
value = int(value)
elif isinstance(value, datetime):
value = value.strftime("%Y-%m-%d %H:%M:%S")
elif isinstance(value, time):
value = value.isoformat()
return super().dumps(value, **kw) # Delegate to the default dumps
default = _default


app.json_provider_class = CustomJsonProvide
Expand Down

0 comments on commit 869b13f

Please sign in to comment.