예제 #1
0
        def check(indent, expected):
            d1 = json.dumpsJSON(h, indent=indent)
            self.assertEqual(d1, expected)

            sio = StringIO()
            json.dumpJSON(h, sio, indent=indent)
            self.assertEqual(sio.getvalue(), expected)
예제 #2
0
        def check(indent, expected):
            d1 = json.dumpsJSON(h, indent=indent)
            self.assertEqual(d1, expected)

            sio = StringIO()
            json.dumpJSON(h, sio, indent=indent)
            self.assertEqual(sio.getvalue(), expected)
예제 #3
0
 def test_tuple_array_dump(self):
     t = (1, 2, 3)
     expect = json.dumpsJSON(list(t))
     # Default is True
     sio = StringIO()
     json.dumpJSON(t, sio)
     self.assertEqual(expect, sio.getvalue())
     sio = StringIO()
     json.dumpJSON(t, sio, tuple_as_array=True)
     self.assertEqual(expect, sio.getvalue())
     self.assertRaises(TypeError, json.dumpJSON, t, StringIO(),
                       tuple_as_array=False)
     # Ensure that the "default" does not get called
     sio = StringIO()
     json.dumpJSON(t, sio, default=repr)
     self.assertEqual(expect, sio.getvalue())
     sio = StringIO()
     json.dumpJSON(t, sio, tuple_as_array=True, default=repr)
     self.assertEqual(expect, sio.getvalue())
     # Ensure that the "default" gets called
     sio = StringIO()
     json.dumpJSON(t, sio, tuple_as_array=False, default=repr)
     self.assertEqual(
         json.dumpsJSON(repr(t)),
         sio.getvalue())
예제 #4
0
 def test_sort_keys(self):
     # https://github.com/simplejson/simplejson/issues/106
     for num_keys in range(2, 32):
         p = dict((str(x), x) for x in range(num_keys))
         sio = StringIO()
         json.dumpJSON(p, sio, sort_keys=True)
         self.assertEqual(sio.getvalue(), json.dumpsJSON(p, sort_keys=True))
         self.assertEqual(json.loads(sio.getvalue()), p)
예제 #5
0
 def test_sort_keys(self):
     # https://github.com/simplejson/simplejson/issues/106
     for num_keys in range(2, 32):
         p = {str(x): x for x in range(num_keys)}
         sio = StringIO()
         json.dumpJSON(p, sio, sort_keys=True)
         self.assertEqual(sio.getvalue(), json.dumpsJSON(p, sort_keys=True))
         self.assertEqual(json.loads(sio.getvalue()), p)
예제 #6
0
 def test_asdict_not_callable_dump(self):
     for f in CONSTRUCTORS:
         self.assertRaises(TypeError,
             json.dumpJSON, f(DeadDuck()), StringIO(), namedtuple_as_object=True)
         sio = StringIO()
         json.dumpJSON(f(DeadDict()), sio, namedtuple_as_object=True)
         self.assertEqual(
             json.dumpsJSON(f({})),
             sio.getvalue())
 def test_asdict_not_callable_dump(self):
     for f in CONSTRUCTORS:
         self.assertRaises(TypeError,
                           json.dumpJSON,
                           f(DeadDuck()),
                           StringIO(),
                           namedtuple_as_object=True)
         sio = StringIO()
         json.dumpJSON(f(DeadDict()), sio, namedtuple_as_object=True)
         self.assertEqual(json.dumpsJSON(f({})), sio.getvalue())
예제 #8
0
 def test_namedtuple_dump_false(self):
     for v in [Value(1), Point(1, 2)]:
         l = list(v)
         sio = StringIO()
         json.dumpJSON(v, sio, namedtuple_as_object=False)
         self.assertEqual(
             l,
             json.loads(sio.getvalue()))
         self.assertRaises(TypeError, json.dumpJSON, v, StringIO(),
             tuple_as_array=False, namedtuple_as_object=False)
 def test_namedtuple_dump_false(self):
     for v in [Value(1), Point(1, 2)]:
         l = list(v)
         sio = StringIO()
         json.dumpJSON(v, sio, namedtuple_as_object=False)
         self.assertEqual(l, json.loads(sio.getvalue()))
         self.assertRaises(TypeError,
                           json.dumpJSON,
                           v,
                           StringIO(),
                           tuple_as_array=False,
                           namedtuple_as_object=False)
 def test_decimal_defaults(self):
     d = Decimal('1.1')
     # use_decimal=True is the default
     self.assertRaises(TypeError, json.dumpsJSON, d, use_decimal=False)
     self.assertEqual('1.1', json.dumpsJSON(d))
     self.assertEqual('1.1', json.dumpsJSON(d, use_decimal=True))
     self.assertRaises(TypeError, json.dumpJSON, d, StringIO(),
                       use_decimal=False)
     sio = StringIO()
     json.dumpJSON(d, sio)
     self.assertEqual('1.1', sio.getvalue())
     sio = StringIO()
     json.dumpJSON(d, sio, use_decimal=True)
     self.assertEqual('1.1', sio.getvalue())
