aywan/json-ast

JSON抽象语法树解析器

0.1.2 2023-03-30 21:02 UTC

This package is auto-updated.

Last update: 2024-08-30 01:32:41 UTC


README

JSON抽象语法树解析器

示例

<?php
declare(strict_types=1);

use \Aywan\JsonAst\JsonAstParser;

$json = <<<JSON
{
    "title": "hello world!",
    "content": [
        {"type": "h1", "value": "hello"},
        {"type": "size", "value": 123}
    ],
    "props": {"width": 85.33}
}
JSON;

$ast = (new \Aywan\JsonAst\JsonAstParser())->parse();
$hello = $ast->getRoot()
    ->getProperty('content')[0]
    ->getProperty('value')
    ->getPhpValue()
;
echo $hello; // hello

近似令牌列表

{, "title", :, "hello world!", ',', "content", :, [, 
{, "type", :, "h1", ',', "value", :, "hello", }, ',',
{, "type", :, "size", ',', "value", :, 123, }, 
], ',', "props", :, {, "width", :, 85.33, }, }

近似树

Object(
    null,
    [
        Scalar("title", ["hello world!"]),
        Array(
            "content",
            [
                Object(
                    null,
                    [
                        Scalar("type", ["h1"]),
                        Scalar("value", ["hello"]),
                    ],
                ),
                Object(
                    null,
                    [
                        Scalar("size", ["h1"]),
                        Scalar("value", [123]),
                    ],
                )
            ]
        ),
        Object(
            "props",
            [
                Scalar("width", [85.33]),
            ],
        ),
    ], 
)

待办事项

  • 选择器,查询,json-path
  • 优化内存使用
  • 更多测试
  • 自动化