Ejemplo n.º 1
0
def value2json(obj, pretty=False, sort_keys=False, keep_whitespace=True):
    """
    :param obj:  THE VALUE TO TURN INTO JSON
    :param pretty: True TO MAKE A MULTI-LINE PRETTY VERSION
    :param sort_keys: True TO SORT KEYS
    :param keep_whitespace: False TO strip() THE WHITESPACE IN THE VALUES
    :return:
    """
    if FIND_LOOPS:
        obj = scrub(obj,
                    scrub_text=_keep_whitespace
                    if keep_whitespace else _trim_whitespace())
    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 as e:
        e = Except.wrap(e)
        try:
            json = pypy_json_encode(obj)
            return json
        except Exception:
            pass
        Log.error("Can not encode into JSON: {{value}}",
                  value=text_type(repr(obj)),
                  cause=e)
Ejemplo n.º 2
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 as e:
        e = Except.wrap(e)
        try:
            json = pypy_json_encode(obj)
            return json
        except Exception:
            pass
        Log.error("Can not encode into JSON: {{value}}", value=repr(obj), cause=e)
Ejemplo n.º 3
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 as e:
        e = Except.wrap(e)
        try:
            json = pypy_json_encode(obj)
            return json
        except Exception:
            pass
        Log.error("Can not encode into JSON: {{value}}",
                  value=repr(obj),
                  cause=e)
Ejemplo n.º 4
0
def value2json(obj, pretty=False, sort_keys=False, keep_whitespace=True):
    """
    :param obj:  THE VALUE TO TURN INTO JSON
    :param pretty: True TO MAKE A MULTI-LINE PRETTY VERSION
    :param sort_keys: True TO SORT KEYS
    :param keep_whitespace: False TO strip() THE WHITESPACE IN THE VALUES
    :return:
    """
    if FIND_LOOPS:
        obj = scrub(obj, scrub_text=_keep_whitespace if keep_whitespace else _trim_whitespace())
    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 as e:
        e = Except.wrap(e)
        try:
            json = pypy_json_encode(obj)
            return json
        except Exception:
            pass
        Log.error("Can not encode into JSON: {{value}}", value=text_type(repr(obj)), cause=e)
Ejemplo n.º 5
0
 def test_double_clean1a(self):
     test = {"value": 0.0000999927520752}
     output = pypy_json_encode(test)
     self.assertEqual(output, '{"value":1.0e-4}')
Ejemplo n.º 6
0
 def test_long2(self):
     test = {"value": 9999}
     output = pypy_json_encode(test)
     self.assertEqual(output, '{"value":9999}')
Ejemplo n.º 7
0
 def test_double_clean3(self):
     test = {"value": 8999.0}
     output = pypy_json_encode(test)
     self.assertEqual(output, '{"value":9000}')
Ejemplo n.º 8
0
def value2json(value):
    return pypy_json_encode(value)
Ejemplo n.º 9
0
 def test_double_clean1E(self):
     test = {"value": 0.999927520752}
     output = pypy_json_encode(test)
     self.assertEqual(output, u'{"value":1}')