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.
This is not possible, and this is expected. If a
WeakMapinstance 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):So for instance, it is not even possible to know how many keys a
WeakMapinstance currently has, which is of course a requirement if you want to serialize an object.