-
Notifications
You must be signed in to change notification settings - Fork 4
/
jdata.js
291 lines (275 loc) · 11 KB
/
jdata.js
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/********************************************************************************
JSData - Lightweight JData Annotation Encoder and Decoder for JavaScript and NodeJS
(for JData Specification Draft 2 defined in http://neurojson.org/jdata/draft2)
Author: Qianqian Fang <q.fang at neu.edu>
URL: http://github.com/NeuroJSON/jsdata
Live Demo: https://jsfiddle.net/fangq/7vLxjwa6/
********************************************************************************/
class jdata {
constructor(data = {}, options = {}) {
this.opt = options;
this.data = data;
this.const = {
_Inf_: Infinity,
_NaN_: NaN
};
this.base64 = options.hasOwnProperty('base64') ? options.base64 : true;
this.typedfun = {
"Float32Array": null,
"Float64Array": null,
"Int8Array": null,
"Uint8Array": null,
"Int16Array": null,
"Uint16Array": null,
"Int32Array": null,
"Uint32Array": null,
"BigInt64Array": null,
"BigUint64Array": null
};
this._zipper = (typeof pako !== 'undefined' ?
pako :
options.hasOwnProperty('zlib') ? require(options['zlib']) : require('pako'));
this._nj = (typeof nj !== 'undefined' ?
nj :
require('numjs'));
this._nj.NdArray.prototype.toJSON = function() {
return JSON.stringify(this.tolist(), function(k, v) {
if (typeof v === 'bigint')
return '~~' + v.toString() + '~~';
return v;
});
};
}
encode() {
this.data = this._encode(this.data);
return this;
}
decode() {
this.data = this._decode(this.data);
return this;
}
base64ToBytes(base64) {
const binString = atob(base64);
return Uint8Array.from(binString, (m) => m.codePointAt(0));
}
bytesToBase64(bytes) {
const binString = String.fromCodePoint(...bytes);
return btoa(binString);
}
tojson() {
return JSON.stringify(this.data, this._exportfilter.bind(this), '\t').replace(/\\/g, '')
.replace(/\"\[/g, '[')
.replace(/\]\"/g, ']')
.replace(/\"\{/g, '{')
.replace(/\}\"/g, '}')
.replace(/\"~~/g, '')
.replace(/~~\"/g, '');
}
zip(buf, method) {
if (method !== 'zlib' || method !== 'gzip')
method = 'zlib';
if (method === 'zlib') {
if (this.base64)
return this.bytesToBase64(this._zipper.deflate(new Uint8Array(buf), {
to: 'string'
}));
else
return this._zipper.deflate(new Uint8Array(buf), {
to: 'string'
});
} else if (method === 'gzip') {
if (this.base64)
return this.bytesToBase64(this._zipper.gzip(new Uint8Array(buf), {
to: 'string'
}));
else
return this._zipper.gzip(new Uint8Array(buf), {
to: 'string'
});
}
}
unzip(str, method) {
if (method === 'zlib')
return this._zipper.inflate(str);
else if (method === 'gzip')
return this._zipper.ungzip(str);
else if (method === 'lzma') {
if (typeof LZMA !== 'undefined') { // need js-lzma (https://github.com/jcmellado/js-lzma)
var inStream = new LZMA.iStream(str.buffer);
return LZMA.decompressFile(inStream).toUint8Array();
}
return this._zipper.decompressFile(str); // must use {zlib:'lzma-purejs'} in the constructor
} else
throw "compression method not supported";
}
_istypedarray(obj) {
return !!obj && obj.byteLength !== undefined;
}
static _str2hex(str) {
str = encodeURIComponent(str).split('%').join('');
return str.toLowerCase();
}
_exportfilter(k, v) {
if (typeof v === 'bigint') {
return v.toString();
} else if (v instanceof Array) {
return JSON.stringify(v);
} else if (this._istypedarray(v)) {
return Array.apply([], v);
} else if (v instanceof this._nj.NdArray) {
return v.tolist();
} else if (v instanceof Map)
return Array.from(v.entries());
return v;
}
_encode(obj) {
let newobj = obj;
if (typeof obj == 'number') {
if (obj === Infinity) {
return "_Inf_";
} else if (obj === -Infinity) {
return "-_Inf_";
} else if (obj !== obj) {
return "_NaN_";
}
} else if (obj instanceof Array) {
obj.forEach(function(e, idx, orig) {
orig[idx] = this._encode(e);
}.bind(this));
newobj = obj;
} else if (this._istypedarray(obj)) {
let dtype = Object.prototype.toString.call(obj);
if (dtype == '[object ArrayBuffer]') {
obj = new Uint8Array(obj);
dtype = Object.prototype.toString.call(obj);
}
dtype = dtype.replace(/\[object /, '').replace(/^Big/, '')
.replace(/Array\]/, '').replace(/Clamped/, '');
dtype = dtype.replace(/Float32/, 'single').replace(/Float64/, 'double');
newobj = {
_ArrayType_: dtype.toLowerCase(),
_ArraySize_: obj.length
};
if ((dtype == 'Int64' || dtype == 'Uint64') || this.opt !== undefined && this.opt.hasOwnProperty('compression')) {
if (this.opt.compression === undefined)
newobj._ArrayZipType_ = 'zlib';
else
newobj._ArrayZipType_ = this.opt.compression;
newobj._ArrayZipSize_ = obj.length;
newobj._ArrayZipData_ = this.zip(obj.buffer, newobj._ArrayZipType_);
} else {
newobj._ArrayData_ = Array.from(obj);
}
} else if (typeof obj === 'bigint') {
newobj = this._encode(BigInt64Array.from([obj]));
} else if (obj instanceof this._nj.NdArray) {
let dtype = obj.dtype;
dtype = dtype.replace(/float32/, 'single').replace(/float64/, 'double');
newobj = {
_ArrayType_: dtype.toLowerCase(),
_ArraySize_: obj.shape
};
if (this.opt !== undefined && this.opt.hasOwnProperty('compression')) {
newobj._ArrayZipType_ = this.opt.compression;
newobj._ArrayZipSize_ = [1, obj.size];
newobj._ArrayZipData_ = this.zip(obj.selection.data.buffer, this.opt.compression);
} else {
newobj._ArrayData_ = obj.flatten().tolist();
}
} else if (obj instanceof Map) {
newobj = {
_MapData_: []
};
obj.forEach(function(value, key) {
newobj._MapData_.push([this._encode(key), this._encode(value)]);
}.bind(this));
} else if (typeof obj == 'object' && obj !== null) {
for (var k in obj) {
newobj[k] = this._encode(obj[k]);
}
}
return newobj;
}
_decode(obj) {
let newobj = obj;
if (obj instanceof Array) {
obj.forEach(function(e, idx, orig) {
orig[idx] = this._decode(e);
}.bind(this));
newobj = obj;
} else if ((typeof obj == 'string') && obj.length <= 6 && obj.slice(-1) == '_') {
if (obj == '_Inf_')
newobj = Infinity;
else if (obj == '-_Inf_')
newobj = -Infinity;
else if (obj == '_NaN_')
newobj = NaN;
} else if (typeof obj == 'object' && obj !== null) {
if (obj.hasOwnProperty('_ArrayType_')) {
let type = obj._ArrayType_;
let data;
type = type.replace(/single/, 'float32').replace(/double/, 'float64');
let typename = type.charAt(0).toUpperCase() + type.substring(1) + "Array";
if (type == 'int64' || type == 'uint64')
typename = 'Big' + typename;
if (obj.hasOwnProperty('_ArrayZipData_')) {
if (this.base64)
data = this.unzip(this.base64ToBytes(obj._ArrayZipData_), obj._ArrayZipType_);
else
data = this.unzip(obj._ArrayZipData_, obj._ArrayZipType_);
//data=Uint8Array.from(data);
if (this.typedfun[typename] == null)
this.typedfun[typename] = new Function('d', 'return new ' + typename + '(d)');
let typecast = this.typedfun[typename];
data = typecast(data.buffer);
data = this._nj.array(data, type).reshape(obj._ArraySize_);
} else if (obj.hasOwnProperty('_ArrayData_')) {
data = obj._ArrayData_;
}
newobj = data;
return newobj;
} else if (obj.hasOwnProperty('_MapData_') && Array.isArray(obj._MapData_)) {
newobj = new Map();
obj._MapData_.forEach(function(e) {
newobj.set(this._decode(e[0]), this._decode(e[1]));
}.bind(this));
return newobj;
} else if (obj.hasOwnProperty('_ByteStream_')) {
if (obj._ByteStream_.hasOwnProperty('_ArraySize_')) {
newobj = this._decode(obj._ByteStream_);
} else {
if (this.base64)
newobj = new Blob(this.base64ToBytes(obj._ByteStream_), {
type: "octet/stream"
});
else
newobj = new Blob(obj._ByteStream_, {
type: "octet/stream"
});
}
return newobj;
} else if (obj.hasOwnProperty('_TableData_') &&
obj._TableData_.hasOwnProperty('_TableRecords_') &&
obj._TableData_._TableRecords_.length) {
newobj = {};
if (obj._TableData_._TableCols_.length == obj._TableData_._TableRecords_[0].length) {
obj._TableData_._TableCols_.forEach(function(e) {
newobj[e] = [];
});
obj._TableRecords_.forEach(function(e) {
for (let i = 0; i < e.length; i++)
newobj[obj._TableData_._TableCols_[i]].push(e[i]);
});
}
return newobj;
}
for (var k in obj) {
newobj[k] = this._decode(obj[k]);
}
}
return newobj;
}
}
try {
module.exports = jdata;
} catch (e) {}