3
0
Fork 0

fix: add handling for decimal values and lists, in make_json_safe()

This commit is contained in:
Lance Edgar 2024-12-14 23:56:36 -06:00
parent dd1fd8c0ce
commit 30671fcd78
2 changed files with 41 additions and 4 deletions

View file

@ -1,5 +1,6 @@
# -*- coding: utf-8; -*-
import decimal
import json
import uuid as _uuid
from unittest import TestCase
@ -570,6 +571,12 @@ class TestMakeJsonSafe(TestCase):
value = mod.make_json_safe(uuid)
self.assertEqual(value, uuid.hex)
def test_decimal(self):
value = decimal.Decimal('42.42')
self.assertNotEqual(value, 42.42)
result = mod.make_json_safe(value)
self.assertEqual(result, 42.42)
def test_dict(self):
model = self.app.model
person = model.Person(full_name="Betty Boop")
@ -585,3 +592,21 @@ class TestMakeJsonSafe(TestCase):
'foo': 'bar',
'person': "Betty Boop",
})
def test_list(self):
model = self.app.model
person = model.Person(full_name="Betty Boop")
data = [
'foo',
'bar',
person,
]
self.assertRaises(TypeError, json.dumps, data)
value = mod.make_json_safe(data)
self.assertEqual(value, [
'foo',
'bar',
"Betty Boop",
])