main
Last change
on this file since 79a0317 was 79a0317, checked in by stefan toskovski <stefantoska84@…>, 3 days ago |
F4 Finalna Verzija
|
-
Property mode
set to
100644
|
File size:
1.1 KB
|
Line | |
---|
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 | class MapObjectSerializer {
|
---|
11 | /**
|
---|
12 | * @template K, V
|
---|
13 | * @param {Map<K, V>} obj map
|
---|
14 | * @param {ObjectSerializerContext} context context
|
---|
15 | */
|
---|
16 | serialize(obj, context) {
|
---|
17 | context.write(obj.size);
|
---|
18 | for (const key of obj.keys()) {
|
---|
19 | context.write(key);
|
---|
20 | }
|
---|
21 | for (const value of obj.values()) {
|
---|
22 | context.write(value);
|
---|
23 | }
|
---|
24 | }
|
---|
25 |
|
---|
26 | /**
|
---|
27 | * @template K, V
|
---|
28 | * @param {ObjectDeserializerContext} context context
|
---|
29 | * @returns {Map<K, V>} map
|
---|
30 | */
|
---|
31 | deserialize(context) {
|
---|
32 | /** @type {number} */
|
---|
33 | const size = context.read();
|
---|
34 | /** @type {Map<K, V>} */
|
---|
35 | const map = new Map();
|
---|
36 | /** @type {K[]} */
|
---|
37 | const keys = [];
|
---|
38 | for (let i = 0; i < size; i++) {
|
---|
39 | keys.push(context.read());
|
---|
40 | }
|
---|
41 | for (let i = 0; i < size; i++) {
|
---|
42 | map.set(keys[i], context.read());
|
---|
43 | }
|
---|
44 | return map;
|
---|
45 | }
|
---|
46 | }
|
---|
47 |
|
---|
48 | module.exports = MapObjectSerializer;
|
---|
Note:
See
TracBrowser
for help on using the repository browser.