@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
104 lines • 4.25 kB
JavaScript
import { isBigIntObject, isBooleanObject, isNumberObject, isStringObject } from "node:util/types";
import { jsonNormalValue } from "./jsonl.js";
/** JSON.stringify's value semantics, without serializing or parsing a record. */
export function normalizeJsonValue(input) {
const ancestors = new Set();
let changed = false;
const visit = (input, key) => {
let value = input;
if ((typeof value === "object" && value !== null) || typeof value === "function" || typeof value === "bigint") {
const toJSON = value.toJSON;
if (typeof toJSON === "function") {
value = toJSON.call(value, key);
changed = true;
}
}
if (typeof value === "object" && value !== null) {
if (isNumberObject(value)) {
value = +value;
changed = true;
}
else if (isStringObject(value)) {
value = String(value);
changed = true;
}
else if (isBooleanObject(value)) {
value = Boolean.prototype.valueOf.call(value);
changed = true;
}
else if (isBigIntObject(value)) {
throw new TypeError("Do not know how to serialize a BigInt");
}
}
if (typeof value === "bigint")
throw new TypeError("Do not know how to serialize a BigInt");
if (typeof value === "number") {
const normalized = Number.isFinite(value) ? (Object.is(value, -0) ? 0 : value) : null;
if (!Object.is(normalized, value))
changed = true;
return normalized;
}
if (typeof value === "undefined" || typeof value === "function" || typeof value === "symbol") {
changed = true;
return undefined;
}
if (typeof value !== "object" || value === null)
return value;
if (ancestors.has(value))
throw new TypeError("Converting circular structure to JSON");
ancestors.add(value);
try {
// A non-callable toJSON getter must not be read again by later serialization.
if ("toJSON" in value)
changed = true;
if (Array.isArray(value)) {
const array = [];
const length = value.length;
for (let index = 0; index < length; index++) {
const descriptor = Object.getOwnPropertyDescriptor(value, String(index));
if (!descriptor || descriptor.get)
changed = true;
array.push(visit(value[index], String(index)) ?? null);
}
return array;
}
const result = {};
const names = Object.keys(value);
if (Object.getPrototypeOf(value) !== Object.prototype || Reflect.ownKeys(value).length !== names.length)
changed = true;
for (const name of names) {
if (Object.getOwnPropertyDescriptor(value, name)?.get)
changed = true;
const child = visit(value[name], name);
if (child !== undefined)
Object.defineProperty(result, name, {
value: child,
enumerable: true,
configurable: true,
writable: true,
});
}
return result;
}
finally {
ancestors.delete(value);
}
};
let value;
try {
value = visit(input, "");
}
catch (error) {
// The recursive walk overflows the stack on a deep record; the explicit-stack walk has no depth limit (senpi#5702).
if (!(error instanceof RangeError))
throw error;
value = jsonNormalValue(input);
if (value === undefined)
throw new SyntaxError("JSON input has no representation");
return { value, changed: true };
}
if (value === undefined)
throw new SyntaxError("JSON input has no representation");
return { value, changed };
}
//# sourceMappingURL=json-normal-value.js.map