Sets plus JSON

Rick Waldron waldron.rick at gmail.com
Wed Oct 3 09:53:40 PDT 2012


On Wed, Oct 3, 2012 at 12:37 PM, Nicholas C. Zakas <
standards at nczconsulting.com> 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,


...In the same way that an Array is just 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);
> };
>
> That way, JSON.stringify() would do something rational by default when
> used with sets.
>
> Thoughts?
>

Definitely +1



This also made me wonder about Maps, if the same use case were applied -
toJSON simply wouldn't work when you have an object as a key.


Rick





>
> Thanks,
> Nicholas
>
> --
> ___________________________
> Nicholas C. Zakas
> http://www.nczonline.net
>
> ______________________________**_________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/**listinfo/es-discuss<https://mail.mozilla.org/listinfo/es-discuss>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20121003/4382d0cd/attachment.html>


More information about the es-discuss mailing list