1import gdb 2 3class JsonValuePrinter: 4 "Print a json-value" 5 6 def __init__(self, val): 7 self.val = val 8 9 def to_string(self): 10 if self.val.type.strip_typedefs().code == gdb.TYPE_CODE_FLT: 11 return ("%.6f" % float(self.val)).rstrip("0") 12 return self.val 13 14def json_lookup_function(val): 15 name = val.type.strip_typedefs().name 16 if name and name.startswith("nlohmann::basic_json<") and name.endswith(">"): 17 t = str(val['m_type']) 18 if t.startswith("nlohmann::detail::value_t::"): 19 try: 20 union_val = val['m_value'][t[27:]] 21 if union_val.type.code == gdb.TYPE_CODE_PTR: 22 return gdb.default_visualizer(union_val.dereference()) 23 else: 24 return JsonValuePrinter(union_val) 25 except: 26 return JsonValuePrinter(val['m_type']) 27 28gdb.pretty_printers.append(json_lookup_function) 29