1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | class JsonSerializer {
|
4 | constructor(_writer, _indentDelta = 2) {
|
5 | this._writer = _writer;
|
6 | this._indentDelta = _indentDelta;
|
7 | this._state = [];
|
8 | }
|
9 | _willOutputValue() {
|
10 | if (this._state.length > 0) {
|
11 | const top = this._top();
|
12 | const wasEmpty = top.empty;
|
13 | top.empty = false;
|
14 | if (!wasEmpty && !top.property) {
|
15 | this._writer(',');
|
16 | }
|
17 | if (!top.property) {
|
18 | this._indent();
|
19 | }
|
20 | }
|
21 | }
|
22 | _top() {
|
23 | return this._state[this._state.length - 1] || {};
|
24 | }
|
25 | _indent() {
|
26 | if (this._indentDelta == 0) {
|
27 | return;
|
28 | }
|
29 | let str = '\n';
|
30 | let i = this._state.length * this._indentDelta;
|
31 | while (i--) {
|
32 | str += ' ';
|
33 | }
|
34 | this._writer(str);
|
35 | }
|
36 | start() { }
|
37 | end() {
|
38 | if (this._indentDelta) {
|
39 | this._writer('\n');
|
40 | }
|
41 | }
|
42 | object(node) {
|
43 | if (node.defined == false) {
|
44 | return;
|
45 | }
|
46 | this._willOutputValue();
|
47 | this._writer('{');
|
48 | this._state.push({ empty: true, type: 'object' });
|
49 | for (const key of Object.keys(node.children)) {
|
50 | this.property(node.children[key]);
|
51 | }
|
52 |
|
53 | if (!node.frozen) {
|
54 | for (const key of Object.keys(node.value)) {
|
55 | if (key in node.children) {
|
56 | continue;
|
57 | }
|
58 | this._willOutputValue();
|
59 | this._writer(JSON.stringify(key));
|
60 | this._writer(': ');
|
61 | this._writer(JSON.stringify(node.value[key]));
|
62 | }
|
63 | }
|
64 | this._state.pop();
|
65 | if (!this._top().empty) {
|
66 | this._indent();
|
67 | }
|
68 | this._writer('}');
|
69 | }
|
70 | property(node) {
|
71 | if (node.defined == false) {
|
72 | return;
|
73 | }
|
74 | this._willOutputValue();
|
75 | this._writer(JSON.stringify(node.name));
|
76 | this._writer(': ');
|
77 | this._top().property = true;
|
78 | node.serialize(this);
|
79 | this._top().property = false;
|
80 | }
|
81 | array(node) {
|
82 | if (node.defined == false) {
|
83 | return;
|
84 | }
|
85 | this._willOutputValue();
|
86 | if (node.items.length === 0) {
|
87 | this._writer('[]');
|
88 | return;
|
89 | }
|
90 | this._writer('[');
|
91 | this._state.push({ empty: true, type: 'array' });
|
92 | for (let i = 0; i < node.items.length; i++) {
|
93 | node.items[i].serialize(this);
|
94 | }
|
95 | this._state.pop();
|
96 | if (!this._top().empty) {
|
97 | this._indent();
|
98 | }
|
99 | this._writer(']');
|
100 | }
|
101 | outputOneOf(node) {
|
102 | this.outputValue(node);
|
103 | }
|
104 | outputEnum(node) {
|
105 | this.outputValue(node);
|
106 | }
|
107 | outputValue(node) {
|
108 | this._willOutputValue();
|
109 | this._writer(JSON.stringify(node.value, null, this._indentDelta));
|
110 | }
|
111 | outputString(node) {
|
112 | this._willOutputValue();
|
113 | this._writer(JSON.stringify(node.value));
|
114 | }
|
115 | outputNumber(node) {
|
116 | this._willOutputValue();
|
117 | this._writer(JSON.stringify(node.value));
|
118 | }
|
119 | outputInteger(node) {
|
120 | this._willOutputValue();
|
121 | this._writer(JSON.stringify(node.value));
|
122 | }
|
123 | outputBoolean(node) {
|
124 | this._willOutputValue();
|
125 | this._writer(JSON.stringify(node.value));
|
126 | }
|
127 | }
|
128 | exports.JsonSerializer = JsonSerializer;
|
129 |
|
\ | No newline at end of file |