Example #1
0
    def testArrayNumpyLabelled(self):
        input = {'a': []}
        output = ujson.loads(ujson.dumps(input), numpy=True, labelled=True)
        self.assertTrue((np.empty((1, 0)) == output[0]).all())
        self.assertTrue((np.array(['a']) == output[1]).all())
        self.assertTrue(output[2] is None)

        input = [{'a': 42}]
        output = ujson.loads(ujson.dumps(input), numpy=True, labelled=True)
        self.assertTrue((np.array([42]) == output[0]).all())
        self.assertTrue(output[1] is None)
        self.assertTrue((np.array([u'a']) == output[2]).all())

        input = [{'a': 42, 'b':31}, {'a': 24, 'c': 99}, {'a': 2.4, 'b': 78}]
        output = ujson.loads(ujson.dumps(input), numpy=True, labelled=True)
        expectedvals = np.array([42, 31, 24, 99, 2.4, 78], dtype=int).reshape((3,2))
        self.assertTrue((expectedvals == output[0]).all())
        self.assertTrue(output[1] is None)
        self.assertTrue((np.array([u'a', 'b']) == output[2]).all())


        input = {1: {'a': 42, 'b':31}, 2: {'a': 24, 'c': 99}, 3: {'a': 2.4, 'b': 78}}
        output = ujson.loads(ujson.dumps(input), numpy=True, labelled=True)
        expectedvals = np.array([42, 31, 24, 99, 2.4, 78], dtype=int).reshape((3,2))
        self.assertTrue((expectedvals == output[0]).all())
        self.assertTrue((np.array(['1','2','3']) == output[1]).all())
        self.assertTrue((np.array(['a', 'b']) == output[2]).all())
Example #2
0
    def testArrayNumpyLabelled(self):
        input = {"a": []}
        output = ujson.loads(ujson.dumps(input), numpy=True, labelled=True)
        self.assertTrue((np.empty((1, 0)) == output[0]).all())
        self.assertTrue((np.array(["a"]) == output[1]).all())
        self.assertTrue(output[2] is None)

        input = [{"a": 42}]
        output = ujson.loads(ujson.dumps(input), numpy=True, labelled=True)
        self.assertTrue((np.array([42]) == output[0]).all())
        self.assertTrue(output[1] is None)
        self.assertTrue((np.array([u"a"]) == output[2]).all())

        input = [{"a": 42, "b": 31}, {"a": 24, "c": 99}, {"a": 2.4, "b": 78}]
        output = ujson.loads(ujson.dumps(input), numpy=True, labelled=True)
        expectedvals = np.array([42, 31, 24, 99, 2.4, 78], dtype=int).reshape((3, 2))
        self.assertTrue((expectedvals == output[0]).all())
        self.assertTrue(output[1] is None)
        self.assertTrue((np.array([u"a", "b"]) == output[2]).all())

        input = {1: {"a": 42, "b": 31}, 2: {"a": 24, "c": 99}, 3: {"a": 2.4, "b": 78}}
        output = ujson.loads(ujson.dumps(input), numpy=True, labelled=True)
        expectedvals = np.array([42, 31, 24, 99, 2.4, 78], dtype=int).reshape((3, 2))
        self.assertTrue((expectedvals == output[0]).all())
        self.assertTrue((np.array(["1", "2", "3"]) == output[1]).all())
        self.assertTrue((np.array(["a", "b"]) == output[2]).all())
Example #3
0
    def test_encodeNullCharacter(self):
        input = "31337 \x00 1337"
        output = ujson.encode(input)
        self.assertEquals(input, json.loads(output))
        self.assertEquals(output, json.dumps(input))
        self.assertEquals(input, ujson.decode(output))

        input = "\x00"
        output = ujson.encode(input)
        self.assertEquals(input, json.loads(output))
        self.assertEquals(output, json.dumps(input))
        self.assertEquals(input, ujson.decode(output))

        self.assertEquals('"  \\u0000\\r\\n "', ujson.dumps(u"  \u0000\r\n "))
        pass
