def test_decimal(self):
     enc = ImprovedDjangoJSONEncoder()
     i_str = '3.99999999999999999999999999999999999999999999999999999999999999999999999999999999'
     d = Decimal(i_str)
     self.assertEqual(i_str, str(d))
     s = enc.encode(d)
     self.assertEqual(s, '"' + i_str + '"')
Example #2
0
    def test_date(self):
        d = datetime.datetime(2019, 9, 18, 21, 30, 7, 303)
        enc = ImprovedDjangoJSONEncoder()
        s = enc.encode(d)
        self.assertEqual(s, '"2019-09-18T21:30:07.000"')

        d = datetime.date(2019, 9, 18)
        s = enc.encode(d)
        self.assertEqual(s, '"2019-09-18"')
Example #3
0
 def test_class(self):
     d = self.TestSrClass(10,
                          'miles',
                          c=self.TestSrClass(a=1, b='2', c={'35': 35}))
     enc = ImprovedDjangoJSONEncoder()
     s = enc.encode(d)
     # should be something like: {"a": 10, "b": "miles", "c": {"a": 1, "b": "2", "c": {"35": 35}}}
     self.assertTrue(
         s.startswith('{') and s.endswith('}') and '"a": 10' in s
         and '"b": "miles"' in s and '{"35": 35}' in s)
    def test_decimal_in_struct(self):
        enc = ImprovedDjangoJSONEncoder(indent=2)
        i_str = '3.99999999999999999999999999999999999999999999999999999999999999'
        d = Decimal(i_str)
        dd = {'a': 'b', 'f': 0.1 + 0.2, 'd': [d, d + 1, d + 2, d + (4 - d)]}

        expected = '''{
  "a": "b",
  "f": 0.30000000000000004,
  "d": [
    "3.99999999999999999999999999999999999999999999999999999999999999",
    "5.000000000000000000000000000",
    "6.000000000000000000000000000",
    "4.000000000000000000000000000"
  ]
}'''
        actual = enc.encode(dd)
        self.assertEqual(expected, actual)
def serialize(obj):
    if isinstance(obj, Mapping):
        for k, v in obj.items():
            obj[k] = serialize(v)
    elif isinstance(obj, Collection) and not isinstance(obj, str):
        for i in obj:
            i = serialize(i)
    elif isinstance(obj, (datetime.datetime, datetime.date, datetime.time)):
        obj = ImprovedDjangoJSONEncoder().default(obj)
    elif isinstance(obj, decimal.Decimal):
        obj = float(obj)  # ImprovedDjangoJSONEncoder().default(obj)
    return obj
Example #6
0
 def test_set(self):
     d = {'alpha', 'bravo', '', 'delta'}
     enc = ImprovedDjangoJSONEncoder()
     s = enc.encode(d)
     self.assertTrue(
         s.startswith('[') and s.endswith(']') and all(i in s for i in d))
Example #7
0
 def test_float(self):
     enc = ImprovedDjangoJSONEncoder()
     i = -123.45
     s = enc.encode(i)
     self.assertEqual(s, '-123.45')
Example #8
0
 def test_int(self):
     enc = ImprovedDjangoJSONEncoder()
     i = 12345
     s = enc.encode(i)
     self.assertEqual(s, '12345')
Example #9
0
def json_gen(v: Any, encoder=None) -> Generator[str, None, None]:
    if not encoder:
        encoder = ImprovedDjangoJSONEncoder()

    yield from encoder.iterencode(v)
 def test_long_float(self):
     enc = ImprovedDjangoJSONEncoder()
     f = 3.99999999999999999999999999999999999999999999999999999999999999999999999999999999
     self.assertEqual(f, 4)
     s = enc.encode(f)
     self.assertEqual(s, '4.0')