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

ts-jsonparse

v0.1.5

Published

JSON parser written in Typescript

Downloads

11

Readme

ts-jsonparse

Overview

I decided one afternoon to write a JSON parser to improve my Typescript skills and reinforce what I learned from Ruslan Spivak's excellent blog on Let's Build a Simple Interpreter. This is a Typescript implementation of a simple JSON parser. It does not handle exponention numbers or backslashes '' in strings.

I used the JSON Interchange Standard - 2nd addition as a reference for the implementation

Installation

npm i ts-jsonparse

To Use

import { Parser } from 'ts-jsonparse';

const parser = new Parser(text);
const obj = parser.parse();

Background

The JSON parser is divided into 3 parts:

  • Lexer - tokenizes the JSON string
  • Parser - Builds Abstract Syntax Tree
  • JSONBuilder - Walks the AST and builds a Javascript representation of the JSON.

Lexer

The job of the Lexer is to break the parts of the string into tokens. For JSON we look for the following tokens.

export enum eTokens {
  BEGIN_ARRAY = '[', // left square bracket
  BEGIN_OBJECT = '{', // left curly bracket
  END_ARRAY = ']', // right square bracket
  END_OBJECT = '}', // right curly bracket
  COLON = ':',
  COMMA = ',',
  TRUE = 'TRUE',
  FALSE = 'FALSE',
  NULL = 'NULL',
  NUMBER = 'NUMBER',
  STRING = 'STRING',
  EOF = 'EOF',
}

Parser

The goal of the parser is to find structure in the stream of tokens. I choose to implement an Abstract Syntax Tree (AST) to represent the JSON string. I choose for the AST to include 4 Nodes:

jObject - to represent the object
jArray - to represent the array
jNameValue - to represent a name - value pair
jPrimative - to represent a number, string, true, false, or null

The Parser implements the following JSON Grammer to build the AST.

value : object
| array
| NUMBER
| STRING
| TRUE
| FALSE
| NULL

object : BEGIN_OBJECT name_value_list END_OBJECT

array : BEGIN_ARRAY value_list END_ARRAY

name_value_list: name_value
| name_value COMMA name_value_list

name_value: string COLON value

value_list: value
| value COMMA value_list

JSONBuilder

The JSONBuilder walks the AST and builds the Javascript represntation of the JSON string.