refactor(json): reuse the shape inference UTF-8 decoder (#17360)

This commit is contained in:
Vincent Koc
2026-12-06 10:35:29 +08:00
committed by GitHub
parent a80b6b79a9
commit 865ab5819a
+4 -10
View File
@@ -5,7 +5,7 @@ import { safeParseJson } from "./safeJson.js";
import { jsonStringByteLength } from "./jsonStringByteLength.js";
import { jsonValueByteLength } from "./jsonValueByteLength.js";
import { err, ok, type Result } from "./result.js";
import { strictUtf8Decoder } from "./strictUtf8.js";
import { decodeUtf8PreservingBomOrNull } from "./strictUtf8.js";
import { isRecord } from "../kernel/isRecord.js";
const jsonValueTypeSchema = z.enum([
@@ -82,15 +82,9 @@ export const inferJsonShape = (
throw new RangeError(
"JSON shape byte allowance must be a non-negative safe integer",
);
let text: string;
try {
text =
typeof source === "string"
? source
: strictUtf8Decoder("preserve").decode(source);
} catch {
return err({ kind: "invalid-json" });
}
const text =
typeof source === "string" ? source : decodeUtf8PreservingBomOrNull(source);
if (text === null) return err({ kind: "invalid-json" });
const parsed = safeParseJson(text);
if (!parsed.ok) return err({ kind: "invalid-json" });
const root = parsed.value;