예제 #11
0
 def test_object_pairs_hook(self):
     s = '{"xkd":1, "kcw":2, "art":3, "hxm":4, "qrt":5, "pad":6, "hoy":7}'
     p = [
         ("xkd", 1),
         ("kcw", 2),
         ("art", 3),
         ("hxm", 4),
         ("qrt", 5),
         ("pad", 6),
         ("hoy", 7),
     ]
     self.assertEqual(json.loads(s), eval(s))
     self.assertEqual(json.loads(s, object_pairs_hook=lambda x: x), p)
     self.assertEqual(json.load(StringIO(s), object_pairs_hook=lambda x: x),
                      p)
     od = json.loads(s, object_pairs_hook=OrderedDict)
     self.assertEqual(od, OrderedDict(p))
     self.assertEqual(type(od), OrderedDict)
     # the object_pairs_hook takes priority over the object_hook
     self.assertEqual(
         json.loads(s,
                    object_pairs_hook=OrderedDict,
                    object_hook=lambda x: None),
         OrderedDict(p),
     )
예제 #12
0
 def test_namedtuple_dump(self):
     for v in [Value(1), Point(1, 2), DuckValue(1), DuckPoint(1, 2)]:
         d = v._asdict()
         sio = StringIO()
         json.dumpJSON(v, sio)
         self.assertEqual(d, json.loads(sio.getvalue()))
         sio = StringIO()
         json.dumpJSON(v, sio, namedtuple_as_object=True)
         self.assertEqual(
             d,
             json.loads(sio.getvalue()))
         sio = StringIO()
         json.dumpJSON(v, sio, tuple_as_array=False)
         self.assertEqual(d, json.loads(sio.getvalue()))
         sio = StringIO()
         json.dumpJSON(v, sio, namedtuple_as_object=True,
                   tuple_as_array=False)
         self.assertEqual(
             d,
             json.loads(sio.getvalue()))
 def test_decimal_defaults(self):
     d = Decimal('1.1')
     # use_decimal=True is the default
     self.assertRaises(TypeError, json.dumpsJSON, d, use_decimal=False)
     self.assertEqual('1.1', json.dumpsJSON(d))
     self.assertEqual('1.1', json.dumpsJSON(d, use_decimal=True))
     self.assertRaises(TypeError,
                       json.dumpJSON,
                       d,
                       StringIO(),
                       use_decimal=False)
     sio = StringIO()
     json.dumpJSON(d, sio)
     self.assertEqual('1.1', sio.getvalue())
     sio = StringIO()
     json.dumpJSON(d, sio, use_decimal=True)
     self.assertEqual('1.1', sio.getvalue())
 def loads(self, s, **kw):
     sio = StringIO(s)
     res = json.loads(s, **kw)
     self.assertEqual(res, json.load(sio, **kw))
     return res
 def dumps(self, obj, **kw):
     sio = StringIO()
     json.dumpJSON(obj, sio, **kw)
     res = json.dumpsJSON(obj, **kw)
     self.assertEqual(res, sio.getvalue())
     return res
예제 #16
0
 def test_dump(self):
     sio = StringIO()
     json.dumpJSON({}, sio)
     self.assertEqual(sio.getvalue(), "{}")
예제 #17
0
 def test_dump(self):
     sio = StringIO()
     json.dumpJSON({}, sio)
     self.assertEqual(sio.getvalue(), '{}')
 def test_namedtuple_dump(self):
     for v in [Value(1), Point(1, 2), DuckValue(1), DuckPoint(1, 2)]:
         d = v._asdict()
         sio = StringIO()
         json.dumpJSON(v, sio)
         self.assertEqual(d, json.loads(sio.getvalue()))
         sio = StringIO()
         json.dumpJSON(v, sio, namedtuple_as_object=True)
         self.assertEqual(d, json.loads(sio.getvalue()))
         sio = StringIO()
         json.dumpJSON(v, sio, tuple_as_array=False)
         self.assertEqual(d, json.loads(sio.getvalue()))
         sio = StringIO()
         json.dumpJSON(v,
                       sio,
                       namedtuple_as_object=True,
                       tuple_as_array=False)
         self.assertEqual(d, json.loads(sio.getvalue()))
 def dumps(self, obj, **kw):
     sio = StringIO()
     json.dumpJSON(obj, sio, **kw)
     res = json.dumpsJSON(obj, **kw)
     self.assertEqual(res, sio.getvalue())
     return res
예제 #20
0
 def test_tuple_array_dump(self):
     t = (1, 2, 3)
     expect = json.dumpsJSON(list(t))
     # Default is True
     sio = StringIO()
     json.dumpJSON(t, sio)
     self.assertEqual(expect, sio.getvalue())
     sio = StringIO()
     json.dumpJSON(t, sio, tuple_as_array=True)
     self.assertEqual(expect, sio.getvalue())
     self.assertRaises(TypeError,
                       json.dumpJSON,
                       t,
                       StringIO(),
                       tuple_as_array=False)
     # Ensure that the "default" does not get called
     sio = StringIO()
     json.dumpJSON(t, sio, default=repr)
     self.assertEqual(expect, sio.getvalue())
     sio = StringIO()
     json.dumpJSON(t, sio, tuple_as_array=True, default=repr)
     self.assertEqual(expect, sio.getvalue())
     # Ensure that the "default" gets called
     sio = StringIO()
     json.dumpJSON(t, sio, tuple_as_array=False, default=repr)
     self.assertEqual(json.dumpsJSON(repr(t)), sio.getvalue())