Hyperlinkv0.8.0-beta.28

SchemaGetter

SchemaGetter.stringifyJsonfunctioneffect/SchemaGetter.ts:1040
(options?: StringifyJsonOptions): Getter<string, unknown>

Stringifies a present value using JSON.stringify.

When to use

Use when you need a schema getter to serialize a present decoded value to JSON text during encoding.

Details

  • Skips None inputs.
  • On thrown stringify failures, such as circular references, fails with SchemaIssue.InvalidValue.
  • Supports optional replacer and space options, matching JSON.stringify.
  • If JSON.stringify returns undefined, such as for undefined, functions, symbols, or a replacer that removes the root value, that undefined result is returned rather than converted into an Issue.

Example (Stringifying JSON)

import { SchemaGetter } from "effect"

const stringify = SchemaGetter.stringifyJson()
// Getter<string, unknown>
JSON gettersparseJson
export function stringifyJson(options?: StringifyJsonOptions): Getter<string, unknown> {
  return onSome((input) =>
    Effect.try({
      try: () => Option.some(JSON.stringify(input, options?.replacer, options?.space)),
      catch: (e) => new SchemaIssue.InvalidValue(Option.some(input), { message: globalThis.String(e) })
    })
  )
}
Referenced by 1 symbols