1 | /*
|
---|
2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
---|
3 | */
|
---|
4 |
|
---|
5 | "use strict";
|
---|
6 |
|
---|
7 | /** @typedef {import("./ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
|
---|
8 | /** @typedef {import("./ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
|
---|
9 |
|
---|
10 | /** @typedef {(arg0?: any) => void} CacheAssoc */
|
---|
11 |
|
---|
12 | /**
|
---|
13 | * @template T
|
---|
14 | * @typedef {WeakMap<CacheAssoc, ObjectStructure<T>>}
|
---|
15 | */
|
---|
16 | const cache = new WeakMap();
|
---|
17 |
|
---|
18 | /**
|
---|
19 | * @template T
|
---|
20 | */
|
---|
21 | class ObjectStructure {
|
---|
22 | constructor() {
|
---|
23 | this.keys = undefined;
|
---|
24 | this.children = undefined;
|
---|
25 | }
|
---|
26 |
|
---|
27 | /**
|
---|
28 | * @param {keyof T[]} keys keys
|
---|
29 | * @returns {keyof T[]} keys
|
---|
30 | */
|
---|
31 | getKeys(keys) {
|
---|
32 | if (this.keys === undefined) this.keys = keys;
|
---|
33 | return this.keys;
|
---|
34 | }
|
---|
35 |
|
---|
36 | /**
|
---|
37 | * @param {keyof T} key key
|
---|
38 | * @returns {ObjectStructure<T>} object structure
|
---|
39 | */
|
---|
40 | key(key) {
|
---|
41 | if (this.children === undefined) this.children = new Map();
|
---|
42 | const child = this.children.get(key);
|
---|
43 | if (child !== undefined) return child;
|
---|
44 | const newChild = new ObjectStructure();
|
---|
45 | this.children.set(key, newChild);
|
---|
46 | return newChild;
|
---|
47 | }
|
---|
48 | }
|
---|
49 |
|
---|
50 | /**
|
---|
51 | * @template T
|
---|
52 | * @param {(keyof T)[]} keys keys
|
---|
53 | * @param {CacheAssoc} cacheAssoc cache assoc fn
|
---|
54 | * @returns {(keyof T)[]} keys
|
---|
55 | */
|
---|
56 | const getCachedKeys = (keys, cacheAssoc) => {
|
---|
57 | let root = cache.get(cacheAssoc);
|
---|
58 | if (root === undefined) {
|
---|
59 | root = new ObjectStructure();
|
---|
60 | cache.set(cacheAssoc, root);
|
---|
61 | }
|
---|
62 | let current = root;
|
---|
63 | for (const key of keys) {
|
---|
64 | current = current.key(key);
|
---|
65 | }
|
---|
66 | return current.getKeys(keys);
|
---|
67 | };
|
---|
68 |
|
---|
69 | class PlainObjectSerializer {
|
---|
70 | /**
|
---|
71 | * @template {object} T
|
---|
72 | * @param {T} obj plain object
|
---|
73 | * @param {ObjectSerializerContext} context context
|
---|
74 | */
|
---|
75 | serialize(obj, context) {
|
---|
76 | const keys = /** @type {(keyof T)[]} */ (Object.keys(obj));
|
---|
77 | if (keys.length > 128) {
|
---|
78 | // Objects with so many keys are unlikely to share structure
|
---|
79 | // with other objects
|
---|
80 | context.write(keys);
|
---|
81 | for (const key of keys) {
|
---|
82 | context.write(obj[key]);
|
---|
83 | }
|
---|
84 | } else if (keys.length > 1) {
|
---|
85 | context.write(getCachedKeys(keys, context.write));
|
---|
86 | for (const key of keys) {
|
---|
87 | context.write(obj[key]);
|
---|
88 | }
|
---|
89 | } else if (keys.length === 1) {
|
---|
90 | const key = keys[0];
|
---|
91 | context.write(key);
|
---|
92 | context.write(obj[key]);
|
---|
93 | } else {
|
---|
94 | context.write(null);
|
---|
95 | }
|
---|
96 | }
|
---|
97 |
|
---|
98 | /**
|
---|
99 | * @template {object} T
|
---|
100 | * @param {ObjectDeserializerContext} context context
|
---|
101 | * @returns {T} plain object
|
---|
102 | */
|
---|
103 | deserialize(context) {
|
---|
104 | const keys = context.read();
|
---|
105 | const obj = /** @type {T} */ ({});
|
---|
106 | if (Array.isArray(keys)) {
|
---|
107 | for (const key of keys) {
|
---|
108 | obj[/** @type {keyof T} */ (key)] = context.read();
|
---|
109 | }
|
---|
110 | } else if (keys !== null) {
|
---|
111 | obj[/** @type {keyof T} */ (keys)] = context.read();
|
---|
112 | }
|
---|
113 | return obj;
|
---|
114 | }
|
---|
115 | }
|
---|
116 |
|
---|
117 | module.exports = PlainObjectSerializer;
|
---|