Server : Apache System : Linux indy02.toastserver.com 3.10.0-962.3.2.lve1.5.85.el7.x86_64 #1 SMP Thu Apr 18 15:18:36 UTC 2024 x86_64 User : palandch ( 1163) PHP Version : 7.1.33 Disable Function : NONE Directory : /opt/imunify360/venv/lib64/python3.11/site-packages/simplejson/tests/ |
from unittest import TestCase import simplejson as json from decimal import Decimal class AlternateInt(int): def __repr__(self): return 'invalid json' __str__ = __repr__ class AlternateFloat(float): def __repr__(self): return 'invalid json' __str__ = __repr__ # class AlternateDecimal(Decimal): # def __repr__(self): # return 'invalid json' class TestSubclass(TestCase): def test_int(self): self.assertEqual(json.dumps(AlternateInt(1)), '1') self.assertEqual(json.dumps(AlternateInt(-1)), '-1') self.assertEqual(json.loads(json.dumps({AlternateInt(1): 1})), {'1': 1}) def test_float(self): self.assertEqual(json.dumps(AlternateFloat(1.0)), '1.0') self.assertEqual(json.dumps(AlternateFloat(-1.0)), '-1.0') self.assertEqual(json.loads(json.dumps({AlternateFloat(1.0): 1})), {'1.0': 1}) # NOTE: Decimal subclasses are not supported as-is # def test_decimal(self): # self.assertEqual(json.dumps(AlternateDecimal('1.0')), '1.0') # self.assertEqual(json.dumps(AlternateDecimal('-1.0')), '-1.0')