MCPcopy Create free account
hub / github.com/SensAI-PT/RAGMeUp / SafeJSONEncoder

Class SafeJSONEncoder

server/server.py:11–24  ·  view source on GitHub ↗

JSON encoder that handles numpy types, Decimals, and other edge cases.

Source from the content-addressed store, hash-verified

9
10class SafeJSONEncoder(json.JSONEncoder):
11 """JSON encoder that handles numpy types, Decimals, and other edge cases."""
12 def default(self, obj):
13 if isinstance(obj, (np.integer,)):
14 return int(obj)
15 if isinstance(obj, (np.floating,)):
16 return float(obj)
17 if isinstance(obj, np.ndarray):
18 return obj.tolist()
19 if isinstance(obj, Decimal):
20 return float(obj)
21 if isinstance(obj, bytes):
22 return obj.decode('utf-8', errors='replace')
23 return super().default(obj)
24
25def safe_json_dumps(obj):
26 """JSON dumps with safe encoding for SSE events."""
27 return json.dumps(obj, cls=SafeJSONEncoder, ensure_ascii=False)

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected