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 to_json(self, orient="index", double_precision=10, force_ascii=True):
    """
    Convert Series to a JSON string

    Note NaN's and None will be converted to null and datetime objects
    will be converted to UNIX timestamps.

    Parameters
    ----------
    orient : {'split', 'records', 'index'}, default 'index'
        The format of the JSON string
        split : dict like
            {index -> [index], name -> name, data -> [values]}
        records : list like [value, ... , value]
        index : dict like {index -> value}
    double_precision : The number of decimal places to use when encoding
        floating point values, default 10.
    force_ascii : force encoded string to be ASCII, default True.

    Returns
    -------
    result : JSON compatible string
    """
    return dumps(self, orient=orient, double_precision=double_precision,
                 ensure_ascii=force_ascii)
Example #4
0
def to_json(self, orient="columns", double_precision=10,
            force_ascii=True):
    """
    Convert DataFrame to a JSON string.

    Note NaN's and None will be converted to null and datetime objects
    will be converted to UNIX timestamps.

    Parameters
    ----------
    orient : {'split', 'records', 'index', 'columns', 'values'},
             default 'columns'
        The format of the JSON string
        split : dict like
            {index -> [index], columns -> [columns], data -> [values]}
        records : list like [{column -> value}, ... , {column -> value}]
        index : dict like {index -> {column -> value}}
        columns : dict like {column -> {index -> value}}
        values : just the values array
    double_precision : The number of decimal places to use when encoding
        floating point values, default 10.
    force_ascii : force encoded string to be ASCII, default True.

    Returns
    -------
    result : JSON compatible string
    """
    return dumps(self, orient=orient, double_precision=double_precision,
                 ensure_ascii=force_ascii)
Example #5
0
def to_json(self, orient="index", double_precision=10, force_ascii=True):
    """
    Convert Series to a JSON string

    Note NaN's and None will be converted to null and datetime objects
    will be converted to UNIX timestamps.

    Parameters
    ----------
    orient : {'split', 'records', 'index'}, default 'index'
        The format of the JSON string
        split : dict like
            {index -> [index], name -> name, data -> [values]}
        records : list like [value, ... , value]
        index : dict like {index -> value}
    double_precision : The number of decimal places to use when encoding
        floating point values, default 10.
    force_ascii : force encoded string to be ASCII, default True.

    Returns
    -------
    result : JSON compatible string
    """
    return dumps(self,
                 orient=orient,
                 double_precision=double_precision,
                 ensure_ascii=force_ascii)
Example #6
0
def to_json(self, orient="columns", double_precision=10, force_ascii=True):
    """
    Convert DataFrame to a JSON string.

    Note NaN's and None will be converted to null and datetime objects
    will be converted to UNIX timestamps.

    Parameters
    ----------
    orient : {'split', 'records', 'index', 'columns', 'values'},
             default 'columns'
        The format of the JSON string
        split : dict like
            {index -> [index], columns -> [columns], data -> [values]}
        records : list like [{column -> value}, ... , {column -> value}]
        index : dict like {index -> {column -> value}}
        columns : dict like {column -> {index -> value}}
        values : just the values array
    double_precision : The number of decimal places to use when encoding
        floating point values, default 10.
    force_ascii : force encoded string to be ASCII, default True.

    Returns
    -------
    result : JSON compatible string
    """
    return dumps(self,
                 orient=orient,
                 double_precision=double_precision,
                 ensure_ascii=force_ascii)
Example #7
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 #8
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 #9
0
 def handle_agg_force(self, request, frame):
     return json.dumps(get_force_data(frame, request))
Example #10
0
 def _respond(self, response, handler):
     output = {'response': response,
               'handler': handler}
     result = json.dumps(output);
     self.write_message(unicode(result))
Example #11
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 #12
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"