-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJSONEncoder.class.nut
196 lines (153 loc) · 4.48 KB
/
JSONEncoder.class.nut
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
// Copyright (c) 2017 Electric Imp
// This file is licensed under the MIT License
// http://opensource.org/licenses/MIT
class JSONEncoder {
static VERSION = "2.0.0";
// max structure depth
// anything above probably has a cyclic ref
static _maxDepth = 32;
/**
* Encode value to JSON
* @param {table|array|*} value
* @returns {string}
*/
function encode(value) {
return this._encode(value);
}
/**
* @param {table|array} val
* @param {integer=0} depth – current depth level
* @private
*/
function _encode(val, depth = 0) {
// detect cyclic reference
if (depth > this._maxDepth) {
throw "Possible cyclic reference";
}
local
r = "",
s = "",
i = 0;
switch (typeof val) {
case "table":
case "class":
s = "";
// serialize properties, but not functions
foreach (k, v in val) {
if (typeof v != "function") {
s += ",\"" + k + "\":" + this._encode(v, depth + 1);
}
}
s = s.len() > 0 ? s.slice(1) : s;
r += "{" + s + "}";
break;
case "array":
s = "";
for (i = 0; i < val.len(); i++) {
s += "," + this._encode(val[i], depth + 1);
}
s = (i > 0) ? s.slice(1) : s;
r += "[" + s + "]";
break;
case "integer":
case "float":
case "bool":
r += val;
break;
case "null":
r += "null";
break;
case "instance":
if ("_serializeRaw" in val && typeof val._serializeRaw == "function") {
// include value produced by _serializeRaw()
r += val._serializeRaw().tostring();
} else if ("_serialize" in val && typeof val._serialize == "function") {
// serialize instances by calling _serialize method
r += this._encode(val._serialize(), depth + 1);
} else {
s = "";
try {
// iterate through instances which implement _nexti meta-method
foreach (k, v in val) {
s += ",\"" + k + "\":" + this._encode(v, depth + 1);
}
} catch (e) {
// iterate through instances w/o _nexti
// serialize properties, but not functions
foreach (k, v in val.getclass()) {
if (typeof v != "function") {
s += ",\"" + k + "\":" + this._encode(val[k], depth + 1);
}
}
}
s = s.len() > 0 ? s.slice(1) : s;
r += "{" + s + "}";
}
break;
case "blob":
// This is a workaround for a known bug:
// on device side Blob.tostring() returns null
// (instaead of an empty string)
r += "\"" + (val.len() ? this._escape(val.tostring()) : "") + "\"";
break;
// strings and all other
default:
r += "\"" + this._escape(val.tostring()) + "\"";
break;
}
return r;
}
/**
* Escape strings according to http://www.json.org/ spec
* @param {string} str
*/
function _escape(str) {
local res = "";
for (local i = 0; i < str.len(); i++) {
local ch1 = (str[i] & 0xFF);
if ((ch1 & 0x80) == 0x00) {
// 7-bit Ascii
ch1 = format("%c", ch1);
if (ch1 == "\"") {
res += "\\\"";
} else if (ch1 == "\\") {
res += "\\\\";
} else if (ch1 == "/") {
res += "\\/";
} else if (ch1 == "\b") {
res += "\\b";
} else if (ch1 == "\f") {
res += "\\f";
} else if (ch1 == "\n") {
res += "\\n";
} else if (ch1 == "\r") {
res += "\\r";
} else if (ch1 == "\t") {
res += "\\t";
} else if (ch1 == "\0") {
res += "\\u0000";
} else {
res += ch1;
}
} else {
if ((ch1 & 0xE0) == 0xC0) {
// 110xxxxx = 2-byte unicode
local ch2 = (str[++i] & 0xFF);
res += format("%c%c", ch1, ch2);
} else if ((ch1 & 0xF0) == 0xE0) {
// 1110xxxx = 3-byte unicode
local ch2 = (str[++i] & 0xFF);
local ch3 = (str[++i] & 0xFF);
res += format("%c%c%c", ch1, ch2, ch3);
} else if ((ch1 & 0xF8) == 0xF0) {
// 11110xxx = 4 byte unicode
local ch2 = (str[++i] & 0xFF);
local ch3 = (str[++i] & 0xFF);
local ch4 = (str[++i] & 0xFF);
res += format("%c%c%c%c", ch1, ch2, ch3, ch4);
}
}
}
return res;
}
}