(
input: unknown,
options?: { readonly space?: number | string | undefined }
): stringStringifies a value to JSON safely, silently dropping circular references.
When to use
Use when you need valid JSON output, unlike format, and the input may
contain circular references that should be silently omitted rather than
throwing a TypeError.
Details
Uses JSON.stringify internally with a replacer that tracks the current
object ancestry. Circular references are replaced with undefined, which
omits them from object output. Redactable values are automatically redacted
before serialization. Values not supported by JSON, such as BigInt,
Symbol, undefined, and functions, follow standard JSON.stringify
behavior. The space parameter controls indentation and defaults to 0.
Example (Formatting compact JSON)
import { Formatter } from "effect"
console.log(Formatter.formatJson({ name: "Alice", age: 30 }))
// {"name":"Alice","age":30}Example (Handling circular references)
import { Formatter } from "effect"
const obj: any = { name: "test" }
obj.self = obj
console.log(Formatter.formatJson(obj))
// {"name":"test"}Example (Pretty-printed JSON)
import { Formatter } from "effect"
console.log(Formatter.formatJson({ name: "Alice", age: 30 }, { space: 2 }))
// {
// "name": "Alice",
// "age": 30
// }export function function formatJson(
input: unknown,
options?: {
readonly space?: number | string | undefined
}
): string
Stringifies a value to JSON safely, silently dropping circular references.
When to use
Use when you need valid JSON output, unlike format, and the input may
contain circular references that should be silently omitted rather than
throwing a TypeError.
Details
Uses JSON.stringify internally with a replacer that tracks the current
object ancestry. Circular references are replaced with undefined, which
omits them from object output. Redactable values are automatically redacted
before serialization. Values not supported by JSON, such as BigInt,
Symbol, undefined, and functions, follow standard JSON.stringify
behavior. The space parameter controls indentation and defaults to 0.
Example (Formatting compact JSON)
import { Formatter } from "effect"
console.log(Formatter.formatJson({ name: "Alice", age: 30 }))
// {"name":"Alice","age":30}
Example (Handling circular references)
import { Formatter } from "effect"
const obj: any = { name: "test" }
obj.self = obj
console.log(Formatter.formatJson(obj))
// {"name":"test"}
Example (Pretty-printed JSON)
import { Formatter } from "effect"
console.log(Formatter.formatJson({ name: "Alice", age: 30 }, { space: 2 }))
// {
// "name": "Alice",
// "age": 30
// }
formatJson(input: unknowninput: unknown, options: | {
readonly space?: number | string | undefined
}
| undefined
options?: {
readonly space?: string | number | undefinedspace?: number | string | undefined
}): string {
const const ancestors: object[]ancestors: interface Array<T>Array<object> = []
return var JSON: JSONAn intrinsic object that provides functions to convert JavaScript values to and from the JavaScript Object Notation (JSON) format.
JSON.JSON.stringify(value: any, replacer?: (this: any, key: string, value: any) => any, space?: string | number): string (+1 overload)Converts a JavaScript value to a JavaScript Object Notation (JSON) string.
stringify(
input: unknowninput,
function(this: unknownthis: unknown, _key: string_key: string, value: unknownvalue: unknown) {
const const redacted: unknownredacted = function redact(u: unknown): unknownReturns a redacted value if it implements
Redactable
, otherwise returns it
unchanged.
When to use
Use as the general-purpose entry point for redaction when the input may
or may not implement the redaction protocol.
Details
This function calls
isRedactable
and, when it returns true,
delegates to
getRedacted
.
Gotchas
Redaction is not recursive. Nested redactable values inside the returned
object are not automatically redacted.
redact(value: unknownvalue)
if (typeof const redacted: unknownredacted !== "object" || const redacted: object | nullredacted === null) {
return const redacted: unknownredacted
}
while (const ancestors: object[]ancestors.Array<T>.length: numberGets or sets the length of the array. This is a number one higher than the highest index in the array.
length > 0 && const ancestors: object[]ancestors[const ancestors: object[]ancestors.Array<T>.length: numberGets or sets the length of the array. This is a number one higher than the highest index in the array.
length - 1] !== this) {
const ancestors: object[]ancestors.Array<object>.pop(): object | undefinedRemoves the last element from an array and returns it.
If the array is empty, undefined is returned and the array is not modified.
pop()
}
if (const ancestors: object[]ancestors.Array<object>.includes(searchElement: object, fromIndex?: number): booleanDetermines whether an array includes a certain element, returning true or false as appropriate.
includes(const redacted: objectredacted)) {
return var undefinedundefined // circular reference
}
const ancestors: object[]ancestors.Array<object>.push(...items: object[]): numberAppends new elements to the end of an array, and returns the new length of the array.
push(const redacted: objectredacted)
return const redacted: objectredacted
},
options: | {
readonly space?: number | string | undefined
}
| undefined
options?.space?: string | number | undefinedspace
)
}