@infra-blocks/json
v0.1.2
Published
JSON related utilities.
Downloads
4
Readme
ts-json
JSON related utilities package. It offers thin wrappers over JSON.parse
and JSON.stringifiy
and convenient types.
import { Json, JsonObject, JsonArray, JsonPrimitive, json } from "@infra-blocks/json";
// Convenient types can be used anywhere useful.
const validJson: Json = { key: "value" };
// @ts-expect-error
const invalidJson: Json = { "undefined is not json": undefined };
// The module offers a thin wrapper that returns a type Json value when parse is used without revivers.
const result: Json = json.parse('"finally, JSON.parse does not return any anymore"');
// Type of anyResult is any, since we used a reviver.
const anyResult = json.parse('{"key": "value"}', () => { /* Insert code here */ });
// The module also offers a stringify function, although it is exactly the same as JSON.stringify. It's mostly
// for completeness.
const stringified = json.stringify(new Map()); // Tolerates non Json typed object, just like the original function.