Dex TypeScript SDK 1.29.0
    Preparing search index...

    Function jsonCodec

    • Creates a JSON-wire codec.

      Call jsonCodec() with no arguments to encode with JSON.stringify and decode with JSON.parse without structural validation. That identity codec is the default when a Step or RPC omits an explicit codec. Pass decode when the parsed JSON must be validated or converted, such as rebuilding a Date. JSON.stringify semantics apply; undefined, cycles, and unsupported bigint values fail during encoding.

      Type Parameters

      • T

        Application value type.

      Parameters

      • options: {
            decode?: (value: unknown) => T;
            encode?: (value: T) => unknown;
            typeName?: string;
        } = {}

        Optional type name and JSON conversion hooks.

        • Optional Readonlydecode?: (value: unknown) => T

          Validates and converts parsed JSON-compatible data; identity by default.

        • Optional Readonlyencode?: (value: T) => unknown

          Converts an application value to JSON-compatible data; identity by default.

        • Optional ReadonlytypeName?: string

          Stable type name used in mapping errors; defaults to json.

      Returns Codec<T>

      A JSON-wire codec for T.

      const order = jsonCodec<Order>();
      const created = jsonCodec<Date>({
      typeName: "Date",
      decode: (value) => new Date(String(value)),
      encode: (value) => value.toISOString(),
      });

      TypeError when encoding produces undefined.