MCPcopy
hub / github.com/codeaashu/claude-code / tryFormatJson

Function tryFormatJson

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

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls 2

jsonParseFunction · 0.85
jsonStringifyFunction · 0.85

Tested by

no test coverage detected