@w5s/coreNamespacesJSONOn this pageJSON A collection of functions to encode/decode JSON. Instead of throwing errors like globalThis.JSON, functions returns Result@exampleimport { 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 FunctionsparsestringifyFunctions parseparse(anyString: string): Result<JSONValue, SyntaxError>Parse using JSON.parse() and return a Result.@exampleconst 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'))stringifystringify(anyValue: unknown): Result<string, TypeError>Convert to string using JSON.stringify() and return a Result@exampleconst valid = { a: true };JSON.stringify(valid); // Result.Ok('{"a":true}')const circular = { get ref() { return this; },};JSON.stringify(circular);// Result.Error(new TypeError(...));
A collection of functions to encode/decode JSON. Instead of throwing errors like
globalThis.JSON
, functions returnsResult