JSFiddle - React, Tailwind, and code Playground
CSS
body { white-space: pre; }
JavaScript
Map.prototype.toJSON = function() {
return ['window.Map', Array.from(this.entries())];
};
Map.fromJSON = function(key, value) {
return (value instanceof Array && value[0] == 'window.Map') ?
new Map(value[1]) :
value
;
};
var buf = '';
function out(str) {
buf += str + '\n';
}
var test = new Map([
[['array', 'key'], 7],
['stringKey', new Map([
['innerMap', 'supported'],
['anotherValue', 8]
])]
]);
//prove its a nested Map
out(test);
out(test.get('stringKey').get('innerMap'));
out('');
//test single Map
out(JSON.stringify(test.get('stringKey')));
//test nested Map
out(JSON.stringify(test));
//test map inside regular JSON stuff
out(JSON.stringify({regular:'object', test:test}));
out('');
//ruin our object
test = JSON.stringify(test);
//prove it
out(typeof test);
//bring it back from the dead
test = JSON.parse(test, Map.fromJSON);
//get the inner Map's value again
out(test.get('stringKey').get('innerMap'));
document.body.innerHTML = buf;