Inactive
Classification: API Change
Human Validated: KW
Title: {Set,Map}.prototype.toJSON
Authors: David Bruant, Jordan Harband
Rejected: better solved by a custom replacer function.
Last Presented: None
Stage Upgrades:
Stage 1: NA
Stage 2: NA
Stage 2.7: NA
Stage 3: NA
Stage 4: NA
Last Commit: 2016-04-01
Topics: objects collections
Keywords: json map set
GitHub Link: https://github.com/DavidBruant/Map-Set.prototype.toJSON
GitHub Note Link: https://github.com/DavidBruant/Map-Set.prototype.toJSON/issues/16
Proposal Description:
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.