-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathencoder.ts
More file actions
138 lines (118 loc) · 3.25 KB
/
encoder.ts
File metadata and controls
138 lines (118 loc) · 3.25 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
import { Buffer } from "./util/index";
export class JSONEncoder {
private _isFirstKey: i32[];
private result: string[];
constructor() {
this._isFirstKey = new Array<i32>(10);
this.result = new Array<string>();
this._isFirstKey.push(1);
}
get isFirstKey(): bool {
return <bool>this._isFirstKey[this._isFirstKey.length - 1];
}
serialize(): Uint8Array {
// TODO: Write directly to UTF8 bytes
return Buffer.fromString(this.toString());
}
toString(): string {
return this.result.join("");
}
setString(name: string | null, value: string): void {
this.writeKey(name);
this.writeString(value);
}
setBoolean(name: string | null, value: bool): void {
this.writeKey(name);
this.writeBoolean(value);
}
setNull(name: string | null): void {
this.writeKey(name);
this.write("null");
}
setInteger(name: string | null, value: i64): void {
this.writeKey(name);
this.writeInteger(value);
}
setFloat(name: string | null, value: f64): void {
this.writeKey(name);
this.writeFloat(value);
}
pushArray(name: string | null): bool {
this.writeKey(name);
this.write("[");
this._isFirstKey.push(1);
return true;
}
popArray(): void {
this.write("]");
this._isFirstKey.pop();
}
pushObject(name: string | null): bool {
this.writeKey(name);
this.write("{");
this._isFirstKey.push(1);
return true;
}
popObject(): void {
this.write("}");
this._isFirstKey.pop();
}
private writeKey(str: string | null): void {
if (!this.isFirstKey) {
this.write(",");
} else {
this._isFirstKey[this._isFirstKey.length - 1] = 0;
}
if (str != null && (<string>str).length > 0) {
this.writeString(str);
this.write(":");
}
}
private writeString(str: string): void {
this.write('"');
let savedIndex = 0;
for (let i = 0; i < str.length; i++) {
let char = str.charCodeAt(i);
let needsEscaping =
char < 0x20 || char == '"'.charCodeAt(0) || char == "\\".charCodeAt(0);
if (needsEscaping) {
this.write(str.substring(savedIndex, i));
savedIndex = i + 1;
if (char == '"'.charCodeAt(0)) {
this.write('\\"');
} else if (char == "\\".charCodeAt(0)) {
this.write("\\\\");
} else if (char == "\b".charCodeAt(0)) {
this.write("\\b");
} else if (char == "\n".charCodeAt(0)) {
this.write("\\n");
} else if (char == "\r".charCodeAt(0)) {
this.write("\\r");
} else if (char == "\t".charCodeAt(0)) {
this.write("\\t");
} else {
// TODO: Implement encoding for other contol characters
// @ts-ignore integer does have toString
assert(
false,
"Unsupported control character code: " + char.toString()
);
}
}
}
this.write(str.substring(savedIndex, str.length));
this.write('"');
}
private writeBoolean(value: bool): void {
this.write(value ? "true" : "false");
}
private writeInteger(value: i64): void {
this.write(value.toString());
}
private writeFloat(value: f64): void {
this.write(value.toString());
}
private write(str: string): void {
this.result.push(str);
}
}