Aller au contenu principal

JSON

A collection of functions to encode/decode JSON. Instead of throwing errors like globalThis.JSON, functions returns Result

@example
import { JSON } from '@w5s/core';

const object = { a: true };
const encoded = JSON.stringify(object); // Result.Ok('{"a":true}')
if (Result.isOk(encoded)) {
const decoded = JSON.parse(encoded); // Result.Ok({ a: true })
}

Index

Functions

parse

  • Parse using JSON.parse() and return a Result.

    @example
    const valid = '{ "a": true }';
    JSON.parse(valid); // Result.Ok({ a: true })

    const invalid = '{ "a": }';
    JSON.parse(invalid); // Result.Error(new SyntaxError('Unexpected token } in JSON at position 7'))

stringify

  • stringify(anyValue: unknown): Result<string, TypeError>
  • Convert to string using JSON.stringify() and return a Result

    @example
    const valid = { a: true };
    JSON.stringify(valid); // Result.Ok('{"a":true}')

    const circular = {
    get ref() {
    return this;
    },
    };
    JSON.stringify(circular);// Result.Error(new TypeError(...));
Page Options