(input: unknown): unknownConverts a value to a JSON-serializable representation safely.
When to use
Use when you need a safe, JSON-serializable representation of a value without risking unhandled errors.
Details
This function attempts to extract JSON data from objects that implement the
toJSON method, recursively processes arrays, and handles errors gracefully.
For objects that don't have a toJSON method, it applies redaction to
protect sensitive information.
export const const toJson: (input: unknown) => unknownConverts a value to a JSON-serializable representation safely.
When to use
Use when you need a safe, JSON-serializable representation of a value
without risking unhandled errors.
Details
This function attempts to extract JSON data from objects that implement the
toJSON method, recursively processes arrays, and handles errors gracefully.
For objects that don't have a toJSON method, it applies redaction to
protect sensitive information.
toJson = (input: unknowninput: unknown): unknown => {
try {
if (
import PredicatePredicate.const hasProperty: <"toJSON">(self: unknown, property: "toJSON") => self is { [K in "toJSON"]: unknown; } (+1 overload)hasProperty(input: unknowninput, "toJSON") &&
import PredicatePredicate.function isFunction(
input: unknown
): input is Function
Checks whether a value is a function.
When to use
Use when you need a Predicate guard to narrow an unknown value to a
callable function.
Details
Uses typeof input === "function".
Example (Guarding functions)
import { Predicate } from "effect"
const data: unknown = () => 1
if (Predicate.isFunction(data)) {
console.log(data())
}
isFunction(input: {
toJSON: unknown
}
input["toJSON"]) &&
input: {
toJSON: unknown
}
input["toJSON"].Function.length: numberlength === 0
) {
return input: {
toJSON: unknown
}
input.toJSON: FunctiontoJSON()
} else if (var Array: ArrayConstructorArray.ArrayConstructor.isArray(arg: any): arg is any[]isArray(input: unknowninput)) {
return input: any[]input.Array<any>.map<unknown>(callbackfn: (value: any, index: number, array: any[]) => unknown, thisArg?: any): unknown[]Calls a defined callback function on each element of an array, and returns an array that contains the results.
map(const toJson: (input: unknown) => unknownConverts a value to a JSON-serializable representation safely.
When to use
Use when you need a safe, JSON-serializable representation of a value
without risking unhandled errors.
Details
This function attempts to extract JSON data from objects that implement the
toJSON method, recursively processes arrays, and handles errors gracefully.
For objects that don't have a toJSON method, it applies redaction to
protect sensitive information.
toJson)
}
} catch {
return "[toJSON threw]"
}
return 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(input: unknowninput)
}