Skip to content

JSON AST 解析器

项目地址: https://github.com/vtrushin/json-to-ast

TIP

你问为什么这里有个 JSON AST parser 的文档? 因为我看不懂原作者的文档,打算强迫自己写文档来理解

安装

bash
npm install json-to-ast

使用

javaScript
const parse = require('json-to-ast');

let ast = parse('{"rawtext": [{"text":"喵呜~"}]}')
console.log(JSON.stringify(ast, null, 4));

输出

JSON
{
    "type": "Object",
    "children": [
        {
            "type": "Property",
            "key": {
                "type": "Identifier",
                "value": "rawtext",
                "raw": "\"rawtext\"",
                "loc": {
                    "start": {
                        "line": 1,
                        "column": 2,
                        "offset": 1
                    },
                    "end": {
                        "line": 1,
                        "column": 11,
                        "offset": 10
                    },
                    "source": "data.json"
                }
            },
            "value": {
                "type": "Array",
                "children": [
                    {
                        "type": "Object",
                        "children": [
                            {
                                "type": "Property",
                                "key": {
                                    "type": "Identifier",
                                    "value": "text",
                                    "raw": "\"text\"",
                                    "loc": {
                                        "start": {
                                            "line": 1,
                                            "column": 15,
                                            "offset": 14
                                        },
                                        "end": {
                                            "line": 1,
                                            "column": 21,
                                            "offset": 20
                                        },
                                        "source": "data.json"
                                    }
                                },
                                "value": {
                                    "type": "Literal",
                                    "value": "喵呜~",
                                    "raw": "\"喵呜~\"",
                                    "loc": {
                                        "start": {
                                            "line": 1,
                                            "column": 22,
                                            "offset": 21
                                        },
                                        "end": {
                                            "line": 1,
                                            "column": 27,
                                            "offset": 26
                                        },
                                        "source": "data.json"
                                    }
                                },
                                "loc": {
                                    "start": {
                                        "line": 1,
                                        "column": 15,
                                        "offset": 14
                                    },
                                    "end": {
                                        "line": 1,
                                        "column": 27,
                                        "offset": 26
                                    },
                                    "source": "data.json"
                                }
                            }
                        ],
                        "loc": {
                            "start": {
                                "line": 1,
                                "column": 14,
                                "offset": 13
                            },
                            "end": {
                                "line": 1,
                                "column": 28,
                                "offset": 27
                            },
                            "source": "data.json"
                        }
                    }
                ],
                "loc": {
                    "start": {
                        "line": 1,
                        "column": 13,
                        "offset": 12
                    },
                    "end": {
                        "line": 1,
                        "column": 29,
                        "offset": 28
                    },
                    "source": "data.json"
                }
            },
            "loc": {
                "start": {
                    "line": 1,
                    "column": 2,
                    "offset": 1
                },
                "end": {
                    "line": 1,
                    "column": 29,
                    "offset": 28
                },
                "source": "data.json"
            }
        }
    ],
    "loc": {
        "start": {
            "line": 1,
            "column": 1,
            "offset": 0
        },
        "end": {
            "line": 1,
            "column": 30,
            "offset": 29
        },
        "source": "data.json"
    }
}

AST 节点类型

对象(Object){...}

表示 JSON 对象

JavaScript
{
    type: 'Object', // 节点类型
    children: Property[], // 子节点
    loc: { ... } // 位置信息
}

键值对(Property)"key": value

表示 JSON 对象中的属性(我喜欢叫它键值对)

JavaScript
{
    type: 'Property', // 节点类型
    key: Identifier, // 键
    value: { ... } | [ ... ] | Literal, // 值
    loc: { ... } // 位置信息
}

数组(Array)[...]

表示 JSON 数组

JavaScript
{
    type: 'Array', // 节点类型
    children: { ... } | [ ... ] | Literal, // 子节点
    loc: { ... } // 位置信息
}

标识符-键(Identifier) "key"

键值对Property中的键,也就是key对象

JavaScript
{
    type: 'Identifier', // 节点类型
    value: 'key', // 键的文本
    raw: '"key"', // 键的原始文本
    loc?: { ... } // 位置信息
}

字面量-值(Literal) 123 "text" true null

值,这里是字面量,也就是 数字、文本、布尔值、null

JavaScript
{
    type: 'Literal', // 节点类型
    value: " ... " | 123 | true/false | null, // 值
    raw: '"value"', // 值的原始文本
    loc?: { ... } // 位置信息
}