Sets plus JSON

Allen Wirfs-Brock allen at
Wed Oct 3 16:44:39 PDT 2012

(oops, forgot to reply-all)

Begin forwarded message:

> From: Allen Wirfs-Brock <allen at>
> Date: October 3, 2012 10:15:57 AM PDT
> To: Herby Vojčík <herby at>
> Subject: Re: Sets plus JSON
> On Oct 3, 2012, at 9:56 AM, Herby Vojčík wrote:
>> Nicholas C. Zakas wrote:
>>> After a little more experimenting with sets (still a really big fan!!),
>>> I've come across an interesting problem. Basically, I found myself using
>>> a set and then wanting to convert that into JSON for storage.
>>> JSON.stringify() run on a set returns "{}", because it's an object
>>> without any enumerable properties. I'm wondering if that's the correct
>>> behavior because a set is really more like an array than it is an
>>> object, and perhaps it would be best to define a toJSON() method for
>>> sets such as:
>>> Set.prototype.toJSON = function() {
>>> return Array.from(this);
>> It depends... you should be able to reread it, so the best thing would proably be to use matching set of transformers for both stringify and parse. I personally would rather see something like
>> 	{ _Set_from_: Array.from(this) }
>> here.
>>> };
>>> ...
> This is one of the reasons that it is important that Set (and Map, etc.) are specified (and implemented) in a manner that makes them fully subclassable.   By subclassing, individual use cases for serializing them can be kept distinct rather than multiple libraries or subsystems fighting over who gets control of the single implementation of Set.prototype.toJSON
> Allen

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the es-discuss mailing list