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)
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)
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)
def test_double_clean1a(self): test = {"value": 0.0000999927520752} output = pypy_json_encode(test) self.assertEqual(output, '{"value":1.0e-4}')
def test_long2(self): test = {"value": 9999} output = pypy_json_encode(test) self.assertEqual(output, '{"value":9999}')
def test_double_clean3(self): test = {"value": 8999.0} output = pypy_json_encode(test) self.assertEqual(output, '{"value":9000}')
def value2json(value): return pypy_json_encode(value)
def test_double_clean1E(self): test = {"value": 0.999927520752} output = pypy_json_encode(test) self.assertEqual(output, u'{"value":1}')