Exemple #1
0
 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)
Exemple #2
0
 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)
Exemple #3
0
 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)
Exemple #4
0
 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)
Exemple #5
0
 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)
Exemple #6
0
 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)
Exemple #7
0
 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)
Exemple #8
0
 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)
Exemple #9
0
 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)
Exemple #10
0
 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)
Exemple #11
0
 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)
Exemple #12
0
 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)
Exemple #13
0
 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)
Exemple #14
0
 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)
Exemple #15
0
 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)
Exemple #16
0
 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)
Exemple #17
0
 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)
Exemple #18
0
 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)
Exemple #19
0
    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")
Exemple #20
0
 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)
Exemple #21
0
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)
Exemple #22
0
 def test_generator(self):
     test = {"value": (x for x in [])}
     output = pypy_json_encode(test)
     if output != u'{"value": []}':
         Log.error("expecting correct value")
Exemple #23
0
 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")
Exemple #24
0
 def test_double3(self):
     test = {"value": .52}
     output = pypy_json_encode(test)
     if output != u'{"value": 0.52}':
         Log.error("expecting correct value")
Exemple #25
0
    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")
Exemple #26
0
 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")
Exemple #27
0
    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")
Exemple #28
0
 def test_date(self):
     output = pypy_json_encode({"test": datetime.date(2013, 11, 13)})
     Log.note("JSON = {{json}}", {"json": output})
Exemple #29
0
 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)
Exemple #30
0
 def test_float(self):
     test = float(10.0)
     output = pypy_json_encode(test)
     expecting='10'
     self.assertEqual(output, expecting, "expecting integer")
Exemple #31
0
 def __json__(self):
     if self.json is not None:
         return self.json[self.start:self.end]
     else:
         return pypy_json_encode(self)