-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathJsonData.js
More file actions
197 lines (165 loc) · 4.91 KB
/
JsonData.js
File metadata and controls
197 lines (165 loc) · 4.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import { JsonType } from './JsonType.js';
class JsonData {
constructor() {
this.inst_array = null;
this.inst_boolean = false;
this.inst_double = 0;
this.inst_object = null;
this.inst_string = null;
this.type = JsonType.none;
}
get Count() {
if (this.type === JsonType.array) {
return this.inst_array.length;
}
if (this.type === JsonType.object) {
return Object.keys(this.inst_object).length;
}
return 0;
}
get IsArray() {
return this.type === JsonType.array;
}
get IsBoolean() {
return this.type === JsonType.boolean;
}
get IsDouble() {
return this.type === JsonType.double;
}
get IsObject() {
return this.type === JsonType.object;
}
get IsString() {
return this.type === JsonType.string;
}
get IsNull() {
return this.type === JsonType.null;
}
get BooleanValue() {
return this.inst_boolean;
}
get NumberValue() {
return this.inst_double;
}
get StringValue() {
return this.inst_string;
}
getEnumerator() {
return this.ensureList().values();
}
*[Symbol.iterator]() {
if (this.type === JsonType.array) {
for (let item of this.inst_array) {
yield item;
}
}
}
ensureDictionary() {
if (this.type === JsonType.object) {
return this.inst_object;
}
this.type = JsonType.object;
this.inst_object = {};
return this.inst_object;
}
ensureList() {
if (this.type === JsonType.array) {
return this.inst_array;
}
this.type = JsonType.array;
this.inst_array = [];
return this.inst_array;
}
setBoolean(val) {
this.type = JsonType.boolean;
this.inst_boolean = val;
}
setDouble(val) {
this.type = JsonType.double;
this.inst_double = val;
}
setString(val) {
this.type = JsonType.string;
this.inst_string = val;
}
setNull() {
this.type = JsonType.null;
}
add(val) {
this.ensureList().push(val);
}
set(key, val) {
this.ensureDictionary()[key] = val;
}
setJsonType(type) {
if (this.type === type) {
return;
}
switch (type) {
case JsonType.none:
break;
case JsonType.object:
this.inst_object = {};
break;
case JsonType.array:
this.inst_array = [];
break;
case JsonType.string:
this.inst_string = '';
break;
case JsonType.double:
this.inst_double = 0;
break;
case JsonType.boolean:
this.inst_boolean = false;
break;
}
this.type = type;
}
toString() {
let stringBuilder = [];
this.toStringBuilder(stringBuilder);
return stringBuilder.join('');
}
toStringBuilder(stringBuilder) {
if (this.IsNull) {
stringBuilder.push('null');
} else if (this.IsBoolean) {
stringBuilder.push(this.inst_boolean ? 'true' : 'false');
} else if (this.IsArray) {
stringBuilder.push('[');
for (let i = 0; i < this.inst_array.length; i++) {
if (i > 0) {
stringBuilder.push(',');
}
this.inst_array[i].toStringBuilder(stringBuilder);
}
stringBuilder.push(']');
} else if (this.IsObject) {
stringBuilder.push('{');
let first = true;
for (let key in this.inst_object) {
if (!first) {
stringBuilder.push(',');
}
first = false;
stringBuilder.push('"');
stringBuilder.push(key.replace(/"/g, '\\"').replace(/\n/g, '\\n').replace(/\r/g, '\\r')
.replace(/\t/g, '\\t').replace(/\0/g, '\\0').replace(/\v/g, '\\v')
.replace(/\a/g, '\\a').replace(/\b/g, '\\b').replace(/\f/g, '\\f'));
stringBuilder.push('":');
this.inst_object[key].toStringBuilder(stringBuilder);
}
stringBuilder.push('}');
} else if (this.IsString) {
stringBuilder.push('"');
stringBuilder.push(this.inst_string.replace(/"/g, '\\"').replace(/\n/g, '\\n').replace(/\r/g, '\\r')
.replace(/\t/g, '\\t').replace(/\0/g, '\\0').replace(/\v/g, '\\v')
.replace(/\a/g, '\\a').replace(/\b/g, '\\b').replace(/\f/g, '\\f'));
stringBuilder.push('"');
} else if (this.IsDouble) {
stringBuilder.push(this.inst_double.toString());
}
}
}
export { JsonData };