asyncSerialize.js
2.12 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
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
// tslint:disable
import find from 'lodash/find';
class Serialized {
}
function isSerialized(object) {
return object.hasOwnProperty('__serializer_id');
}
export function toObjects(serializers, o) {
return __awaiter(this, void 0, void 0, function* () {
if (typeof o !== 'object') {
return o;
}
const serializer = find(serializers, (s) => s.isType(o));
if (serializer) {
const value = serializer.toObject ? yield serializer.toObject(o) : o;
return {
__serializer_id: serializer.id,
value: yield toObjects(serializers, value),
};
}
const newO = o instanceof Array ? [] : {};
for (const atr in o) {
newO[atr] = yield toObjects(serializers, o[atr]);
}
return newO;
});
}
export function fromObjects(serializers, o) {
return __awaiter(this, void 0, void 0, function* () {
if (typeof o !== 'object') {
return o;
}
if (isSerialized(o)) {
const value = yield fromObjects(serializers, o.value);
const serializer = find(serializers, ['id', o.__serializer_id]) || {};
if (serializer.fromObject) {
return serializer.fromObject(value);
}
return value;
}
const newO = o instanceof Array ? [] : {};
for (const atr in o) {
newO[atr] = yield fromObjects(serializers, o[atr]);
}
return newO;
});
}
//# sourceMappingURL=asyncSerialize.js.map