PHP code example of jojo1981 / json-ast-builder

1. Go to this page and download the library: Download jojo1981/json-ast-builder library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

jojo1981 / json-ast-builder example snippets




ojo1981\JsonAstBuilder\Generator;
use Jojo1981\JsonAstBuilder\Lexer\Lexer;
use Jojo1981\JsonAstBuilder\Parser;

// setup lexer and parser
$parser = new Parser(new Lexer());
$parser->setInput(\file_get_contents('data.json'));

// build AST
$ast = $parser->parse();

// You can use the generator to generate multiple things
$generator = new Generator();

// default generate json string options
$generateJsonStringOptions = [
    'useTabs' => false,
    'pretty' => true,
    'indentSize' => 2,
    'spacesBeforeColon' => 0,
    'spacesAfterColon' => 1,
    'lineSeparator' => PHP_EOL
];

// options can be omitted
$jsonString = $generator->generateJsonString($ast, $generateJsonStringOptions);

// default generate json string options
$generateDataOptions = [
    'assoc' => false
];

// options can be omitted
$data = $generator->generateData($ast, $generateDataOptions);

$plantUmlString = $generator->generatePlantUmlData($ast);
\file_put_contents('test-output.puml', $plantUmlString);

$statistics = $generator->getStatistics($ast);