GitHub - DavidBruant/Map-Set.prototype.toJSON: An ECMAScript proposal (original) (raw)
Map-Set.prototype.toJSON
An ECMAScript proposal. Currently at Stage 0.
This proposal was brought before the committee in the March 2016 meeting, and thoroughly rejected. Details
Problem
Here is the current situation:
var s = new Set(['yo', 'ya', true, 8]);
console.log(JSON.stringify(s)); // '{}'
This result is unhelpful. Same goes for Map.
Proposal
This proposal is about providing a sensible default to the common operation of JSON serialization via default toJSON
implementations on Set.prototype
and Map.prototype
. Of course, userland code can always shadow this value on specific instances.
Map.prototype.toJSON
The essence of the proposal is captured in this snippet (spec in markdown or HTML):
Map.prototype.toJSON = function toJSON() { return [...Map.prototype.entries.call(this)]; }
Set.prototype.toJSON
The essence of the proposal is captured in this snippet (spec in markdown or HTML):
Set.prototype.toJSON = function toJSON() { return [...Set.prototype.values.call(this)]; }
Discussion
There might be a web compat concern if code using native Map and Set or polyfill relies on the current JSON.stringify behavior.
Licence
This work is dedicated to the public domain. It is CC0 licenced.