Example #4
0
    def testArrayNumpyExcept(self):

        input = ujson.dumps([42, {}, 'a'])
        try:
            ujson.decode(input, numpy=True)
            assert False, "Expected exception!"
        except(TypeError):
            pass
        except:
            assert False, "Wrong exception"

        input = ujson.dumps(['a', 'b', [], 'c'])
        try:
            ujson.decode(input, numpy=True)
            assert False, "Expected exception!"
        except(ValueError):
            pass
        except:
            assert False, "Wrong exception"

        input = ujson.dumps([['a'], 42])
        try:
            ujson.decode(input, numpy=True)
            assert False, "Expected exception!"
        except(ValueError):
            pass
        except:
            assert False, "Wrong exception"

        input = ujson.dumps([42, ['a'], 42])
        try:
            ujson.decode(input, numpy=True)
            assert False, "Expected exception!"
        except(ValueError):
            pass
        except:
            assert False, "Wrong exception"

        input = ujson.dumps([{}, []])
        try:
            ujson.decode(input, numpy=True)
            assert False, "Expected exception!"
        except(ValueError):
            pass
        except:
            assert False, "Wrong exception"

        input = ujson.dumps([42, None])
        try:
            ujson.decode(input, numpy=True)
            assert False, "Expected exception!"
        except(TypeError):
            pass
        except:
            assert False, "Wrong exception"

        input = ujson.dumps([{'a': 'b'}])
        try:
            ujson.decode(input, numpy=True, labelled=True)
            assert False, "Expected exception!"
        except(ValueError):
            pass
        except:
            assert False, "Wrong exception"

        input = ujson.dumps({'a': {'b': {'c': 42}}})
        try:
            ujson.decode(input, numpy=True, labelled=True)
            assert False, "Expected exception!"
        except(ValueError):
            pass
        except:
            assert False, "Wrong exception"

        input = ujson.dumps([{'a': 42, 'b': 23}, {'c': 17}])
        try:
            ujson.decode(input, numpy=True, labelled=True)
            assert False, "Expected exception!"
        except(ValueError):
            pass
        except:
            assert False, "Wrong exception"
Example #5
0
    def testArrayNumpyExcept(self):

        input = ujson.dumps([42, {}, "a"])
        try:
            ujson.decode(input, numpy=True)
            assert False, "Expected exception!"
        except (TypeError):
            pass
        except:
            assert False, "Wrong exception"

        input = ujson.dumps(["a", "b", [], "c"])
        try:
            ujson.decode(input, numpy=True)
            assert False, "Expected exception!"
        except (ValueError):
            pass
        except:
            assert False, "Wrong exception"

        input = ujson.dumps([["a"], 42])
        try:
            ujson.decode(input, numpy=True)
            assert False, "Expected exception!"
        except (ValueError):
            pass
        except:
            assert False, "Wrong exception"

        input = ujson.dumps([42, ["a"], 42])
        try:
            ujson.decode(input, numpy=True)
            assert False, "Expected exception!"
        except (ValueError):
            pass
        except:
            assert False, "Wrong exception"

        input = ujson.dumps([{}, []])
        try:
            ujson.decode(input, numpy=True)
            assert False, "Expected exception!"
        except (ValueError):
            pass
        except:
            assert False, "Wrong exception"

        input = ujson.dumps([42, None])
        try:
            ujson.decode(input, numpy=True)
            assert False, "Expected exception!"
        except (TypeError):
            pass
        except:
            assert False, "Wrong exception"

        input = ujson.dumps([{"a": "b"}])
        try:
            ujson.decode(input, numpy=True, labelled=True)
            assert False, "Expected exception!"
        except (ValueError):
            pass
        except:
            assert False, "Wrong exception"

        input = ujson.dumps({"a": {"b": {"c": 42}}})
        try:
            ujson.decode(input, numpy=True, labelled=True)
            assert False, "Expected exception!"
        except (ValueError):
            pass
        except:
            assert False, "Wrong exception"

        input = ujson.dumps([{"a": 42, "b": 23}, {"c": 17}])
        try:
            ujson.decode(input, numpy=True, labelled=True)
            assert False, "Expected exception!"
        except (ValueError):
            pass
        except:
            assert False, "Wrong exception"