|
Last change
on this file since 9af201e was 9af201e, checked in by MBK <marija.karapandzova@…>, 12 days ago |
|
Fix frontend appearance
|
-
Property mode
set to
100644
|
|
File size:
1.2 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 | * Serializes this instance into the provided serializer context.
|
|---|
| 13 | * @template K, V
|
|---|
| 14 | * @param {Map<K, V>} obj map
|
|---|
| 15 | * @param {ObjectSerializerContext} context context
|
|---|
| 16 | */
|
|---|
| 17 | serialize(obj, context) {
|
|---|
| 18 | context.write(obj.size);
|
|---|
| 19 | for (const key of obj.keys()) {
|
|---|
| 20 | context.write(key);
|
|---|
| 21 | }
|
|---|
| 22 | for (const value of obj.values()) {
|
|---|
| 23 | context.write(value);
|
|---|
| 24 | }
|
|---|
| 25 | }
|
|---|
| 26 |
|
|---|
| 27 | /**
|
|---|
| 28 | * Restores this instance from the provided deserializer context.
|
|---|
| 29 | * @template K, V
|
|---|
| 30 | * @param {ObjectDeserializerContext} context context
|
|---|
| 31 | * @returns {Map<K, V>} map
|
|---|
| 32 | */
|
|---|
| 33 | deserialize(context) {
|
|---|
| 34 | /** @type {number} */
|
|---|
| 35 | const size = context.read();
|
|---|
| 36 | /** @type {Map<K, V>} */
|
|---|
| 37 | const map = new Map();
|
|---|
| 38 | /** @type {K[]} */
|
|---|
| 39 | const keys = [];
|
|---|
| 40 | for (let i = 0; i < size; i++) {
|
|---|
| 41 | keys.push(context.read());
|
|---|
| 42 | }
|
|---|
| 43 | for (let i = 0; i < size; i++) {
|
|---|
| 44 | map.set(keys[i], context.read());
|
|---|
| 45 | }
|
|---|
| 46 | return map;
|
|---|
| 47 | }
|
|---|
| 48 | }
|
|---|
| 49 |
|
|---|
| 50 | module.exports = MapObjectSerializer;
|
|---|
Note:
See
TracBrowser
for help on using the repository browser.