Skip to content

writeJsonFile

Serializes and writes JSON to a file.

Example

ts
import { 
SF
} from "@duplojs/server-utils";
const
result
= await
SF
.
writeJsonFile
("/tmp/config.json", {
enabled
: true });
// result: E.Ok | SF.FileSystemLeft await
SF
.
writeJsonFile
("/tmp/config.json", {
enabled
: true }, {
space
: 2 });

Syntax

typescript
function writeJsonFile(
  path: string | URL,
  data: unknown,
  params?: {
    space?: number;
  }
): Promise<FileSystemLeft | E.Ok>

Parameters

  • path : path of the JSON file.
  • data : data to serialize.
  • params.space : JSON indentation (optional).

Return value

  • E.Ok : if the write succeeds.
  • FileSystemLeft : if serialization or writing fails.

Notes

  • If JSON.stringify fails, the function returns a failure.

See also

Released under the MIT license.