Recursively converts int64 and float64 to int and float in a dictionary. Parameters: - data (dict): Input dictionary with potentially non-serializable types. Returns: - dict: Output dictionary with serializable types.
(data)
| 102 | return obj, soup |
| 103 | |
| 104 | def convert_to_json_serializable(data): |
| 105 | """ |
| 106 | Recursively converts int64 and float64 to int and float in a dictionary. |
| 107 | |
| 108 | Parameters: |
| 109 | - data (dict): Input dictionary with potentially non-serializable types. |
| 110 | |
| 111 | Returns: |
| 112 | - dict: Output dictionary with serializable types. |
| 113 | """ |
| 114 | for key, value in data.items(): |
| 115 | if isinstance(value, (np.int64, np.int32, np.int16, np.int8)): |
| 116 | data[key] = int(value) |
| 117 | elif isinstance(value, np.float64): |
| 118 | data[key] = float(value) |
| 119 | elif isinstance(value, dict): |
| 120 | # Recursively convert nested dictionaries |
| 121 | data[key] = convert_to_json_serializable(value) |
| 122 | return data |
nothing calls this directly
no outgoing calls
no test coverage detected