56 | def stringify(obj): |
57 | """Convert object to string. |
58 | If the object is a dictionary-like object or list, |
59 | the objects in the dictionary or list will be converted to strings, recursively. |
60 | |
61 | Returns: If the input is dictionary or list, the return value will also be a list or dictionary. |
62 | |
63 | """ |
64 | if isinstance(obj, collections.Mapping): |
65 | obj = copy.deepcopy(obj) |
66 | obj_dict = {} |
67 | for key, value in obj.items(): |
68 | obj_dict[key] = stringify(value) |
69 | return obj_dict |
70 | elif isinstance(obj, list): |
71 | str_list = [] |
72 | for item in obj: |
73 | str_list.append(stringify(item)) |
74 | return str_list |
75 | else: |
76 | try: |
77 | json.dumps(obj) |
78 | return obj |
79 | except TypeError: |
80 | return str(obj) |