项目作者: vtrushin

项目描述 :
JSON AST parser
高级语言: JavaScript
项目地址: git://github.com/vtrushin/json-to-ast.git
创建时间: 2016-01-24T23:14:41Z
项目社区:https://github.com/vtrushin/json-to-ast

开源协议:MIT License

下载


JSON AST parser

NPM
NPM downloads
Requirements
Travis-CI

Install

  1. > npm install json-to-ast

Usage

  1. const parse = require('json-to-ast');
  2. const settings = {
  3. // Appends location information. Default is <true>
  4. loc: true,
  5. // Appends source information to node’s location. Default is <null>
  6. source: 'data.json'
  7. };
  8. parse('{"a": 1}', settings);

Output

  1. {
  2. type: 'Object',
  3. children: [
  4. {
  5. type: 'Property',
  6. key: {
  7. type: 'Identifier',
  8. value: 'a',
  9. raw: '"a"',
  10. loc: {
  11. start: { line: 1, column: 2, offset: 1 },
  12. end: { line: 1, column: 5, offset: 4 },
  13. source: 'data.json'
  14. }
  15. },
  16. value: {
  17. type: 'Literal',
  18. value: 1,
  19. raw: '1',
  20. loc: {
  21. start: { line: 1, column: 7, offset: 6 },
  22. end: { line: 1, column: 8, offset: 7 },
  23. source: 'data.json'
  24. }
  25. },
  26. loc: {
  27. start: { line: 1, column: 2, offset: 1 },
  28. end: { line: 1, column: 8, offset: 7 },
  29. source: 'data.json'
  30. }
  31. }
  32. ],
  33. loc: {
  34. start: { line: 1, column: 1, offset: 0 },
  35. end: { line: 1, column: 9, offset: 8 },
  36. source: 'data.json'
  37. }
  38. }

Node types

Object:

  1. {
  2. type: 'Object',
  3. children: <Property>[],
  4. loc?: Object
  5. }

Property:

  1. {
  2. type: 'Property',
  3. key: <Identifier>,
  4. value: Object | Array | <Literal>,
  5. loc?: Object
  6. }

Identifier:

  1. {
  2. type: 'Identifier',
  3. value: string,
  4. raw: string,
  5. loc?: Object
  6. }

Array:

  1. {
  2. type: 'Array',
  3. children: (Object | Array | <Literal>)[],
  4. loc?: Object
  5. }

Literal:

  1. {
  2. type: 'Literal',
  3. value: string | number | boolean | null,
  4. raw: string,
  5. loc?: Object
  6. }

AST explorer

Try it online on astexplorer.net

License

MIT