An intrinsic object that provides functions to convert JavaScript values to and from the JavaScript Object Notation (JSON) format.
JSON.
JSON.parse(text: string, reviver?: (this:any, key:string, value:any) => any): any
Converts a JavaScript Object Notation (JSON) string into an object.
@param ― text A valid JSON string.
@param ― reviver A function that transforms the results. This function is called for each member of the object.
If a member contains nested objects, the nested objects are transformed before the parent object is.
parse(
conststring:string
string));
Here, the last line will throw if the validation fails.
Transformations
Schema’s superpower is its ability to describe two-way transformations.
The type of a Schema has two type parameters: the input type and the output type.
1
type
typeSchema<Decoded, Encoded> = {}
Schema<
function (typeparameter) DecodedintypeSchema<Decoded, Encoded>
Decoded,
function (typeparameter) EncodedintypeSchema<Decoded, Encoded>
Encoded> = {};
We can then use such a schema to transform between the two types.
Consider the Schema.parseJson function, which takes a schema and returns a new schema which transforms between JSON and the schema’s type.
Combined with other schemas, which can serialize/deserialize types that are not JSON-compatible, we can build a complex schema capable of encoding/decoding any type.
This schema converts a string into a Date object using the new Date
constructor. It ensures that only valid date strings are accepted,
rejecting any strings that would result in an invalid date, such as new Date("Invalid Date").
The ParseJson combinator provides a method to convert JSON strings into the unknown type using the underlying
functionality of JSON.parse. It also utilizes JSON.stringify for encoding.
You can optionally provide a ParseJsonOptions to configure both JSON.parse and JSON.stringify executions.
Optionally, you can pass a schema Schema<A, I, R> to obtain an A type instead of unknown.
All of the examples so far on this page have been using the *Sync variants of the Schema functions. These all return synchronously, but throw if the validation fails, or a transformation is asynchronous.
To consume these apis as Effects, just drop the Sync suffix.
The ParseJson combinator provides a method to convert JSON strings into the unknown type using the underlying
functionality of JSON.parse. It also utilizes JSON.stringify for encoding.
You can optionally provide a ParseJsonOptions to configure both JSON.parse and JSON.stringify executions.
Optionally, you can pass a schema Schema<A, I, R> to obtain an A type instead of unknown.
Provides a way to write effectful code using generator functions, simplifying
control flow and error handling.
When to Use
Effect.gen allows you to write code that looks and behaves like synchronous
code, but it can handle asynchronous tasks, errors, and complex control flow
(like loops and conditions). It helps make asynchronous code more readable
and easier to manage.
The generator functions work similarly to async/await but with more
explicit control over the execution of effects. You can yield* values from
effects and return the final result at the end.