Transformation<unknown, string, never, never>Decodes a JSON string with JSON.parse and encodes a value with
JSON.stringify.
When to use
Use when you need a schema transformation to decode JSON stored or transmitted as a string, usually before composing with another schema that validates the parsed structure.
Details
Decode fails with InvalidValue for invalid JSON, and encode can fail with
InvalidValue when JSON.stringify cannot serialize the value.
Example (Parsing JSON)
import { Schema, SchemaTransformation } from "effect"
const schema = Schema.String.pipe(
Schema.decodeTo(Schema.Unknown, SchemaTransformation.fromJsonString)
)export const const fromJsonString: Transformation<
unknown,
string,
never,
never
>
const fromJsonString: {
_tag: 'Transformation';
decode: SchemaGetter.Getter<T, E, RD>;
encode: SchemaGetter.Getter<E, T, RE>;
flip: () => Transformation<string, unknown, never, never>;
compose: (other: Transformation<T2, unknown, RD2, RE2>) => Transformation<T2, string, RD2, RE2>;
}
Decodes a JSON string with JSON.parse and encodes a value with
JSON.stringify.
When to use
Use when you need a schema transformation to decode JSON stored or
transmitted as a string, usually before composing with another schema that
validates the parsed structure.
Details
Decode fails with InvalidValue for invalid JSON, and encode can fail with
InvalidValue when JSON.stringify cannot serialize the value.
Example (Parsing JSON)
import { Schema, SchemaTransformation } from "effect"
const schema = Schema.String.pipe(
Schema.decodeTo(Schema.Unknown, SchemaTransformation.fromJsonString)
)
fromJsonString = new constructor Transformation<unknown, string, never, never>(decode: SchemaGetter.Getter<unknown, string, never>, encode: SchemaGetter.Getter<string, unknown, never>): Transformation<unknown, string, never, never>Represents a bidirectional transformation between a decoded type T and an encoded
type E, built from a pair of Getters.
When to use
Use when you need a schema transformation that defines how a schema converts
between two representations.
- You want to compose multiple transformations into a pipeline.
- You want to flip a transformation to swap decode/encode.
Details
This is the primary building block for Schema.decodeTo, Schema.encodeTo,
Schema.decode, Schema.encode, and Schema.link. Each direction is a
SchemaGetter.Getter that handles optionality, failure, and Effect services.
- Immutable —
flip() and compose() return new instances.
flip() swaps the decode and encode getters.
compose(other) chains: this.decode then other.decode for decoding,
other.encode then this.encode for encoding.
Example (Composing two transformations)
import { SchemaTransformation } from "effect"
const trimAndLower = SchemaTransformation.trim().compose(
SchemaTransformation.toLowerCase()
)
// decode: trim then lowercase
// encode: passthrough (both directions)
Transformation<unknown, string>(
import SchemaGetterSchemaGetter.function parseJson<string>(): SchemaGetter.Getter<MutableJson, string, never> (+1 overload)Parses a JSON string into a value.
When to use
Use when you need a schema getter to parse a present encoded JSON string
during decoding.
Details
- Skips
None inputs.
- Without
reviver: returns Schema.MutableJson (typed JSON).
- With
reviver: returns unknown (reviver may produce arbitrary values).
- On parse failure, fails with
SchemaIssue.InvalidValue containing the error message.
Example (Parsing JSON)
import { SchemaGetter } from "effect"
const parse = SchemaGetter.parseJson<string>()
// Getter<MutableJson, string>
parseJson(),
import SchemaGetterSchemaGetter.function stringifyJson(
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>
stringifyJson()
)