INNER CODE UNIT · Python
_parse_json_or_jsonp
simonlin1212/Vibe-Research · calc/cli.py:174
def _parse_json_or_jsonp(data: bytes):
"""严格解析:UTF-8 必须合法;纯 JSON 直接 loads;否则只接受 <标识符>(<JSON>);? 形式的 JSONP(尾随垃圾 / 多余花括号不猜)。"""
try:
text = data.decode("utf-8")
except UnicodeDecodeError as e:
raise ValueError(f"history_json.raw_ref 不是合法 UTF-8:{e}") from e
s = text.strip()
if s.startswith("{") or s.startswith("["):
return json.loads(s)
m = _JSONP_RE.match(s)
if not m:
raise ValueError("history_json.raw_ref 既不是 JSON 也不是 <callback>(<JSON>) 形式的 JSONP")
return json.loads(m.group(2))
def _load_history_json(spec: dict, run_dir: str | None) -> tuple[list, dict]:
"""从 <run_dir>/raw/ 的 JSON(或 JSONP:取第一个 '{' 到最后一个 '}')确定性加载一张 K 线表:
spec = {raw_ref, rows_path: "data.sz300308.qfqday", columns: {"date": 0, "open": 1, "close": 2, "high": 3, "low": 4} 或 {"date": "date", ...}, where?: {"tradestatus": "1"}}