Skip to main content

JSONUtils

@webda/utils


Variable: JSONUtils

const JSONUtils: object

Defined in: packages/utils/src/serializers.ts:482

Simple JSON utils

Type Declaration​

duplicate​

duplicate: (value) => any

Deep-clone an object via JSON round-trip serialization.

Parameters​

value​

any

The object to clone.

Returns​

any

A deep clone of the object.

loadFile​

loadFile: (filename) => any

Load a JSON file. Shorthand for FileUtils.load(filename, "json").

Parameters​

filename​

string

Path to the JSON file to load.

Returns​

any

The parsed object.

parse​

parse: (value) => any

Parse a JSON data

Parameters​

value​

any

to parse

Returns​

any

object parsed

safeStringify​

safeStringify: (value, replacer, space) => string

Stringify a value, handling circular references by omitting duplicate object references.

Parameters​

value​

any

The value to serialize.

replacer?​

(key, value) => any

Optional replacer function applied to each key/value pair.

space?​

string | number

Indentation: number of spaces or a string (default 2).

Returns​

string

The JSON string.

saveFile​

saveFile: (object, filename) => void

Save an object as a JSON file. Shorthand for FileUtils.save(object, filename, "json").

Parameters​

object​

any

The object to serialize.

filename​

string

Destination file path.

Returns​

void

void

Example​

JSONUtils.saveFile({ hello: "world" }, "test.json");

sortObject​

sortObject: (unordered, transformer) => any

Return a new object with the same entries as unordered but with keys sorted alphabetically. An optional transformer can filter or transform each value (returning a falsy value omits the key).

Parameters​

unordered​

any

The source object whose keys should be sorted.

transformer?​

(obj) => any

Optional value transformer; return falsy to omit the key (default: identity).

Returns​

any

A new object with sorted keys.

stringify​

stringify: (value, replacer, space, publicAudience) => string

Stringify a value similarly to JSON.stringify, with automatic fallback to safeStringify for circular structures.

When publicAudience is true, keys starting with __ are omitted.

Parameters​

value​

any

The value to serialize.

replacer?​

(key, value) => any

Optional replacer function applied to each key/value pair.

space?​

string | number

Indentation: number of spaces or a string (default 2).

publicAudience?​

boolean = false

When true, strip private __ prefixed keys (default false).

Returns​

string

The JSON string.