项目作者: Nazar910

项目描述 :
Descent recursive json parser
高级语言: JavaScript
项目地址: git://github.com/Nazar910/json_parser.git
创建时间: 2018-07-13T20:35:20Z
项目社区:https://github.com/Nazar910/json_parser

开源协议:MIT License

下载


Json parser Build Status

Descent recursive json parser (created for educational reasons, do not use it in production)

Getting started

In order to play with json_parser:

  • install Node.js (click here to see a way to install it on your system)
  • clone json_parser with
    1. git clone git@github.com:Nazar910/json_parser.git
  • cd into location where you cloned json_parser
    1. cd ~/Documents/examples/json_parser
  • install dependencies
    1. npm install
  • run tests (optional)
    1. npm test
  • run following command, where flag f points to json file you need to parse
    1. node src/runner.js -f /tmp/example.json
    Alt screen

Also you can require it and use in your node.js code (I don’t know for what reason but you can)

  1. const parse = require('./json_parser/src/index');
  2. console.log(parse('{"foo":"bar"}'));

Details

Inspired by a series of articles about interpeters at https://ruslanspivak.com (click to see the first article about interpeters)

Json parser consists of two main parts: Lexer and Parser. Both of them work with Tokens.

Token

Token is a atomic part of the grammar. In this example it may be type of NUMBER with value 5. Generally it is a just an object that has type and value. See implementation here.

Lexer

Lexer component is implemented as class (see src/lexer.js). It gets a json string as his input, and provides a stream of tokens: for each call of getNextToken() you’ll get new Token object, untill EOF.

Parser

Parser is a component which validates token order and creates object from json string on the fly. As input it accepts lexer object. Method parse is the main method that returns object parsed from json string. See Parser implementation here.