@hg-ts/serialize 0.0.15
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.eslintcache +1 -0
- package/.eslintrc.json +1 -0
- package/.mocharc.cjs +1 -0
- package/dist/additional-classes.d.ts +3 -0
- package/dist/additional-classes.d.ts.map +1 -0
- package/dist/additional-classes.js +9 -0
- package/dist/additional-classes.js.map +1 -0
- package/dist/deserialize.d.ts +3 -0
- package/dist/deserialize.d.ts.map +1 -0
- package/dist/deserialize.js +58 -0
- package/dist/deserialize.js.map +1 -0
- package/dist/exceptions/deserialize-unknown-prototype.exception.d.ts +5 -0
- package/dist/exceptions/deserialize-unknown-prototype.exception.d.ts.map +1 -0
- package/dist/exceptions/deserialize-unknown-prototype.exception.js +11 -0
- package/dist/exceptions/deserialize-unknown-prototype.exception.js.map +1 -0
- package/dist/exceptions/deserialize.exception.d.ts +6 -0
- package/dist/exceptions/deserialize.exception.d.ts.map +1 -0
- package/dist/exceptions/deserialize.exception.js +11 -0
- package/dist/exceptions/deserialize.exception.js.map +1 -0
- package/dist/exceptions/index.d.ts +4 -0
- package/dist/exceptions/index.d.ts.map +1 -0
- package/dist/exceptions/index.js +7 -0
- package/dist/exceptions/index.js.map +1 -0
- package/dist/exceptions/serialize.exception.d.ts +5 -0
- package/dist/exceptions/serialize.exception.d.ts.map +1 -0
- package/dist/exceptions/serialize.exception.js +11 -0
- package/dist/exceptions/serialize.exception.js.map +1 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +11 -0
- package/dist/index.js.map +1 -0
- package/dist/serialize.d.ts +4 -0
- package/dist/serialize.d.ts.map +1 -0
- package/dist/serialize.js +104 -0
- package/dist/serialize.js.map +1 -0
- package/dist/tests/serialization.test.d.ts +10 -0
- package/dist/tests/serialization.test.d.ts.map +1 -0
- package/dist/tests/serialization.test.js +64 -0
- package/dist/tests/serialization.test.js.map +1 -0
- package/dist/types.d.ts +49 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +3 -0
- package/dist/types.js.map +1 -0
- package/dist-esm/additional-classes.d.ts +3 -0
- package/dist-esm/additional-classes.d.ts.map +1 -0
- package/dist-esm/additional-classes.js +5 -0
- package/dist-esm/additional-classes.js.map +1 -0
- package/dist-esm/deserialize.d.ts +3 -0
- package/dist-esm/deserialize.d.ts.map +1 -0
- package/dist-esm/deserialize.js +54 -0
- package/dist-esm/deserialize.js.map +1 -0
- package/dist-esm/exceptions/deserialize-unknown-prototype.exception.d.ts +5 -0
- package/dist-esm/exceptions/deserialize-unknown-prototype.exception.d.ts.map +1 -0
- package/dist-esm/exceptions/deserialize-unknown-prototype.exception.js +7 -0
- package/dist-esm/exceptions/deserialize-unknown-prototype.exception.js.map +1 -0
- package/dist-esm/exceptions/deserialize.exception.d.ts +6 -0
- package/dist-esm/exceptions/deserialize.exception.d.ts.map +1 -0
- package/dist-esm/exceptions/deserialize.exception.js +7 -0
- package/dist-esm/exceptions/deserialize.exception.js.map +1 -0
- package/dist-esm/exceptions/index.d.ts +4 -0
- package/dist-esm/exceptions/index.d.ts.map +1 -0
- package/dist-esm/exceptions/index.js +4 -0
- package/dist-esm/exceptions/index.js.map +1 -0
- package/dist-esm/exceptions/serialize.exception.d.ts +5 -0
- package/dist-esm/exceptions/serialize.exception.d.ts.map +1 -0
- package/dist-esm/exceptions/serialize.exception.js +7 -0
- package/dist-esm/exceptions/serialize.exception.js.map +1 -0
- package/dist-esm/index.d.ts +6 -0
- package/dist-esm/index.d.ts.map +1 -0
- package/dist-esm/index.js +6 -0
- package/dist-esm/index.js.map +1 -0
- package/dist-esm/serialize.d.ts +4 -0
- package/dist-esm/serialize.d.ts.map +1 -0
- package/dist-esm/serialize.js +99 -0
- package/dist-esm/serialize.js.map +1 -0
- package/dist-esm/tests/serialization.test.d.ts +10 -0
- package/dist-esm/tests/serialization.test.d.ts.map +1 -0
- package/dist-esm/tests/serialization.test.js +61 -0
- package/dist-esm/tests/serialization.test.js.map +1 -0
- package/dist-esm/types.d.ts +49 -0
- package/dist-esm/types.d.ts.map +1 -0
- package/dist-esm/types.js +2 -0
- package/dist-esm/types.js.map +1 -0
- package/package.json +39 -0
- package/src/additional-classes.ts +5 -0
- package/src/deserialize.ts +69 -0
- package/src/exceptions/deserialize-unknown-prototype.exception.ts +7 -0
- package/src/exceptions/deserialize.exception.ts +9 -0
- package/src/exceptions/index.ts +3 -0
- package/src/exceptions/serialize.exception.ts +7 -0
- package/src/index.ts +7 -0
- package/src/serialize.ts +114 -0
- package/src/tests/serialization.test.ts +47 -0
- package/src/types.ts +71 -0
- package/tsconfig.esm.json +9 -0
- package/tsconfig.json +10 -0
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { BaseException } from '@hg-ts/exception';
|
|
2
|
+
export class DeserializeUnknownPrototypeException extends BaseException {
|
|
3
|
+
constructor(ctorName) {
|
|
4
|
+
super(`Deserialize error. Constructor with name ${ctorName} not found`);
|
|
5
|
+
}
|
|
6
|
+
}
|
|
7
|
+
//# sourceMappingURL=deserialize-unknown-prototype.exception.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"deserialize-unknown-prototype.exception.js","sourceRoot":"","sources":["../../src/exceptions/deserialize-unknown-prototype.exception.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC;AAEjD,MAAM,OAAO,oCAAqC,SAAQ,aAAa;IACtE,YAAmB,QAAgB;QAClC,KAAK,CAAC,4CAA4C,QAAQ,YAAY,CAAC,CAAC;IACzE,CAAC;CACD"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"deserialize.exception.d.ts","sourceRoot":"","sources":["../../src/exceptions/deserialize.exception.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC;AAEjD,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,UAAU,CAAC;AAEhD,qBAAa,oBAAqB,SAAQ,aAAa;gBACnC,KAAK,EAAE,eAAe;CAGzC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"deserialize.exception.js","sourceRoot":"","sources":["../../src/exceptions/deserialize.exception.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC;AAIjD,MAAM,OAAO,oBAAqB,SAAQ,aAAa;IACtD,YAAmB,KAAsB;QACxC,KAAK,CAAC,gCAAgC,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC;IAChE,CAAC;CACD"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/exceptions/index.ts"],"names":[],"mappings":"AAAA,cAAc,2CAA2C,CAAC;AAC1D,cAAc,yBAAyB,CAAC;AACxC,cAAc,uBAAuB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/exceptions/index.ts"],"names":[],"mappings":"AAAA,cAAc,2CAA2C,CAAC;AAC1D,cAAc,yBAAyB,CAAC;AACxC,cAAc,uBAAuB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"serialize.exception.d.ts","sourceRoot":"","sources":["../../src/exceptions/serialize.exception.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC;AAEjD,qBAAa,kBAAmB,SAAQ,aAAa;gBACjC,IAAI,EAAE,MAAM;CAG/B"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"serialize.exception.js","sourceRoot":"","sources":["../../src/exceptions/serialize.exception.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC;AAEjD,MAAM,OAAO,kBAAmB,SAAQ,aAAa;IACpD,YAAmB,IAAY;QAC9B,KAAK,CAAC,uCAAuC,IAAI,EAAE,CAAC,CAAC;IACtD,CAAC;CACD"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,SAAS,CAAC;AAExB,OAAO,EAAE,kBAAkB,EAAE,MAAM,sBAAsB,CAAC;AAC1D,cAAc,aAAa,CAAC;AAC5B,cAAc,eAAe,CAAC;AAE9B,cAAc,cAAc,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,SAAS,CAAC;AAExB,OAAO,EAAE,kBAAkB,EAAE,MAAM,sBAAsB,CAAC;AAC1D,cAAc,aAAa,CAAC;AAC5B,cAAc,eAAe,CAAC;AAE9B,cAAc,cAAc,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"serialize.d.ts","sourceRoot":"","sources":["../src/serialize.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAGX,eAAe,EACf,MAAM,SAAS,CAAC;AAGjB,wBAAgB,qBAAqB,CAAC,KAAK,EAAE,MAAM,CAAC,MAAM,GAAG,MAAM,EAAE,OAAO,CAAC,GAAG,MAAM,EAAE,CAWvF;AA6FD,wBAAgB,SAAS,CAAC,KAAK,EAAE,OAAO,GAAG,eAAe,CAEzD"}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import { SerializeException } from './exceptions';
|
|
2
|
+
export function getPrototypeListNames(value) {
|
|
3
|
+
const result = [];
|
|
4
|
+
let nextPrototype = Object.getPrototypeOf(value);
|
|
5
|
+
while (nextPrototype !== null) {
|
|
6
|
+
result.push(nextPrototype.constructor.name);
|
|
7
|
+
nextPrototype = Object.getPrototypeOf(nextPrototype);
|
|
8
|
+
}
|
|
9
|
+
return result;
|
|
10
|
+
}
|
|
11
|
+
function serializeArray(value) {
|
|
12
|
+
return {
|
|
13
|
+
type: 'array',
|
|
14
|
+
value: value.map(item => serializeValue(item)),
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
function serializeRecord(value) {
|
|
18
|
+
const fields = Object.keys(value)
|
|
19
|
+
.reduce((result, key) => {
|
|
20
|
+
if (typeof key === 'symbol') {
|
|
21
|
+
throw new SerializeException('symbol key');
|
|
22
|
+
}
|
|
23
|
+
result[key] = serializeValue(value[key]);
|
|
24
|
+
return result;
|
|
25
|
+
}, {});
|
|
26
|
+
return {
|
|
27
|
+
type: 'object',
|
|
28
|
+
prototypeNameList: getPrototypeListNames(value),
|
|
29
|
+
value: fields,
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
function serializeObject(value) {
|
|
33
|
+
if (value === null) {
|
|
34
|
+
return { type: 'null' };
|
|
35
|
+
}
|
|
36
|
+
if (value instanceof Date) {
|
|
37
|
+
return {
|
|
38
|
+
type: 'date',
|
|
39
|
+
value: value.getTime(),
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
if (value instanceof Buffer) {
|
|
43
|
+
return {
|
|
44
|
+
type: 'buffer',
|
|
45
|
+
value: value.toString('hex'),
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
if (value instanceof Map) {
|
|
49
|
+
return {
|
|
50
|
+
type: 'map',
|
|
51
|
+
value: [...value.entries()].map(([key, value]) => ({
|
|
52
|
+
key: serializeValue(key),
|
|
53
|
+
value: serializeValue(value),
|
|
54
|
+
})),
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
if (value instanceof Set) {
|
|
58
|
+
return {
|
|
59
|
+
type: 'set',
|
|
60
|
+
value: [...value.values()].map(item => serializeValue(item)),
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
if (Array.isArray(value)) {
|
|
64
|
+
return serializeArray(value);
|
|
65
|
+
}
|
|
66
|
+
return serializeRecord(value);
|
|
67
|
+
}
|
|
68
|
+
function serializeValue(value) {
|
|
69
|
+
switch (typeof value) {
|
|
70
|
+
case 'boolean':
|
|
71
|
+
return {
|
|
72
|
+
value,
|
|
73
|
+
type: 'boolean',
|
|
74
|
+
};
|
|
75
|
+
case 'bigint':
|
|
76
|
+
return {
|
|
77
|
+
type: 'bigint',
|
|
78
|
+
value: String(value),
|
|
79
|
+
};
|
|
80
|
+
case 'string':
|
|
81
|
+
return {
|
|
82
|
+
value,
|
|
83
|
+
type: 'string',
|
|
84
|
+
};
|
|
85
|
+
case 'number':
|
|
86
|
+
return {
|
|
87
|
+
value,
|
|
88
|
+
type: 'number',
|
|
89
|
+
};
|
|
90
|
+
case 'object':
|
|
91
|
+
return serializeObject(value);
|
|
92
|
+
default:
|
|
93
|
+
throw new SerializeException(typeof value);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
export function serialize(value) {
|
|
97
|
+
return serializeValue(value);
|
|
98
|
+
}
|
|
99
|
+
//# sourceMappingURL=serialize.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"serialize.js","sourceRoot":"","sources":["../src/serialize.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,kBAAkB,EAAE,MAAM,cAAc,CAAC;AAElD,MAAM,UAAU,qBAAqB,CAAC,KAAuC;IAC5E,MAAM,MAAM,GAAa,EAAE,CAAC;IAC5B,IAAI,aAAa,GAAG,MAAM,CAAC,cAAc,CAAC,KAAK,CAAC,CAAC;IAEjD,OAAO,aAAa,KAAK,IAAI,EAAE;QAC9B,MAAM,CAAC,IAAI,CAAC,aAAa,CAAC,WAAW,CAAC,IAAI,CAAC,CAAC;QAE5C,aAAa,GAAG,MAAM,CAAC,cAAc,CAAC,aAAa,CAAC,CAAC;KACrD;IAED,OAAO,MAAM,CAAC;AACf,CAAC;AAED,SAAS,cAAc,CAAC,KAAgB;IACvC,OAAO;QACN,IAAI,EAAE,OAAO;QACb,KAAK,EAAE,KAAK,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,CAAC,cAAc,CAAC,IAAI,CAAC,CAA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AC;aAC5B,CAAC,CAAC;SACH,CAAC;KACF;IACD,IAAI,KAAK,YAAY,GAAG,EAAE;QACzB,OAAO;YACN,IAAI,EAAE,KAAK;YACX,KAAK,EAAE,CAAC,GAAG,KAAK,CAAC,MAAM,EAAE,CAAC,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,CAAC,cAAc,CAAC,IAAI,CAAC,CAAC;SAC5D,CAAC;KACF;IACD,IAAI,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,EAAE;QACzB,OAAO,cAAc,CAAC,KAAK,CAAC,CAAC;KAC7B;IAED,OAAO,eAAe,CAAC,KAAY,CAAC,CAAC;AACtC,CAAC;AAED,SAAS,cAAc,CAAC,KAAc;IACrC,QAAQ,OAAO,KAAK,EAAE;QACtB,KAAK,SAAS;YACb,OAAO;gBACN,KAAK;gBACL,IAAI,EAAE,SAAS;aACf,CAAC;QACH,KAAK,QAAQ;YACZ,OAAO;gBACN,IAAI,EAAE,QAAQ;gBACd,KAAK,EAAE,MAAM,CAAC,KAAK,CAAC;aACpB,CAAC;QACH,KAAK,QAAQ;YACZ,OAAO;gBACN,KAAK;gBACL,IAAI,EAAE,QAAQ;aACd,CAAC;QACH,KAAK,QAAQ;YACZ,OAAO;gBACN,KAAK;gBACL,IAAI,EAAE,QAAQ;aACd,CAAC;QACH,KAAK,QAAQ;YACZ,OAAO,eAAe,CAAC,KAAY,CAAC,CAAC;QACtC;YACC,MAAM,IAAI,kBAAkB,CAAC,OAAO,KAAK,CAAC,CAAC;KAC3C;AACF,CAAC;AAED,MAAM,UAAU,SAAS,CAAC,KAAc;IACvC,OAAO,cAAc,CAAC,KAAK,CAAC,CAAC;AAC9B,CAAC"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import '@hg-ts/types';
|
|
2
|
+
import { TestSuite } from '@hg-ts/tests';
|
|
3
|
+
export declare class SerializationTestSuite extends TestSuite {
|
|
4
|
+
serializeNull(): Promise<void>;
|
|
5
|
+
serializeNumber(): Promise<void>;
|
|
6
|
+
serializeBigint(): Promise<void>;
|
|
7
|
+
serializeBuffer(): Promise<void>;
|
|
8
|
+
protected test(expected: unknown): void;
|
|
9
|
+
}
|
|
10
|
+
//# sourceMappingURL=serialization.test.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"serialization.test.d.ts","sourceRoot":"","sources":["../../src/tests/serialization.test.ts"],"names":[],"mappings":"AAAA,OAAO,cAAc,CAAC;AACtB,OAAO,EAEN,SAAS,EAGT,MAAM,cAAc,CAAC;AAItB,qBACa,sBAAuB,SAAQ,SAAS;IAEvC,aAAa,IAAI,OAAO,CAAC,IAAI,CAAC;IAK9B,eAAe,IAAI,OAAO,CAAC,IAAI,CAAC;IAShC,eAAe,IAAI,OAAO,CAAC,IAAI,CAAC;IAUhC,eAAe,IAAI,OAAO,CAAC,IAAI,CAAC;IAI7C,SAAS,CAAC,IAAI,CAAC,QAAQ,EAAE,OAAO,GAAG,IAAI;CAKvC"}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { __decorate, __metadata } from "tslib";
|
|
2
|
+
import '@hg-ts/types';
|
|
3
|
+
import { Test, TestSuite, Describe, expect, } from '@hg-ts/tests';
|
|
4
|
+
import { serialize } from '../serialize';
|
|
5
|
+
import { deserialize } from '../deserialize';
|
|
6
|
+
let SerializationTestSuite = class SerializationTestSuite extends TestSuite {
|
|
7
|
+
async serializeNull() {
|
|
8
|
+
this.test(null);
|
|
9
|
+
}
|
|
10
|
+
async serializeNumber() {
|
|
11
|
+
this.test(123);
|
|
12
|
+
this.test(12.35);
|
|
13
|
+
this.test(0);
|
|
14
|
+
this.test(-12.35);
|
|
15
|
+
this.test(-123);
|
|
16
|
+
}
|
|
17
|
+
async serializeBigint() {
|
|
18
|
+
this.test(1235n * 1000000000000000000000000n);
|
|
19
|
+
this.test(1235n);
|
|
20
|
+
this.test(123n);
|
|
21
|
+
this.test(0n);
|
|
22
|
+
this.test(-123n);
|
|
23
|
+
this.test(-1235n * 1000000000000000000000000n);
|
|
24
|
+
}
|
|
25
|
+
async serializeBuffer() {
|
|
26
|
+
this.test(Buffer.from('sdfsdgdfgdfg'));
|
|
27
|
+
}
|
|
28
|
+
test(expected) {
|
|
29
|
+
const value = deserialize(serialize(expected));
|
|
30
|
+
expect(value).toEqual(expected);
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
__decorate([
|
|
34
|
+
Test(),
|
|
35
|
+
__metadata("design:type", Function),
|
|
36
|
+
__metadata("design:paramtypes", []),
|
|
37
|
+
__metadata("design:returntype", Promise)
|
|
38
|
+
], SerializationTestSuite.prototype, "serializeNull", null);
|
|
39
|
+
__decorate([
|
|
40
|
+
Test(),
|
|
41
|
+
__metadata("design:type", Function),
|
|
42
|
+
__metadata("design:paramtypes", []),
|
|
43
|
+
__metadata("design:returntype", Promise)
|
|
44
|
+
], SerializationTestSuite.prototype, "serializeNumber", null);
|
|
45
|
+
__decorate([
|
|
46
|
+
Test(),
|
|
47
|
+
__metadata("design:type", Function),
|
|
48
|
+
__metadata("design:paramtypes", []),
|
|
49
|
+
__metadata("design:returntype", Promise)
|
|
50
|
+
], SerializationTestSuite.prototype, "serializeBigint", null);
|
|
51
|
+
__decorate([
|
|
52
|
+
Test(),
|
|
53
|
+
__metadata("design:type", Function),
|
|
54
|
+
__metadata("design:paramtypes", []),
|
|
55
|
+
__metadata("design:returntype", Promise)
|
|
56
|
+
], SerializationTestSuite.prototype, "serializeBuffer", null);
|
|
57
|
+
SerializationTestSuite = __decorate([
|
|
58
|
+
Describe()
|
|
59
|
+
], SerializationTestSuite);
|
|
60
|
+
export { SerializationTestSuite };
|
|
61
|
+
//# sourceMappingURL=serialization.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"serialization.test.js","sourceRoot":"","sources":["../../src/tests/serialization.test.ts"],"names":[],"mappings":";AAAA,OAAO,cAAc,CAAC;AACtB,OAAO,EACN,IAAI,EACJ,SAAS,EACT,QAAQ,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;AAjCA;IADC,IAAI,EAAE;;;;2DAGN;AAGD;IADC,IAAI,EAAE;;;;6DAON;AAGD;IADC,IAAI,EAAE;;;;6DAQN;AAGD;IADC,IAAI,EAAE;;;;6DAGN;AA5BW,sBAAsB;IADlC,QAAQ,EAAE;GACE,sBAAsB,CAmClC;SAnCY,sBAAsB"}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
export declare type SerializedNumber = {
|
|
2
|
+
type: 'number';
|
|
3
|
+
value: number;
|
|
4
|
+
};
|
|
5
|
+
export declare type SerializedString = {
|
|
6
|
+
type: 'string';
|
|
7
|
+
value: string;
|
|
8
|
+
};
|
|
9
|
+
export declare type SerializedBoolean = {
|
|
10
|
+
type: 'boolean';
|
|
11
|
+
value: boolean;
|
|
12
|
+
};
|
|
13
|
+
export declare type SerializedBigint = {
|
|
14
|
+
type: 'bigint';
|
|
15
|
+
value: string;
|
|
16
|
+
};
|
|
17
|
+
export declare type SerializedDate = {
|
|
18
|
+
type: 'date';
|
|
19
|
+
value: number;
|
|
20
|
+
};
|
|
21
|
+
export declare type SerializedArray = {
|
|
22
|
+
type: 'array';
|
|
23
|
+
value: SerializedValue[];
|
|
24
|
+
};
|
|
25
|
+
export declare type SerializedBuffer = {
|
|
26
|
+
type: 'buffer';
|
|
27
|
+
value: string;
|
|
28
|
+
};
|
|
29
|
+
export declare type SerializedObject = {
|
|
30
|
+
type: 'object';
|
|
31
|
+
prototypeNameList: string[];
|
|
32
|
+
value: Record<string, SerializedValue>;
|
|
33
|
+
};
|
|
34
|
+
export declare type SerializedMap = {
|
|
35
|
+
type: 'map';
|
|
36
|
+
value: {
|
|
37
|
+
key: SerializedValue;
|
|
38
|
+
value: SerializedValue;
|
|
39
|
+
}[];
|
|
40
|
+
};
|
|
41
|
+
export declare type SerializedSet = {
|
|
42
|
+
type: 'set';
|
|
43
|
+
value: SerializedValue[];
|
|
44
|
+
};
|
|
45
|
+
export declare type SerializedNull = {
|
|
46
|
+
type: 'null';
|
|
47
|
+
};
|
|
48
|
+
export declare type SerializedValue = SerializedArray | SerializedBigint | SerializedBoolean | SerializedDate | SerializedMap | SerializedNull | SerializedBuffer | SerializedNumber | SerializedObject | SerializedSet | SerializedString;
|
|
49
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,oBAAY,gBAAgB,GAAG;IAC9B,IAAI,EAAE,QAAQ,CAAC;IACf,KAAK,EAAE,MAAM,CAAC;CACd,CAAC;AAEF,oBAAY,gBAAgB,GAAG;IAC9B,IAAI,EAAE,QAAQ,CAAC;IACf,KAAK,EAAE,MAAM,CAAC;CACd,CAAC;AAEF,oBAAY,iBAAiB,GAAG;IAC/B,IAAI,EAAE,SAAS,CAAC;IAChB,KAAK,EAAE,OAAO,CAAC;CACf,CAAC;AAEF,oBAAY,gBAAgB,GAAG;IAC9B,IAAI,EAAE,QAAQ,CAAC;IACf,KAAK,EAAE,MAAM,CAAC;CACd,CAAC;AAEF,oBAAY,cAAc,GAAG;IAC5B,IAAI,EAAE,MAAM,CAAC;IACb,KAAK,EAAE,MAAM,CAAC;CACd,CAAC;AAEF,oBAAY,eAAe,GAAG;IAC7B,IAAI,EAAE,OAAO,CAAC;IACd,KAAK,EAAE,eAAe,EAAE,CAAC;CACzB,CAAC;AAEF,oBAAY,gBAAgB,GAAG;IAC9B,IAAI,EAAE,QAAQ,CAAC;IACf,KAAK,EAAE,MAAM,CAAC;CACd,CAAC;AAEF,oBAAY,gBAAgB,GAAG;IAC9B,IAAI,EAAE,QAAQ,CAAC;IACf,iBAAiB,EAAE,MAAM,EAAE,CAAC;IAC5B,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,eAAe,CAAC,CAAC;CACvC,CAAC;AAEF,oBAAY,aAAa,GAAG;IAC3B,IAAI,EAAE,KAAK,CAAC;IACZ,KAAK,EAAE;QACN,GAAG,EAAE,eAAe,CAAC;QACrB,KAAK,EAAE,eAAe,CAAC;KACvB,EAAE,CAAC;CACJ,CAAC;AAEF,oBAAY,aAAa,GAAG;IAC3B,IAAI,EAAE,KAAK,CAAC;IACZ,KAAK,EAAE,eAAe,EAAE,CAAC;CACzB,CAAC;AAEF,oBAAY,cAAc,GAAG;IAC5B,IAAI,EAAE,MAAM,CAAC;CACb,CAAC;AAGF,oBAAY,eAAe,GACxB,eAAe,GACf,gBAAgB,GAChB,iBAAiB,GACjB,cAAc,GACd,aAAa,GACb,cAAc,GACd,gBAAgB,GAChB,gBAAgB,GAChB,gBAAgB,GAChB,aAAa,GACb,gBAAgB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":""}
|
package/package.json
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@hg-ts/serialize",
|
|
3
|
+
"version": "0.0.15",
|
|
4
|
+
"main": "dist/index.js",
|
|
5
|
+
"module": "dist-esm/index.js",
|
|
6
|
+
"repository": "git@gitlab.com:hyper-graph/framework.git",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"prepack": "yarn build && yarn lint && yarn test",
|
|
9
|
+
"build": "hg build",
|
|
10
|
+
"build:dev": "hg build:dev",
|
|
11
|
+
"lint": "hg lint",
|
|
12
|
+
"lint:fix": "hg lint:fix",
|
|
13
|
+
"test": "hg test"
|
|
14
|
+
},
|
|
15
|
+
"devDependencies": {
|
|
16
|
+
"@hg-ts-config/eslint-config": "0.0.15",
|
|
17
|
+
"@hg-ts-config/typescript": "0.0.15",
|
|
18
|
+
"@hg-ts/cli": "0.0.15",
|
|
19
|
+
"@hg-ts/exception": "0.0.15",
|
|
20
|
+
"@hg-ts/tests": "0.0.15",
|
|
21
|
+
"@hg-ts/types": "0.0.15",
|
|
22
|
+
"@types/node": "18.7.6",
|
|
23
|
+
"@typescript-eslint/eslint-plugin": "5.33.1",
|
|
24
|
+
"@typescript-eslint/parser": "5.33.1",
|
|
25
|
+
"eslint": "8.24.0",
|
|
26
|
+
"eslint-formatter-codeframe": "7.32.1",
|
|
27
|
+
"mocha": "10.0.0",
|
|
28
|
+
"mocha-junit-reporter": "2.0.2",
|
|
29
|
+
"reflect-metadata": "0.1.13",
|
|
30
|
+
"tsc-watch": "5.0.3",
|
|
31
|
+
"tslib": "2.4.0",
|
|
32
|
+
"typescript": "4.7.4"
|
|
33
|
+
},
|
|
34
|
+
"peerDependencies": {
|
|
35
|
+
"@hg-ts/exception": "0.0.15",
|
|
36
|
+
"reflect-metadata": "*",
|
|
37
|
+
"tslib": "*"
|
|
38
|
+
}
|
|
39
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { DeserializeException, DeserializeUnknownPrototypeException } from './exceptions';
|
|
2
|
+
import type {
|
|
3
|
+
SerializedObject,
|
|
4
|
+
SerializedValue,
|
|
5
|
+
} from './types';
|
|
6
|
+
import { classes } from './additional-classes';
|
|
7
|
+
|
|
8
|
+
function deserializeObject(
|
|
9
|
+
{ value, prototypeNameList }: SerializedObject,
|
|
10
|
+
): unknown {
|
|
11
|
+
let targetClass: Class = Object;
|
|
12
|
+
const [preferredClassName = null] = prototypeNameList.filter(name => name !== Object.name);
|
|
13
|
+
|
|
14
|
+
if (preferredClassName) {
|
|
15
|
+
const foundCtor = classes.get(preferredClassName);
|
|
16
|
+
|
|
17
|
+
if (!foundCtor) {
|
|
18
|
+
throw new DeserializeUnknownPrototypeException(preferredClassName);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
targetClass = foundCtor;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const result: Record<string, unknown> = Object.create(targetClass.prototype);
|
|
25
|
+
|
|
26
|
+
return Object.keys(value)
|
|
27
|
+
.reduce<Record<string, unknown>>((result, key) => {
|
|
28
|
+
result[key] = deserializeValue(value[key]!);
|
|
29
|
+
|
|
30
|
+
return result;
|
|
31
|
+
}, result);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function deserializeValue(value: SerializedValue): unknown {
|
|
35
|
+
switch (value.type) {
|
|
36
|
+
case 'boolean':
|
|
37
|
+
case 'string':
|
|
38
|
+
case 'number':
|
|
39
|
+
return value.value;
|
|
40
|
+
case 'buffer':
|
|
41
|
+
return Buffer.from(value.value, 'hex');
|
|
42
|
+
case 'bigint':
|
|
43
|
+
return BigInt(value.value);
|
|
44
|
+
case 'null':
|
|
45
|
+
return null;
|
|
46
|
+
case 'date':
|
|
47
|
+
return new Date(value.value);
|
|
48
|
+
case 'array':
|
|
49
|
+
return value.value.map(item => deserializeValue(item));
|
|
50
|
+
case 'set':
|
|
51
|
+
return new Set(value.value.map(item => deserializeValue(item)));
|
|
52
|
+
case 'map':
|
|
53
|
+
return new Map(value.value.map(item => [
|
|
54
|
+
deserializeValue(item.key),
|
|
55
|
+
deserializeValue(item.value),
|
|
56
|
+
]));
|
|
57
|
+
case 'object':
|
|
58
|
+
return deserializeObject(value);
|
|
59
|
+
|
|
60
|
+
default: {
|
|
61
|
+
const error: never = value;
|
|
62
|
+
throw new DeserializeException(error);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export function deserialize(value: SerializedValue): unknown {
|
|
68
|
+
return deserializeValue(value);
|
|
69
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { BaseException } from '@hg-ts/exception';
|
|
2
|
+
|
|
3
|
+
import type { SerializedValue } from '../types';
|
|
4
|
+
|
|
5
|
+
export class DeserializeException extends BaseException {
|
|
6
|
+
public constructor(value: SerializedValue) {
|
|
7
|
+
super(`Error for deserialize value: ${JSON.stringify(value)}`);
|
|
8
|
+
}
|
|
9
|
+
}
|
package/src/index.ts
ADDED
package/src/serialize.ts
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
SerializedArray,
|
|
3
|
+
SerializedObject,
|
|
4
|
+
SerializedValue,
|
|
5
|
+
} from './types';
|
|
6
|
+
import { SerializeException } from './exceptions';
|
|
7
|
+
|
|
8
|
+
export function getPrototypeListNames(value: Record<string | symbol, unknown>): string[] {
|
|
9
|
+
const result: string[] = [];
|
|
10
|
+
let nextPrototype = Object.getPrototypeOf(value);
|
|
11
|
+
|
|
12
|
+
while (nextPrototype !== null) {
|
|
13
|
+
result.push(nextPrototype.constructor.name);
|
|
14
|
+
|
|
15
|
+
nextPrototype = Object.getPrototypeOf(nextPrototype);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
return result;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function serializeArray(value: unknown[]): SerializedArray {
|
|
22
|
+
return {
|
|
23
|
+
type: 'array',
|
|
24
|
+
value: value.map(item => serializeValue(item)),
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function serializeRecord(value: Record<string | symbol, unknown>): SerializedObject {
|
|
29
|
+
const fields = Object.keys(value)
|
|
30
|
+
.reduce<Record<string, SerializedValue>>((result, key) => {
|
|
31
|
+
if (typeof key === 'symbol') {
|
|
32
|
+
throw new SerializeException('symbol key');
|
|
33
|
+
}
|
|
34
|
+
result[key] = serializeValue(value[key]);
|
|
35
|
+
return result;
|
|
36
|
+
}, {});
|
|
37
|
+
|
|
38
|
+
return {
|
|
39
|
+
type: 'object',
|
|
40
|
+
prototypeNameList: getPrototypeListNames(value),
|
|
41
|
+
value: fields,
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function serializeObject(value: object | null): SerializedValue {
|
|
46
|
+
if (value === null) {
|
|
47
|
+
return { type: 'null' };
|
|
48
|
+
}
|
|
49
|
+
if (value instanceof Date) {
|
|
50
|
+
return {
|
|
51
|
+
type: 'date',
|
|
52
|
+
value: value.getTime(),
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
if (value instanceof Buffer) {
|
|
56
|
+
return {
|
|
57
|
+
type: 'buffer',
|
|
58
|
+
value: value.toString('hex'),
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
if (value instanceof Map) {
|
|
62
|
+
return {
|
|
63
|
+
type: 'map',
|
|
64
|
+
value: [...value.entries()].map(([key, value]) => ({
|
|
65
|
+
key: serializeValue(key),
|
|
66
|
+
value: serializeValue(value),
|
|
67
|
+
})),
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
if (value instanceof Set) {
|
|
71
|
+
return {
|
|
72
|
+
type: 'set',
|
|
73
|
+
value: [...value.values()].map(item => serializeValue(item)),
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
if (Array.isArray(value)) {
|
|
77
|
+
return serializeArray(value);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
return serializeRecord(value as any);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function serializeValue(value: unknown): SerializedValue {
|
|
84
|
+
switch (typeof value) {
|
|
85
|
+
case 'boolean':
|
|
86
|
+
return {
|
|
87
|
+
value,
|
|
88
|
+
type: 'boolean',
|
|
89
|
+
};
|
|
90
|
+
case 'bigint':
|
|
91
|
+
return {
|
|
92
|
+
type: 'bigint',
|
|
93
|
+
value: String(value),
|
|
94
|
+
};
|
|
95
|
+
case 'string':
|
|
96
|
+
return {
|
|
97
|
+
value,
|
|
98
|
+
type: 'string',
|
|
99
|
+
};
|
|
100
|
+
case 'number':
|
|
101
|
+
return {
|
|
102
|
+
value,
|
|
103
|
+
type: 'number',
|
|
104
|
+
};
|
|
105
|
+
case 'object':
|
|
106
|
+
return serializeObject(value as any);
|
|
107
|
+
default:
|
|
108
|
+
throw new SerializeException(typeof value);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
export function serialize(value: unknown): SerializedValue {
|
|
113
|
+
return serializeValue(value);
|
|
114
|
+
}
|