Commit b3186283 authored by John Wang's avatar John Wang

fix: uuid json dumps bug

parent 8b6bf432
# -*- coding:utf-8 -*- # -*- coding:utf-8 -*-
import os import os
import uuid
from datetime import datetime from datetime import datetime
if not os.environ.get("DEBUG") or os.environ.get("DEBUG").lower() != 'true': if not os.environ.get("DEBUG") or os.environ.get("DEBUG").lower() != 'true':
...@@ -183,6 +184,15 @@ if app.config['TESTING']: ...@@ -183,6 +184,15 @@ if app.config['TESTING']:
print("App is running in TESTING mode") print("App is running in TESTING mode")
def uuid_encoder(self, obj):
if isinstance(obj, uuid.UUID):
return str(obj)
raise TypeError(f"Object of type '{type(obj)}' is not JSON serializable")
json.JSONEncoder.default = uuid_encoder
@app.after_request @app.after_request
def after_request(response): def after_request(response):
"""Add Version headers to the response.""" """Add Version headers to the response."""
......
import json import json
import uuid
from collections import defaultdict from collections import defaultdict
from typing import Any, List, Optional, Dict from typing import Any, List, Optional, Dict
...@@ -235,4 +236,6 @@ class SetEncoder(json.JSONEncoder): ...@@ -235,4 +236,6 @@ class SetEncoder(json.JSONEncoder):
def default(self, obj): def default(self, obj):
if isinstance(obj, set): if isinstance(obj, set):
return list(obj) return list(obj)
return super().default(obj) elif isinstance(obj, uuid.UUID):
\ No newline at end of file return str(obj)
return super().default(obj)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment