@tsed/json-mapper 7.34.1 → 7.35.0-beta.1
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/jest.config.js +3 -3
- package/lib/browser/json-mapper.umd.min.js +1 -1
- package/lib/browser/json-mapper.umd.min.js.map +1 -1
- package/lib/cjs/components/ArrayMapper.js.map +1 -1
- package/lib/cjs/components/DateMapper.js +1 -1
- package/lib/cjs/components/DateMapper.js.map +1 -1
- package/lib/cjs/components/MapMapper.js.map +1 -1
- package/lib/cjs/components/PrimitiveMapper.js.map +1 -1
- package/lib/cjs/components/SetMapper.js.map +1 -1
- package/lib/cjs/components/SymbolMapper.js.map +1 -1
- package/lib/cjs/domain/JsonMapperCompiler.js +156 -0
- package/lib/cjs/domain/JsonMapperCompiler.js.map +1 -0
- package/lib/cjs/domain/JsonSerializer.js +214 -0
- package/lib/cjs/domain/JsonSerializer.js.map +1 -0
- package/lib/cjs/domain/JsonSerializerOptions.js +3 -0
- package/lib/cjs/domain/JsonSerializerOptions.js.map +1 -0
- package/lib/cjs/domain/Writer.js +111 -0
- package/lib/cjs/domain/Writer.js.map +1 -0
- package/lib/cjs/hooks/alterOnSerialize.js +8 -0
- package/lib/cjs/hooks/alterOnSerialize.js.map +1 -0
- package/lib/cjs/index.js +6 -0
- package/lib/cjs/index.js.map +1 -1
- package/lib/cjs/utils/getObjectProperties.js +9 -0
- package/lib/cjs/utils/getObjectProperties.js.map +1 -0
- package/lib/cjs/utils/serialize.js +5 -143
- package/lib/cjs/utils/serialize.js.map +1 -1
- package/lib/esm/components/ArrayMapper.js.map +1 -1
- package/lib/esm/components/DateMapper.js +1 -1
- package/lib/esm/components/DateMapper.js.map +1 -1
- package/lib/esm/components/MapMapper.js.map +1 -1
- package/lib/esm/components/PrimitiveMapper.js.map +1 -1
- package/lib/esm/components/SetMapper.js.map +1 -1
- package/lib/esm/components/SymbolMapper.js.map +1 -1
- package/lib/esm/domain/JsonMapperCompiler.js +152 -0
- package/lib/esm/domain/JsonMapperCompiler.js.map +1 -0
- package/lib/esm/domain/JsonSerializer.js +210 -0
- package/lib/esm/domain/JsonSerializer.js.map +1 -0
- package/lib/esm/domain/JsonSerializerOptions.js +2 -0
- package/lib/esm/domain/JsonSerializerOptions.js.map +1 -0
- package/lib/esm/domain/Writer.js +108 -0
- package/lib/esm/domain/Writer.js.map +1 -0
- package/lib/esm/hooks/alterOnSerialize.js +4 -0
- package/lib/esm/hooks/alterOnSerialize.js.map +1 -0
- package/lib/esm/index.js +6 -0
- package/lib/esm/index.js.map +1 -1
- package/lib/esm/utils/getObjectProperties.js +5 -0
- package/lib/esm/utils/getObjectProperties.js.map +1 -0
- package/lib/esm/utils/serialize.js +4 -141
- package/lib/esm/utils/serialize.js.map +1 -1
- package/lib/tsconfig.esm.tsbuildinfo +1 -1
- package/lib/tsconfig.tsbuildinfo +1 -1
- package/lib/types/domain/JsonMapperCompiler.d.ts +52 -0
- package/lib/types/domain/JsonSerializer.d.ts +20 -0
- package/lib/types/domain/JsonSerializerOptions.d.ts +13 -0
- package/lib/types/domain/Writer.d.ts +34 -0
- package/lib/types/hooks/alterOnSerialize.d.ts +2 -0
- package/lib/types/index.d.ts +6 -0
- package/lib/types/utils/getObjectProperties.d.ts +1 -0
- package/lib/types/utils/serialize.d.ts +2 -18
- package/package.json +11 -8
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
class Writer {
|
|
2
|
+
static object = {
|
|
3
|
+
assign(...args) {
|
|
4
|
+
return `{ ${args.filter(Boolean).join(", ")} }`;
|
|
5
|
+
}
|
|
6
|
+
};
|
|
7
|
+
_root;
|
|
8
|
+
body = [];
|
|
9
|
+
_indent;
|
|
10
|
+
constructor(root) {
|
|
11
|
+
this._root = root;
|
|
12
|
+
}
|
|
13
|
+
static indent(lines) {
|
|
14
|
+
return lines.map((p) => `\t${p}`);
|
|
15
|
+
}
|
|
16
|
+
static mapper(mapperId, key, options) {
|
|
17
|
+
return `execMapper('${mapperId}', ${key}, ${options})`;
|
|
18
|
+
}
|
|
19
|
+
static mapperFrom(key, options) {
|
|
20
|
+
return `execMapper(nameOf(classOf(${key})), ${key}, ${options})`;
|
|
21
|
+
}
|
|
22
|
+
static options(...args) {
|
|
23
|
+
args = args.filter(Boolean);
|
|
24
|
+
return !args.length ? "options" : Writer.object.assign("...options", ...args);
|
|
25
|
+
}
|
|
26
|
+
add(...lines) {
|
|
27
|
+
lines = lines.filter((line) => line !== undefined);
|
|
28
|
+
lines.length && this.body.push(...lines);
|
|
29
|
+
return this;
|
|
30
|
+
}
|
|
31
|
+
arrow(...args) {
|
|
32
|
+
this.add(`(${args}) => {`);
|
|
33
|
+
const writer = this.new();
|
|
34
|
+
this.add("}");
|
|
35
|
+
return writer;
|
|
36
|
+
}
|
|
37
|
+
build() {
|
|
38
|
+
const result = this.body.flatMap((line) => {
|
|
39
|
+
return line instanceof Writer ? line.build() : line;
|
|
40
|
+
});
|
|
41
|
+
return this._indent ? result.map((item) => "\t" + item) : result;
|
|
42
|
+
}
|
|
43
|
+
callMapper(id, key, ...options) {
|
|
44
|
+
return this.set(key, Writer.mapper(id, key, Writer.options(...options)));
|
|
45
|
+
}
|
|
46
|
+
const(name, line) {
|
|
47
|
+
this.add(`const ${name} = ${line};`);
|
|
48
|
+
return this;
|
|
49
|
+
}
|
|
50
|
+
each(iterable, args = []) {
|
|
51
|
+
const writer = this.add(iterable + ".forEach((" + args.join(", ") + ") => {").new();
|
|
52
|
+
this.add("});");
|
|
53
|
+
return writer;
|
|
54
|
+
}
|
|
55
|
+
if(condition) {
|
|
56
|
+
const writer = new IfWriter(condition, this);
|
|
57
|
+
this.add(writer);
|
|
58
|
+
return writer;
|
|
59
|
+
}
|
|
60
|
+
indent(indent) {
|
|
61
|
+
this._indent = indent;
|
|
62
|
+
return this;
|
|
63
|
+
}
|
|
64
|
+
new(indent = true) {
|
|
65
|
+
const writer = new Writer(this.root());
|
|
66
|
+
writer.indent(indent);
|
|
67
|
+
this.add(writer);
|
|
68
|
+
return writer;
|
|
69
|
+
}
|
|
70
|
+
return(line) {
|
|
71
|
+
this.add(`return ${line};`);
|
|
72
|
+
return this.root();
|
|
73
|
+
}
|
|
74
|
+
root() {
|
|
75
|
+
return this._root || this;
|
|
76
|
+
}
|
|
77
|
+
set(left, right) {
|
|
78
|
+
this.add(`${left} = ${right};`);
|
|
79
|
+
return this;
|
|
80
|
+
}
|
|
81
|
+
toString() {
|
|
82
|
+
return this.build().join("\n");
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
export { Writer };
|
|
86
|
+
class IfWriter extends Writer {
|
|
87
|
+
condition;
|
|
88
|
+
elseWriter;
|
|
89
|
+
constructor(condition, root) {
|
|
90
|
+
super();
|
|
91
|
+
this.condition = condition;
|
|
92
|
+
this._root = root;
|
|
93
|
+
}
|
|
94
|
+
else() {
|
|
95
|
+
const writer = new Writer(this._root);
|
|
96
|
+
this.elseWriter = writer;
|
|
97
|
+
return writer;
|
|
98
|
+
}
|
|
99
|
+
build() {
|
|
100
|
+
return [
|
|
101
|
+
`if (${this.condition}) {`,
|
|
102
|
+
...Writer.indent(super.build()),
|
|
103
|
+
"}",
|
|
104
|
+
this.elseWriter ? [`else {`, ...Writer.indent(this.elseWriter.build()), "}"] : []
|
|
105
|
+
].flat();
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
//# sourceMappingURL=Writer.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Writer.js","sourceRoot":"","sources":["../../../src/domain/Writer.ts"],"names":[],"mappings":"AAAA,MAAa,MAAM;IACjB,MAAM,CAAC,MAAM,GAAG;QACd,MAAM,CAAC,GAAG,IAAc;YACtB,OAAO,KAAK,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC;QAClD,CAAC;KACF,CAAC;IAEQ,KAAK,CAAU;IACf,IAAI,GAAwB,EAAE,CAAC;IACjC,OAAO,CAAU;IAE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AM,CAAC,MAAM,CAAC,KAAK,CAAC,KAAK,EAAE,CAAC;YAC/B,GAAG;YACH,IAAI,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,QAAQ,EAAE,GAAG,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,UAAU,CAAC,KAAK,EAAE,CAAC,EAAE,GAAG,CAAC,CAAC,CAAC,CAAC,EAAE;SAClF,CAAC,IAAI,EAAE,CAAC;IACX,CAAC;CACF"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"alterOnSerialize.js","sourceRoot":"","sources":["../../../src/hooks/alterOnSerialize.ts"],"names":[],"mappings":"AAEA,MAAM,UAAU,gBAAgB,CAAC,MAAkB,EAAE,KAAU,EAAE,OAAwB;IACvF,OAAO,MAAM,CAAC,MAAM,CAAC,KAAK,CAAC,aAAa,EAAE,KAAK,EAAE,CAAC,OAAO,CAAC,CAAC,CAAC;AAC9D,CAAC"}
|
package/lib/esm/index.js
CHANGED
|
@@ -12,12 +12,18 @@ export * from "./decorators/beforeDeserialize.js";
|
|
|
12
12
|
export * from "./decorators/jsonMapper.js";
|
|
13
13
|
export * from "./decorators/onDeserialize.js";
|
|
14
14
|
export * from "./decorators/onSerialize.js";
|
|
15
|
+
export * from "./domain/JsonMapperCompiler.js";
|
|
15
16
|
export * from "./domain/JsonMapperContext.js";
|
|
16
17
|
export * from "./domain/JsonMapperSettings.js";
|
|
17
18
|
export * from "./domain/JsonMapperTypesContainer.js";
|
|
19
|
+
export * from "./domain/JsonSerializer.js";
|
|
20
|
+
export * from "./domain/JsonSerializerOptions.js";
|
|
21
|
+
export * from "./domain/Writer.js";
|
|
18
22
|
export * from "./hooks/alterAfterDeserialize.js";
|
|
19
23
|
export * from "./hooks/alterBeforeDeserialize.js";
|
|
24
|
+
export * from "./hooks/alterOnSerialize.js";
|
|
20
25
|
export * from "./interfaces/JsonMapperMethods.js";
|
|
21
26
|
export * from "./utils/deserialize.js";
|
|
27
|
+
export * from "./utils/getObjectProperties.js";
|
|
22
28
|
export * from "./utils/serialize.js";
|
|
23
29
|
//# sourceMappingURL=index.js.map
|
package/lib/esm/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,cAAc,0BAA0B,CAAC;AACzC,cAAc,yBAAyB,CAAC;AACxC,cAAc,wBAAwB,CAAC;AACvC,cAAc,8BAA8B,CAAC;AAC7C,cAAc,wBAAwB,CAAC;AACvC,cAAc,2BAA2B,CAAC;AAC1C,cAAc,+BAA+B,CAAC;AAC9C,cAAc,gCAAgC,CAAC;AAC/C,cAAc,yBAAyB,CAAC;AACxC,cAAc,4BAA4B,CAAC;AAC3C,cAAc,0BAA0B,CAAC;AACzC,cAAc,4BAA4B,CAAC;AAC3C,cAAc,6BAA6B,CAAC;AAC5C,cAAc,mCAAmC,CAAC;AAClD,cAAc,+BAA+B,CAAC;AAC9C,cAAc,gCAAgC,CAAC;AAC/C,cAAc,gCAAgC,CAAC;AAC/C,cAAc,qBAAqB,CAAC;AACpC,cAAc,mBAAmB,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,cAAc,0BAA0B,CAAC;AACzC,cAAc,yBAAyB,CAAC;AACxC,cAAc,wBAAwB,CAAC;AACvC,cAAc,8BAA8B,CAAC;AAC7C,cAAc,wBAAwB,CAAC;AACvC,cAAc,2BAA2B,CAAC;AAC1C,cAAc,+BAA+B,CAAC;AAC9C,cAAc,gCAAgC,CAAC;AAC/C,cAAc,yBAAyB,CAAC;AACxC,cAAc,4BAA4B,CAAC;AAC3C,cAAc,0BAA0B,CAAC;AACzC,cAAc,6BAA6B,CAAC;AAC5C,cAAc,4BAA4B,CAAC;AAC3C,cAAc,6BAA6B,CAAC;AAC5C,cAAc,mCAAmC,CAAC;AAClD,cAAc,yBAAyB,CAAC;AACxC,cAAc,gCAAgC,CAAC;AAC/C,cAAc,iBAAiB,CAAC;AAChC,cAAc,+BAA+B,CAAC;AAC9C,cAAc,gCAAgC,CAAC;AAC/C,cAAc,0BAA0B,CAAC;AACzC,cAAc,gCAAgC,CAAC;AAC/C,cAAc,qBAAqB,CAAC;AACpC,cAAc,6BAA6B,CAAC;AAC5C,cAAc,mBAAmB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"getObjectProperties.js","sourceRoot":"","sources":["../../../src/utils/getObjectProperties.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,UAAU,EAAC,MAAM,YAAY,CAAC;AAEtC,MAAM,UAAU,mBAAmB,CAAC,GAAQ;IAC1C,OAAO,MAAM,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,KAAK,CAAC,EAAE,EAAE,CAAC,CAAC,UAAU,CAAC,KAAK,CAAC,CAAC,CAAC;AACvE,CAAC"}
|
|
@@ -1,146 +1,9 @@
|
|
|
1
|
-
import { classOf, isArray, isClassObject, isCollection, isDate, isEmpty, isFunction, isNil, nameOf, objectKeys } from "@tsed/core";
|
|
2
|
-
import { alterIgnore, getPropertiesStores, JsonEntityStore } from "@tsed/schema";
|
|
3
|
-
import "../components/ArrayMapper.js";
|
|
4
1
|
import "../components/DateMapper.js";
|
|
5
|
-
import "../components/MapMapper.js";
|
|
6
2
|
import "../components/PrimitiveMapper.js";
|
|
7
|
-
import "../components/SetMapper.js";
|
|
8
3
|
import "../components/SymbolMapper.js";
|
|
9
|
-
import {
|
|
10
|
-
|
|
11
|
-
function
|
|
12
|
-
return
|
|
13
|
-
}
|
|
14
|
-
function getSchemaProperties(storedJson, obj) {
|
|
15
|
-
const stores = Array.from(getPropertiesStores(storedJson).entries());
|
|
16
|
-
if (!stores.length) {
|
|
17
|
-
// fallback to auto discovering field from obj
|
|
18
|
-
objectKeys(obj).forEach((key) => {
|
|
19
|
-
const propStore = JsonEntityStore.from(storedJson.target, key);
|
|
20
|
-
stores.push([key, propStore]);
|
|
21
|
-
});
|
|
22
|
-
}
|
|
23
|
-
return stores;
|
|
24
|
-
}
|
|
25
|
-
function getObjectProperties(obj) {
|
|
26
|
-
return Object.entries(obj).filter(([, value]) => !isFunction(value));
|
|
27
|
-
}
|
|
28
|
-
function getType(propStore, value) {
|
|
29
|
-
if (isNil(value) || propStore.type === Object || isCollection(propStore.type)) {
|
|
30
|
-
return undefined;
|
|
31
|
-
}
|
|
32
|
-
return propStore.type;
|
|
33
|
-
}
|
|
34
|
-
export function classToPlainObject(obj, options) {
|
|
35
|
-
const { useAlias = true, type, ...props } = options;
|
|
36
|
-
const entity = JsonEntityStore.from(type || obj);
|
|
37
|
-
const additionalProperties = !!entity.schema.get("additionalProperties");
|
|
38
|
-
const schemaProperties = getSchemaProperties(entity, obj);
|
|
39
|
-
const properties = new Set();
|
|
40
|
-
const out = schemaProperties.reduce((newObj, [key, propStore]) => {
|
|
41
|
-
properties.add(key);
|
|
42
|
-
const schema = propStore.schema;
|
|
43
|
-
if (alterIgnore(schema, { useAlias, ...props, self: obj })) {
|
|
44
|
-
return newObj;
|
|
45
|
-
}
|
|
46
|
-
let value = alterValue(schema, obj[key], { useAlias, ...props, self: obj });
|
|
47
|
-
value = serialize(value, {
|
|
48
|
-
useAlias,
|
|
49
|
-
self: obj,
|
|
50
|
-
type: value === obj[key] ? getType(propStore, value) : undefined,
|
|
51
|
-
collectionType: propStore.collectionType,
|
|
52
|
-
format: propStore.itemSchema.get("format"),
|
|
53
|
-
...props
|
|
54
|
-
});
|
|
55
|
-
if (value === undefined) {
|
|
56
|
-
return newObj;
|
|
57
|
-
}
|
|
58
|
-
key = useAlias ? propStore.parent.schema.getAliasOf(key) || key : key;
|
|
59
|
-
return {
|
|
60
|
-
...newObj,
|
|
61
|
-
[key]: value
|
|
62
|
-
};
|
|
63
|
-
}, {});
|
|
64
|
-
if (entity.discriminatorAncestor) {
|
|
65
|
-
const discriminator = entity.discriminatorAncestor.schema.discriminator();
|
|
66
|
-
if (!out[discriminator.propertyName]) {
|
|
67
|
-
out[discriminator.propertyName] = discriminator.getDefaultValue(entity.target);
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
if (additionalProperties) {
|
|
71
|
-
objectKeys(obj).forEach((key) => {
|
|
72
|
-
if (!properties.has(key)) {
|
|
73
|
-
out[key] = obj[key];
|
|
74
|
-
}
|
|
75
|
-
});
|
|
76
|
-
}
|
|
77
|
-
return out;
|
|
78
|
-
}
|
|
79
|
-
function toObject(obj, options) {
|
|
80
|
-
return getObjectProperties(obj).reduce((newObj, [key, value]) => ({
|
|
81
|
-
...newObj,
|
|
82
|
-
[key]: serialize(value, options)
|
|
83
|
-
}), {});
|
|
84
|
-
}
|
|
85
|
-
function getBestType(type, obj) {
|
|
86
|
-
const dataType = classOf(obj);
|
|
87
|
-
if (dataType && !isClassObject(dataType)) {
|
|
88
|
-
return dataType;
|
|
89
|
-
}
|
|
90
|
-
return type || Object;
|
|
91
|
-
}
|
|
92
|
-
function hasJsonMethod(obj) {
|
|
93
|
-
return typeof obj.toJSON === "function";
|
|
94
|
-
}
|
|
95
|
-
function isMongooseObject(obj) {
|
|
96
|
-
return (hasJsonMethod(obj) && obj.$isMongooseModelPrototype) || obj._bsontype;
|
|
97
|
-
}
|
|
98
|
-
function isMomentObject(obj) {
|
|
99
|
-
return obj && obj?._isAMomentObject;
|
|
100
|
-
}
|
|
101
|
-
export function serialize(obj, { type, collectionType, groups = false, ...options } = {}) {
|
|
102
|
-
if (isEmpty(obj)) {
|
|
103
|
-
return obj;
|
|
104
|
-
}
|
|
105
|
-
let currentType = type;
|
|
106
|
-
const types = options.types ? options.types : getJsonMapperTypes();
|
|
107
|
-
options.groups = groups;
|
|
108
|
-
// FIX custom serialization function from @tsed/mongoose and bson
|
|
109
|
-
if (isMongooseObject(obj)) {
|
|
110
|
-
return obj.toJSON(options);
|
|
111
|
-
}
|
|
112
|
-
if (hasJsonMethod(obj) && !isMomentObject(obj) && !isDate(obj)) {
|
|
113
|
-
return serialize(obj.toJSON(), { ...options, type: classOf(obj) });
|
|
114
|
-
}
|
|
115
|
-
if (!isCollection(obj)) {
|
|
116
|
-
options.type = currentType = getBestType(currentType, obj);
|
|
117
|
-
}
|
|
118
|
-
else if (!options.collectionType) {
|
|
119
|
-
currentType = classOf(obj);
|
|
120
|
-
options.type = type;
|
|
121
|
-
options.collectionType = currentType;
|
|
122
|
-
}
|
|
123
|
-
const context = new JsonMapperContext({
|
|
124
|
-
type: currentType,
|
|
125
|
-
options,
|
|
126
|
-
next: (data) => serialize(data, {
|
|
127
|
-
...options,
|
|
128
|
-
collectionType: undefined,
|
|
129
|
-
type: options.type
|
|
130
|
-
})
|
|
131
|
-
});
|
|
132
|
-
const mapper = types.get(currentType) || types.get(nameOf(currentType));
|
|
133
|
-
if (mapper) {
|
|
134
|
-
const jsonMapper = mapper;
|
|
135
|
-
return jsonMapper.serialize(obj, context);
|
|
136
|
-
}
|
|
137
|
-
else if (obj?._isAMomentObject) {
|
|
138
|
-
return obj.toJSON();
|
|
139
|
-
}
|
|
140
|
-
if (isArray(obj)) {
|
|
141
|
-
// Serialize Array class like
|
|
142
|
-
return types.get(Array)?.serialize(obj, context);
|
|
143
|
-
}
|
|
144
|
-
return !isClassObject(currentType) ? classToPlainObject(obj, options) : toObject(obj, options);
|
|
4
|
+
import { JsonSerializer } from "../domain/JsonSerializer.js";
|
|
5
|
+
const serializer = new JsonSerializer();
|
|
6
|
+
export function serialize(input, options) {
|
|
7
|
+
return serializer.map(input, options);
|
|
145
8
|
}
|
|
146
9
|
//# sourceMappingURL=serialize.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"serialize.js","sourceRoot":"","sources":["../../../src/utils/serialize.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"serialize.js","sourceRoot":"","sources":["../../../src/utils/serialize.ts"],"names":[],"mappings":"AAAA,OAAO,0BAA0B,CAAC;AAClC,OAAO,+BAA+B,CAAC;AACvC,OAAO,4BAA4B,CAAC;AAEpC,OAAO,EAAC,cAAc,EAAC,MAAM,0BAA0B,CAAC;AAGxD,MAAM,UAAU,GAAG,IAAI,cAAc,EAAE,CAAC;AAExC,MAAM,UAAU,SAAS,CAAC,KAAU,EAAE,OAA+B;IACnE,OAAO,UAAU,CAAC,GAAG,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC;AACxC,CAAC"}
|