MapObjectSerializer.js 554 B

12345678910111213141516171819202122232425262728293031
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. */
  4. "use strict";
  5. class MapObjectSerializer {
  6. serialize(obj, { write }) {
  7. write(obj.size);
  8. for (const key of obj.keys()) {
  9. write(key);
  10. }
  11. for (const value of obj.values()) {
  12. write(value);
  13. }
  14. }
  15. deserialize({ read }) {
  16. let size = read();
  17. const map = new Map();
  18. const keys = [];
  19. for (let i = 0; i < size; i++) {
  20. keys.push(read());
  21. }
  22. for (let i = 0; i < size; i++) {
  23. map.set(keys[i], read());
  24. }
  25. return map;
  26. }
  27. }
  28. module.exports = MapObjectSerializer;