Browse Source

flask_app: json格式化

Signed-off-by: binren <zhangbr@elab-plus.com>
binren 5 years ago
parent
commit
470c642c53
1 changed files with 12 additions and 1 deletions
  1. 12 1
      flask_app.py

+ 12 - 1
flask_app.py

@@ -5,6 +5,7 @@ from test_info import TestInfo
 from tongce import TongCe
 from apscheduler_elab import Config
 from flask_apscheduler import APScheduler
+import decimal
 from report_push import ReportPush
 
 
@@ -218,11 +219,21 @@ def report_test():
     try:
         reprt_push = ReportPush('bi_report')
         result = reprt_push.report_data_query(2)
-        return json.dumps(result, ensure_ascii=False, default=lambda obj: obj.__dict__)
+        return json.dumps(result, ensure_ascii=False, cls=DecimalEncoder)
     except Exception as e:
         return str(e)
 
 
+class DecimalEncoder(json.JSONEncoder):
+
+    def default(self, o):
+
+        if isinstance(o, decimal.Decimal):
+            return float(o)
+
+        super(DecimalEncoder, self).default(o)
+
+
 if __name__ == '__main__':
     # scheduler = APScheduler()
     # scheduler.init_app(app)