1JSON representation
2===================
3
4Nodes (and comments) implement the `JsonSerializable` interface. As such, it is possible to JSON
5encode the AST directly using `json_encode()`:
6
7```php
8<?php
9
10use PhpParser\ParserFactory;
11
12$code = <<<'CODE'
13<?php
14
15/** @param string $msg */
16function printLine($msg) {
17    echo $msg, "\n";
18}
19CODE;
20
21$parser = (new ParserFactory())->createForHostVersion();
22
23try {
24    $stmts = $parser->parse($code);
25
26    echo json_encode($stmts, JSON_PRETTY_PRINT), "\n";
27} catch (PhpParser\Error $e) {
28    echo 'Parse Error: ', $e->getMessage();
29}
30```
31
32This will result in the following output (which includes attributes):
33
34```json
35[
36    {
37        "nodeType": "Stmt_Function",
38        "attributes": {
39            "startLine": 4,
40            "comments": [
41                {
42                    "nodeType": "Comment_Doc",
43                    "text": "\/** @param string $msg *\/",
44                    "line": 3,
45                    "filePos": 7,
46                    "tokenPos": 2,
47                    "endLine": 3,
48                    "endFilePos": 31,
49                    "endTokenPos": 2
50                }
51            ],
52            "endLine": 6
53        },
54        "byRef": false,
55        "name": {
56            "nodeType": "Identifier",
57            "attributes": {
58                "startLine": 4,
59                "endLine": 4
60            },
61            "name": "printLine"
62        },
63        "params": [
64            {
65                "nodeType": "Param",
66                "attributes": {
67                    "startLine": 4,
68                    "endLine": 4
69                },
70                "type": null,
71                "byRef": false,
72                "variadic": false,
73                "var": {
74                    "nodeType": "Expr_Variable",
75                    "attributes": {
76                        "startLine": 4,
77                        "endLine": 4
78                    },
79                    "name": "msg"
80                },
81                "default": null,
82                "flags": 0,
83                "attrGroups": []
84            }
85        ],
86        "returnType": null,
87        "stmts": [
88            {
89                "nodeType": "Stmt_Echo",
90                "attributes": {
91                    "startLine": 5,
92                    "endLine": 5
93                },
94                "exprs": [
95                    {
96                        "nodeType": "Expr_Variable",
97                        "attributes": {
98                            "startLine": 5,
99                            "endLine": 5
100                        },
101                        "name": "msg"
102                    },
103                    {
104                        "nodeType": "Scalar_String",
105                        "attributes": {
106                            "startLine": 5,
107                            "endLine": 5,
108                            "kind": 2,
109                            "rawValue": "\"\\n\""
110                        },
111                        "value": "\n"
112                    }
113                ]
114            }
115        ],
116        "attrGroups": [],
117        "namespacedName": null
118    }
119]
120```
121
122The JSON representation may be converted back into an AST using the `JsonDecoder`:
123
124```php
125<?php
126
127$jsonDecoder = new PhpParser\JsonDecoder();
128$ast = $jsonDecoder->decode($json);
129```
130
131Note that not all ASTs can be represented using JSON. In particular:
132
133 * JSON only supports UTF-8 strings.
134 * JSON does not support non-finite floating-point numbers. This can occur if the original source
135   code contains non-representable floating-pointing literals such as `1e1000`.
136
137If the node tree is not representable in JSON, the initial `json_encode()` call will fail.
138
139From the command line, a JSON dump can be obtained using `vendor/bin/php-parse -j file.php`.
140