npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

typesafe-json

v0.2.0

Published

An easy, typesafe way to handle JSON.

Downloads

35

Readme

typesafe-json

An easy, typesafe way to handle JSON.

Accessing JSON in TypeScript (or safely in JavaScript) is a nuisance. You need to check for undefined every step of the way and check the type is correct before you can use it.

This package is designed to reduce that boilerplate, making it more concise, readable, and reliable.

Note there have been some breaking changes since v0.1.0. See the changelog for details.

Example

const json = `{"a":[0,{"b":["c","d"]}]}`;

let c = TypedJSON.parse(json).get("a", 1, "b", 0).string();
if (c !== undefined) {
    // c is guaranteed to be a string
}

let e = TypedJSON.parse(json).get("z", 99, "x", 12).number();
if (e === undefined) {
    // No errors are thrown
}

Installation

Run npm i typesafe-json in your project.

Type information is included automatically. Don't install @types/typesafe-json; it is neither needed nor possible.

Use

// Importing in TypeScript
import { TypedJSON } from "typesafe-json";
// or in JavaScript
const TypedJSON = require("typesafe-json").TypedJSON;

// A TypedJSON object can be parsed from JSON,
const typedJSON1 = TypedJSON.parse(`{"a":[0,{"b":["c","d"]}]}`);
// or from any JavaScript value.
const typedJSON2 = new TypedJSON({a:[0,{b:["c","d"]}]});
const typedJSON3 = new TypedJSON(12);

// .get returns another TypedJSON object.
const typedJSON4 = typedJSON1.get("a", 1, "b"); // new TypedJSON(["c", "d"])
const typedJSON5 = typedJSON4.get(0); // new TypedJSON("c")

// The value of a TypedJSON object can be accessed like so.
// Accessors return `undefined` if the TypedJSON object
// does not contain the correct type.
const typedJSON = new TypedJSON("c");
const number = typedJSON.number(); // undefined
const string = typedJSON.string(); // "c"
const boolean = typedJSON.boolean(); // undefined
// .object and .array also exist, but are not recommended.
// Instead, use .get().

// Checks exists to check the type without getting the value.
const isNumber = typedJSON.isNumber() // false
const isString = typedJSON.isString() // true
const isBoolean = typedJSON.isBoolean() // false
const isArray = typedJSON.isArray() // false
const isObject = typedJSON.isObject() // false
const isNull = typedJSON.isNull() // false
const isUndefined = typedJSON.isUndefined() // false
// The type system is able to use the type information these return.

// .values() and .keys() return arrays.
// Both work for both arrays and objects.
// Otherwise they return an empty array.
const arrayJSON = new TypedJSON(["a", "b"]);
const objectJSON = new TypedJSON({ a: 1, b: 2});
const numberJSON = new TypedJSON(12);
arrayJSON.values(); // [new TypedJSON("a"), new TypedJSON("b")]
objectJSON.values(); // [new TypedJSON(1), new TypedJSON(2)]
numberJSON.values(); // []
arrayJSON.keys(); // [0, 1]
objectJSON.keys(); // ["a", "b"]
numberJSON.keys(); // []

// Invalid operations return either `undefined`,
// a TypedJSON containing `undefined`,
// or an empty array.
const invalidJSON = TypedJSON.parse("invalid json");
invalidJSON.isUndefined() // true
invalidJSON.get("a", 1, "b", 2).isUndefined() // true
invalidJSON.number() // undefined
invalidJSON.stringify() // undefined
invalidJSON.keys() // []

// .stringify() returns the JSON representation string
// of the TypedJSON object, or undefined if it cannot.
// .value is the underlying value of the TypedJSON object,
// and usually should not be used.

More detailed documentation can be found in documentation comments.

Links

Repository Issues Changelog npm