OvisOCR3 / structured_json.py
xxyyy123's picture
Add OvisOCR3 ZeroGPU demo
0b00897
Raw History Blame Contribute Delete
695 Bytes
"""Strict JSON decoding without silent key loss or non-JSON numeric values."""
import json
import math
def _object(pairs):
result = {}
for key, value in pairs:
if key in result:
raise ValueError(f"Duplicate JSON key: {key!r}.")
result[key] = value
return result
def _constant(value):
raise ValueError(f"{value} is not a valid JSON value.")
def _float(value):
number = float(value)
if not math.isfinite(number):
raise ValueError("JSON number exceeds the supported finite range.")
return number
def strict_json_loads(text):
return json.loads(text, object_pairs_hook=_object, parse_constant=_constant, parse_float=_float)