MCPcopy Index your code
hub / github.com/claude-code-best/claude-code / tryFormatJson

Function tryFormatJson

src/components/shell/OutputLine.tsx:13–34  ·  view source on GitHub ↗
(line: string)

Source from the content-addressed store, hash-verified

11import { useExpandShellOutput } from './ExpandShellOutputContext.js';
12
13export function tryFormatJson(line: string): string {
14 try {
15 const parsed = jsonParse(line);
16 const stringified = jsonStringify(parsed);
17
18 // Check if precision was lost during JSON round-trip
19 // This happens when large integers exceed Number.MAX_SAFE_INTEGER
20 // We normalize both strings by removing whitespace and unnecessary
21 // escapes (\/ is valid but optional in JSON) for comparison
22 const normalizedOriginal = line.replace(/\\\//g, '/').replace(/\s+/g, '');
23 const normalizedStringified = stringified.replace(/\s+/g, '');
24
25 if (normalizedOriginal !== normalizedStringified) {
26 // Precision loss detected - return original line unformatted
27 return line;
28 }
29
30 return jsonStringify(parsed, null, 2);
31 } catch {
32 return line;
33 }
34}
35
36const MAX_JSON_FORMAT_LENGTH = 10_000;
37

Callers

nothing calls this directly

Calls 2

jsonParseFunction · 0.85
jsonStringifyFunction · 0.85

Tested by

no test coverage detected