stringifyJson
stringifyJson(
value,options?):string
Defined in: stringify-json.ts:95
JSON.stringify pretty-printed and ending in exactly one newline — the shape
every package.json-style file on disk should have.
Pass the file’s current text as like to keep its own indent (tabs, 4
spaces, …) and line endings (\r\n) when rewriting it.
Parameters
Section titled “Parameters”unknown
options?
Section titled “options?”StringifyJsonOptions = {}
Returns
Section titled “Returns”string
Throws
Section titled “Throws”when value has no JSON representation (undefined, a
function, a symbol) — JSON.stringify would return undefined for it — or
when indent isn’t a non-negative integer or a string of spaces and tabs
(anything else would produce text no JSON parser accepts).
Example
Section titled “Example”import { stringifyJson } from "@theholocron/json-utils";
stringifyJson({ a: 1 }); // '{\n "a": 1\n}\n'
// rewrite a file without reformatting itconst original = await readFile("package.json", "utf8");const pkg = JSON.parse(original);pkg.version = "2.0.0";await writeFile("package.json", stringifyJson(pkg, { like: original }));