# json-parse-ast

> Just another custom JSON AST parser, but this time with position/range types compatible with monaco's API

Latest version **1.0.8** (published 2021-02-25) · Apache-2.0 license · 0 weekly downloads

## Install

```sh
npm install json-parse-ast
pnpm add json-parse-ast
yarn add json-parse-ast
bun add json-parse-ast
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.8 |
| Published | 2021-02-25 |
| First published | 2021-02-25 |
| Weekly downloads | 0 |
| License | Apache-2.0 |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 30.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 5 |
| Author | Johannes Oehm |
| Maintainers | johannesoehm |
| Keywords | json, AST, parser, monaco |

## Links

- npm: https://www.npmjs.com/package/json-parse-ast
- Repository: https://github.com/JohannesOehm/json-parse-ast
- Homepage: https://github.com/JohannesOehm/json-parse-ast#readme
- Issues: https://github.com/JohannesOehm/json-parse-ast/issues
- npm.io page: https://npm.io/package/json-parse-ast

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 1.0.8 (latest) — 2021-02-25
- 1.0.7 — 2021-02-25
- 1.0.6 — 2021-02-25
- 1.0.5 — 2021-02-25
- 1.0.4 — 2021-02-25
- 1.0.3 — 2021-02-25
- 1.0.2 — 2021-02-25
- 1.0.1 — 2021-02-25
- 1.0.0 — 2021-02-25

## README

# json-parse-ast
JSON Tokenizer & AST (Abstract Syntax Tree) parser

## Background
I wrote my custom parser since [monaco's](https://microsoft.github.io/monaco-editor/) built-in JSON
library does not expose any syntax tree which can be used to check position in file and enhance 
autocomplete experience.

## Usage
Currently, only tokenizer is stable:
```javascript
var tokens = tokenize('{"json": "string"}'); //returns list of tokens
//tokens have the following attributes
// type: "inlinecomment"|"multilinecomment"|"whitespace"|"string"|"literal"|"number"|"punctuaction" 
// position: IRange compatible with monaco's IRange-interface
// raw: string 
// value: string Unescaped JSON string, parsed number, parsed literal (null, true, false)
```
## Tokenizer additional features
```javascript
var [path, willBeValue] = getPathInObject(tokenize('{"foo": {"bar": "'));
//returns path == ["foo", "bar"] and willBeValue == true
```

## Parser
```javascript
var ast = parseTokens(tokenize(testString));
//AST elements have
// type 
// position: IRange
// raw: string
// value?: string
// parent?: AST 
// children?: AST[]

//Find node at specified position
var node = findAtPosition(ast, {lineNumber: 4, column: 16})
```

## Usage with monaco
This libraries intended use is for enhancing experience with custom JSON formats. See example use-cases below:

### Enhancing autocomplete/IntelliSense
```javascript
monaco.languages.registerCompletionItemProvider('json', {
    provideCompletionItems: function(model, position) {
        let textUntilPosition = model.getValueInRange({startLineNumber: 1, startColumn: 1, endLineNumber: position.lineNumber, endColumn: position.column});
        let [jsonPath, willBeValue] = getPathInObject(tokenize(textUntilPosition));
        if (willBeValue && jsonPath[jsonPath.length-1] === "dependencies") {
             return { suggestions: listDependencies() };
        } else {
             return { suggestions: [] };
        }
    }
});
```

---
_Source: https://npm.io/package/json-parse-ast · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
