Skip to main content
Module

x/fun/mod.ts>decoder.json

A collection of algebraic data types, lenses, and schemables based on a light weight higher kinded type implementation. Written for deno.
Latest
function decoder.json
import { decoder } from "https://deno.land/x/fun@v2.0.0/mod.ts";
const { json } = decoder;

A Decoder combinator that will check that a value is a string and then attempt to parse it as JSON.

Examples

Example 1

import * as D from "./decoder.ts";
import { pipe } from "./fn.ts";

const person = D.struct({
  name: D.string,
  age: D.number,
});
const json = D.json(person);

const result1 = json(null); // Left(DecodeError)
const result2 = json(""); // Left(DecodeError)
const result3 = json('{"name":"Brandon","age":37}');
// Right({ name: "Brandon", age: 37 })

Parameters

decoder: Decoder<unknown, A>

Returns

Decoder<unknown, A>