This repository has been archived on 2023-01-14. You can view files and clone it, but cannot push or open issues or pull requests.
libsiaweb/dist/stringifyJSON.js

23 lines
614 B
JavaScript

import { addContextToErr } from "./err.js";
import { objAsString } from "./objAsString.js";
// jsonStringify is a replacement for JSON.stringify that returns an error
// rather than throwing.
function jsonStringify(obj) {
try {
const str = JSON.stringify(obj, (_, v) => {
if (typeof v === "bigint") {
return Number(v);
}
return v;
});
return [str, null];
}
catch (err) {
return [
"",
addContextToErr(objAsString(err), "unable to stringify object"),
];
}
}
export { jsonStringify };