def test_empty_list(self): value = {"value": []} test1 = typed_encode(value) test2 = json2typed(pypy_json_encode(value)) expected = u'{"$object": ".", "value": []}' self.assertEqual(test1, expected) self.assertEqual(test2, expected)
def test_nested(self): value = {"a": {}, "b": {}} test1 = typed_encode(value) test2 = json2typed(pypy_json_encode(value)) expected = u'{"$object": ".", "a": {"$object": "."}, "b": {"$object": "."}}' self.assertEqual(test1, expected) self.assertEqual(test2, expected)
def test_unicode3(self): value = {"comment": u"testing accented char ŕáâăäĺćçčéęëěíîďđńňóôőö÷řůúűüýţ˙"} test1 = typed_encode(value) test2 = json2typed(pypy_json_encode(value)) expected = u'{"$object": ".", "comment": {"$value": "testing accented char ŕáâăäĺćçčéęëěíîďđńňóôőö÷řůúűüýţ˙"}}' self.assertEqual(test1, expected) self.assertEqual(test2, expected)
def test_double(self): value = {"value": 5.2025595183536973e-07} test1 = typed_encode(value) test2 = json2typed(pypy_json_encode(value)) expected = u'{"$object": ".", "value": {"$value": 5.202559518353697e-7}}' self.assertEqual(test1, expected) self.assertEqual(test2, expected)
def test_unicode1(self): value = {"comment": u"Open all links in the current tab, except the pages opened from external apps — open these ones in new windows"} test1 = typed_encode(value) test2 = json2typed(pypy_json_encode(value)) expected = u'{"$object": ".", "comment": {"$value": "Open all links in the current tab, except the pages opened from external apps — open these ones in new windows"}}' self.assertEqual(test1, expected) self.assertEqual(test2, expected)
def test_unicode2(self): value = {"comment": b"testing accented char àáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿ"} test1 = typed_encode(value) test2 = json2typed(pypy_json_encode(value)) expected = u'{"$object": ".", "comment": {"$value": "testing accented char àáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿ"}}' self.assertEqual(test1, expected) self.assertEqual(test2, expected)
def test_empty_list2(self): value = wrap({"a": [], "b": 1}) test1 = typed_encode(value) test2 = json2typed(pypy_json_encode(value)) expected = u'{"$object": ".", "a": [], "b": {"$value": 1}}' self.assertEqual(test1, expected) self.assertEqual(test2, expected)
def test_list_value(self): value = [42] test1 = typed_encode(value) test2 = json2typed(pypy_json_encode(value)) expected = u'[{"$value": 42}]' self.assertEqual(test1, expected) self.assertEqual(test2, expected)
def test_empty_dict(self): value = wrap({"match_all": wrap({})}) test1 = typed_encode(value) test2 = json2typed(pypy_json_encode(value)) expected = u'{"$object": ".", "match_all": {"$object": "."}}' self.assertEqual(test1, expected) self.assertEqual(test2, expected)
def test_complex_object(self): value = wrap({"s": 0, "r": 5}) test1 = typed_encode(value) test2 = json2typed(pypy_json_encode(value)) expected = u'{"$object": ".", "s": {"$value": 0}, "r": {"$value": 5}}' self.assertEqual(test1, expected) self.assertEqual(test2, expected)
def test_null(self): value = None test1 = typed_encode(value) test2 = json2typed(pypy_json_encode(value)) expected = '{"$value": null}' self.assertEqual(test1, expected) self.assertEqual(test2, expected)
def test_true(self): value = True test1 = typed_encode(value) test2 = json2typed(pypy_json_encode(value)) expected = '{"$value": true}' self.assertEqual(test1, expected) self.assertEqual(test2, expected)
def test_escaped_string_value(self): value = "\"" test1 = typed_encode(value) test2 = json2typed(pypy_json_encode(value)) expected = '{"$value": "\\""}' self.assertEqual(test1, expected) self.assertEqual(test2, expected)
def test_number_value(self): value = 42 test1 = typed_encode(value) test2 = json2typed(pypy_json_encode(value)) expected = '{"$value": 42}' self.assertEqual(test1, expected) self.assertEqual(test2, expected)
def test_date(self): value = {"test": datetime.date(2013, 11, 13)} test1 = typed_encode(value) test2 = json2typed(pypy_json_encode(value)) expected = u'{"$object": ".", "test": {"$value": 1384318800}}' self.assertEqual(test1, expected) self.assertEqual(test2, expected)
def test_empty_list_value(self): value = [] test1 = typed_encode(value) test2 = json2typed(pypy_json_encode(value)) expected = u'[]' self.assertEqual(test1, expected) self.assertEqual(test2, expected)
def test_list_of_objects(self): value = {"a": [{}, "b"]} test1 = typed_encode(value) test2 = json2typed(pypy_json_encode(value)) expected = u'{"$object": ".", "a": [{"$object": "."}, {"$value": "b"}]}' self.assertEqual(test1, expected) self.assertEqual(test2, expected)
def test_list(self): value = {"value": [23, 42]} test1 = typed_encode(value) test2 = json2typed(pypy_json_encode(value)) expected = u'{"$object": ".", "value": [{"$value": 23}, {"$value": 42}]}' self.assertEqual(test1, expected) self.assertEqual(test2, expected)
def test_default_python(self): test = {"add": Data(start=b"".join([" ", u"â€"]))} output = pypy_json_encode(test) expecting = u'{"add": {"start": " â€"}}' self.assertEqual(expecting, output, "expecting correct json")
def test_bad_long_json(self): test = pypy_json_encode({"values": [i for i in range(1000)]}) test = test[:1000] + "|" + test[1000:] expected = u"Can not decode JSON at:\n\t..., 216, 217, 218, 219|, 220, 221, 222, 22...\n\t ^\n" # expected = u'Can not decode JSON at:\n\t...9,270,271,272,273,27|4,275,276,277,278,2...\n\t ^\n' try: output = convert.json2value(test) Log.error("Expecting error") except Exception, e: if e.message != expected: Log.error("Expecting good error message", cause=e)
def value2json(obj, pretty=False, sort_keys=False): try: json = json_encoder(obj, pretty=pretty) if json == None: Log.note(str(type(obj)) + " is not valid{{type}}JSON", type= " (pretty) " if pretty else " ") Log.error("Not valid JSON: " + str(obj) + " of type " + str(type(obj))) return json except Exception, e: e = Except.wrap(e) with suppress_exception: json = pypy_json_encode(obj) return json Log.error("Can not encode into JSON: {{value}}", value=repr(obj), cause=e)
def test_generator(self): test = {"value": (x for x in [])} output = pypy_json_encode(test) if output != u'{"value": []}': Log.error("expecting correct value")
def test_double1(self): test = {"value": 5.2025595183536973e-07} output = pypy_json_encode(test) if output != u'{"value": 5.202559518353697e-7}': Log.error("expecting correct value")
def test_double3(self): test = {"value": .52} output = pypy_json_encode(test) if output != u'{"value": 0.52}': Log.error("expecting correct value")
def test_unicode2(self): output = pypy_json_encode({"comment": b"testing accented char àáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿ"}) assert output == u'{"comment": "testing accented char àáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿ"}' if not isinstance(output, unicode): Log.error("expecting unicode json")
def test_unicode3(self): output = pypy_json_encode({"comment": u"testing accented char ŕáâăäĺćçčéęëěíîďđńňóôőö÷řůúűüýţ˙"}) assert output == u'{"comment": "testing accented char ŕáâăäĺćçčéęëěíîďđńňóôőö÷řůúűüýţ˙"}' if not isinstance(output, unicode): Log.error("expecting unicode json")
def test_unicode1(self): output = pypy_json_encode({"comment": u"Open all links in the current tab, except the pages opened from external apps — open these ones in new windows"}) assert output == u'{"comment": "Open all links in the current tab, except the pages opened from external apps — open these ones in new windows"}' if not isinstance(output, unicode): Log.error("expecting unicode json")
def test_date(self): output = pypy_json_encode({"test": datetime.date(2013, 11, 13)}) Log.note("JSON = {{json}}", {"json": output})
def test_minus_inf(self): test = float("-inf") output = pypy_json_encode(test) expecting = cpython_json_encoder(jsons.scrub(test)) self.assertEqual(output, expecting, "expecting "+expecting)
def test_float(self): test = float(10.0) output = pypy_json_encode(test) expecting='10' self.assertEqual(output, expecting, "expecting integer")
def __json__(self): if self.json is not None: return self.json[self.start:self.end] else: return pypy_json_encode(self)