mirror of
https://github.com/morluto/rea.git
synced 2026-12-06 14:59:05 +00:00
refactor(json): reuse the shape inference UTF-8 decoder (#17360)
This commit is contained in:
1 parent
a80b6b79a9
commit
865ab5819a
1 file changed
+4
-10
+4
-10
@@ -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;
|
||||
|
||||
Reference in new issue
Block a user