Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

json-cst

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

json-cst

Parse JSON into CST (Concrete Syntax Tree)

  • 1.2.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

npm version downloads build status coverage status Language grade: JavaScript

json-cst

This package parses a JSON into CST (Concrete Syntax Tree), similar to an AST but more low-level and with ties to the lexer tokens. It uses json-lexer to parse the file into tokens. The speed is practically the same as json-to-ast (it's ~10% faster than json-to-ast), but it's far smaller (even including json-lexer). Pure package is 7x smaller, install size 12x smaller, bundling it makes it 6x smaller according to bundlephobia (json-to-ast vs json-cst).

It comes with TypeScript typings.

Install

npm i json-cst or yarn add json-cst

This is a pure ESM package, and requires Node.js >=14.13.1

Simple usage

Exports

The package exports parse(json: string, options: ParseCstOptions): CstNode.

options is an optional object which can contain includeValueTokens: true to include the value tokens in the result, meaning, for objects and arrays, they will include the slice of tokens for the beginning and end of the object/array.

Definition

The tokens are parsed into a hierarchy of nodes, each with a "kind" property:

type CstKindLiteral = 'literal'; // null, true, false
type CstKindNumber = 'number';
type CstKindString = 'string';
type CstKindObjectPropertyColon = 'object-property-colon';
type CstKindObjectProperty = 'object-property';
type CstKindObject = 'object';
type CstKindArrayElement = 'array-element';
type CstKindArray = 'array';

And the CstNode returned by parse() is a CstValueNode, i.e. one of:

  • CstNodeLiteral
  • CstNodeNumber
  • CstNodeString
  • CstNodeObject
  • CstNodeArray

Other nodes are:

  • CstNodeObjectProperty
  • CstNodeObjectPropertyColon
  • CstNodeArrayElement

Each token contain a { range: CstTokenRange } where

interface CstTokenRange {
    start: number;
    end: number;
}

Each of the primitive tokens CstNodeLiteral, CstNodeNumber and CstNodeString contain { token: Token } being the raw token from json-lexer.

Object and array tokens CstNodeObject and CstNodeArray contain a property children being an array of either CstNodeObjectProperty or CstNodeArrayElement.

A CstNodeObjectProperty has a keyToken property being the lexer token for the property name, and a valueNode being a CstNode. A CstNodeArrayElement also has a valueNode.

See types.ts for exact typings.

Keywords

FAQs

Package last updated on 02 May 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc