Includes a type definition and some utility functions for working with arbitrary JSON data in Deno.
If you don't know the shape of some data, but you're sure that it will be JSON data (e.g. no functions, undefined
, NaN
or Infinity
), this type is safer than using any
.
Usage example:
import { JsonValue, isJsonValue, parseJson } from "https://gist.githubusercontent.com/crabmusket/660f58266d91c316db35bbcbeed221cb/raw/020eb47ad479c56cbd01c3de009d65f6a2c184b9/json.ts";
console.log(isJsonValue({foo: "bar"})); // true
console.log(isJsonValue(5)); // true
console.log(isJsonValue(new Number(5))); // false, not a POJSO
let result: JsonValue | Error = parseJson("blahblah"); // typesafe parse, in this case will be Error