2008-05-05 17:21:38 -03:00
|
|
|
import decimal
|
2009-03-29 19:33:58 -03:00
|
|
|
from StringIO import StringIO
|
2009-03-19 16:19:03 -03:00
|
|
|
from collections import OrderedDict
|
2011-05-14 00:24:53 -03:00
|
|
|
from json.tests import PyTest, CTest
|
|
|
|
|
2008-05-05 17:21:38 -03:00
|
|
|
|
2011-05-14 00:24:53 -03:00
|
|
|
class TestDecode(object):
|
2008-05-05 17:21:38 -03:00
|
|
|
def test_decimal(self):
|
2011-05-14 00:24:53 -03:00
|
|
|
rval = self.loads('1.1', parse_float=decimal.Decimal)
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(isinstance(rval, decimal.Decimal))
|
2010-11-21 09:34:58 -04:00
|
|
|
self.assertEqual(rval, decimal.Decimal('1.1'))
|
2008-05-05 17:21:38 -03:00
|
|
|
|
|
|
|
def test_float(self):
|
2011-05-14 00:24:53 -03:00
|
|
|
rval = self.loads('1', parse_int=float)
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(isinstance(rval, float))
|
2010-11-21 09:34:58 -04:00
|
|
|
self.assertEqual(rval, 1.0)
|
2009-03-17 20:19:00 -03:00
|
|
|
|
|
|
|
def test_decoder_optimizations(self):
|
|
|
|
# Several optimizations were made that skip over calls to
|
|
|
|
# the whitespace regex, so this test is designed to try and
|
|
|
|
# exercise the uncommon cases. The array cases are already covered.
|
2011-05-14 00:24:53 -03:00
|
|
|
rval = self.loads('{ "key" : "value" , "k":"v" }')
|
2010-11-21 09:34:58 -04:00
|
|
|
self.assertEqual(rval, {"key":"value", "k":"v"})
|
2009-03-19 16:19:03 -03:00
|
|
|
|
2011-04-12 23:37:29 -03:00
|
|
|
def test_empty_objects(self):
|
2011-05-14 00:24:53 -03:00
|
|
|
self.assertEqual(self.loads('{}'), {})
|
|
|
|
self.assertEqual(self.loads('[]'), [])
|
|
|
|
self.assertEqual(self.loads('""'), u"")
|
|
|
|
self.assertIsInstance(self.loads('""'), unicode)
|
2011-04-12 23:37:29 -03:00
|
|
|
|
2009-03-19 16:19:03 -03:00
|
|
|
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)]
|
2011-05-14 00:24:53 -03:00
|
|
|
self.assertEqual(self.loads(s), eval(s))
|
|
|
|
self.assertEqual(self.loads(s, object_pairs_hook=lambda x: x), p)
|
|
|
|
self.assertEqual(self.json.load(StringIO(s),
|
|
|
|
object_pairs_hook=lambda x: x), p)
|
|
|
|
od = self.loads(s, object_pairs_hook=OrderedDict)
|
2009-03-19 16:19:03 -03:00
|
|
|
self.assertEqual(od, OrderedDict(p))
|
|
|
|
self.assertEqual(type(od), OrderedDict)
|
|
|
|
# the object_pairs_hook takes priority over the object_hook
|
2011-05-14 00:24:53 -03:00
|
|
|
self.assertEqual(self.loads(s,
|
2009-03-29 19:33:58 -03:00
|
|
|
object_pairs_hook=OrderedDict,
|
|
|
|
object_hook=lambda x: None),
|
2009-03-19 16:19:03 -03:00
|
|
|
OrderedDict(p))
|
2011-05-14 00:24:53 -03:00
|
|
|
|
2012-08-18 15:51:05 -03:00
|
|
|
def test_extra_data(self):
|
|
|
|
s = '[1, 2, 3]5'
|
|
|
|
msg = 'Extra data'
|
|
|
|
self.assertRaisesRegexp(ValueError, msg, self.loads, s)
|
|
|
|
|
|
|
|
def test_invalid_escape(self):
|
|
|
|
s = '["abc\\y"]'
|
|
|
|
msg = 'escape'
|
|
|
|
self.assertRaisesRegexp(ValueError, msg, self.loads, s)
|
2011-05-14 00:24:53 -03:00
|
|
|
|
|
|
|
class TestPyDecode(TestDecode, PyTest): pass
|
|
|
|
class TestCDecode(TestDecode, CTest): pass
|