def _load_json_object(self, data): if data is None: return None data = misc.loads_jsonx(data) if not isinstance(data, dict): raise Exception("text/json type data after loads should be dict type") return data
def test_dumps_jsonx(self): test_cases = [{"abc" : 1, "doc__" : "xxyyzz"},{"doc2__" : "xx", "x" : 1, "doc1__" : "yy", "y" : 2}] for obj in test_cases: print obj binary = misc.dumps_jsonx(copy.deepcopy(obj)) print binary ret_obj = misc.loads_jsonx(binary) print ret_obj self.assertTrue(len(obj) == len(ret_obj)) for key, value in obj.items(): self.assertEqual(value, ret_obj[key]) self.assertTrue(obj == ret_obj)
def test_dumps_jsonx_body(self): req = urllib2.Request("http://www.5173.com") res = urllib2.urlopen(req) body = res.read() body = body.decode("utf-8", "ignore").encode("utf-8") message = {"doc__" : body, "url" : "xx"} binary = misc.dumps_jsonx(copy.deepcopy(message)) ret_message = misc.loads_jsonx(binary) self.assertEqual(message, ret_message) try: simplejson.loads(simplejson.dumps(message)) self.assertFail() except: pass