From a85a7651efc46774926ccb4b9eb6d7b667aa2bbe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= Date: Fri, 20 May 2016 10:11:40 +0200 Subject: [PATCH] misc: let Decimal be encoded in json (as strings) (#11001) --- tests/test_workflows.py | 5 +++-- wcs/qommon/misc.py | 4 ++++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/tests/test_workflows.py b/tests/test_workflows.py index fb2c989..bf94d48 100644 --- a/tests/test_workflows.py +++ b/tests/test_workflows.py @@ -620,14 +620,15 @@ def test_webservice_call(pub): item = WebserviceCallStatusItem() item.url = 'http://remote.example.net' - item.post_data = {'str': 'abcd', 'one': '=1', + item.post_data = {'str': 'abcd', 'one': '=1', 'decimal': '=Decimal(2)', 'evalme': '=form_number', 'error':'=1=3'} pub.substitutions.feed(formdata) item.perform(formdata) assert http_requests.get_last('url') == 'http://remote.example.net' assert http_requests.get_last('method') == 'POST' payload = json.loads(http_requests.get_last('body')) - assert payload['extra'] == {'one': 1, 'str': 'abcd', 'evalme': formdata.get_display_id()} + assert payload['extra'] == {'one': 1, 'str': 'abcd', + 'decimal': '2', 'evalme': formdata.get_display_id()} assert payload['url'] == 'http://example.net/baz/%s/' % formdata.id assert payload['display_id'] == formdata.get_display_id() diff --git a/wcs/qommon/misc.py b/wcs/qommon/misc.py index 12b1b0a..32859d8 100644 --- a/wcs/qommon/misc.py +++ b/wcs/qommon/misc.py @@ -15,6 +15,7 @@ # along with this program; if not, see . import datetime +import decimal import calendar import re import os @@ -428,6 +429,9 @@ class JSONEncoder(json.JSONEncoder): if isinstance(obj, time.struct_time): return datetime.datetime.utcfromtimestamp(time.mktime(obj)).isoformat() + 'Z' + if isinstance(obj, decimal.Decimal): + return str(obj) + # PicklableUpload may be known as qommon.form.PicklableUpload or # wcs.qommon.form.PicklableUpload and considered different classes # by Python. It shouldn't have to be that way but workaround that -- 2.8.1