0

Given a JS WeakMap, I want to convert this to JSON data.

const weakmap = new WeakMap() // Convert weakmap to JSON data / JS object

I've tried JSON.parse(JSON.stringify(weakmap)) which returns empty object. Since one cannot iterate over keys of WeakMap, it's also not possible to convert it by iteration.

Ayush Jain
  • 316
  • 4
  • 11
  • 2
    You can't. There is no way to iterate a WeakMap, and this is expected. If you could, it could not be "weak". – trincot Aug 20 '23 at 09:43
  • I know we cannot iterate a WeakMap which I've mentioned in the question itself. What I'm trying to find and could not find anywhere on the SO is whether you can convert a WeakMap to some other data type or not. Even if the answer is no, the question stands as valid question to be documented here for anyone looking to try find the same. – Ayush Jain Aug 20 '23 at 09:50
  • Yes, I am not arguing the question is not valid. But the answer is "no, you can't". JSON is a form of serialization, and serialization involves enumeration, and enumeration is not supported by design. – trincot Aug 20 '23 at 09:51
  • @AyushJain If documenting is the goal, then the title should read "Is it possible to convert a `WeakMap` to a JSON object in JavaScript?" – Parzh from Ukraine Aug 20 '23 at 10:02
  • 1
    Also, even if it were possible to enumerate it, keys in a `WeakMap` are objects, which is not representable in JSON. – user3840170 Aug 20 '23 at 10:17
  • Also note that JavaScript only has one [JSON object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON), which isn't what you are referring to. Either speak of JSON -- the text format used for serialization -- or JavaScript object. For JavaScript object the question would be ambiguous, since a WeakMap is already a JavaScript object, so I take it you speak of serialization, or some specific kind of JavaScript objects that only use a small subset of JavaScript. But then: WeakMaps are **not** limited in that way. – trincot Aug 20 '23 at 10:30

1 Answers1

2

This is not possible, and this is expected. If a WeakMap instance would be serializable (for example to JSON), then you would need to be able to enumerate its keys, but to have the "weak" behaviour, this is not supported.

Mozilla Contributors write the following on WeakMap (I highlight):

WeakMap allows associating data to objects in a way that doesn't prevent the key objects from being collected, even if the values reference the keys. However, a WeakMap doesn't allow observing the liveness of its keys, which is why it doesn't allow enumeration; if a WeakMap exposed any method to obtain a list of its keys, the list would depend on the state of garbage collection, introducing non-determinism. If you want to have a list of keys, you should use a Map rather than a WeakMap.

So for instance, it is not even possible to know how many keys a WeakMap instance currently has, which is of course a requirement if you want to serialize an object.

trincot
  • 317,000
  • 35
  • 244
  • 286