MCPcopy Create free account
hub / github.com/su-kaka/gcli2api / _parse_json_objects

Function _parse_json_objects

src/api/vertex.py:418–460  ·  view source on GitHub ↗

从上游响应文本中花括号配对扫描提取所有 JSON 对象。 yield (dict, end_pos) — end_pos 是该对象在 raw_text 中结束后的位置。

(raw_text: str)

Source from the content-addressed store, hash-verified

416
417
418def _parse_json_objects(raw_text: str):
419 """
420 从上游响应文本中花括号配对扫描提取所有 JSON 对象。
421 yield (dict, end_pos) — end_pos 是该对象在 raw_text 中结束后的位置。
422 """
423 i = 0
424 while i < len(raw_text):
425 start = raw_text.find("{", i)
426 if start == -1:
427 break
428 depth = 0
429 in_string = False
430 escape = False
431 j = start
432 while j < len(raw_text):
433 ch = raw_text[j]
434 if escape:
435 escape = False
436 j += 1
437 continue
438 if ch == "\\":
439 escape = True
440 j += 1
441 continue
442 if ch == '"':
443 in_string = not in_string
444 elif not in_string:
445 if ch == "{":
446 depth += 1
447 elif ch == "}":
448 depth -= 1
449 if depth == 0:
450 json_str = raw_text[start:j + 1]
451 try:
452 obj = json.loads(json_str)
453 yield obj, j + 1
454 except json.JSONDecodeError:
455 pass
456 i = j + 1
457 break
458 j += 1
459 else:
460 break
461
462
463def _process_object(obj: Dict[str, Any]) -> Tuple[Optional[Dict[str, Any]], Optional[str], bool]:

Callers 2

stream_requestFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected