New JavaScript Set methods are landing across browsers. Learn about sets, how you can use these methods to compare different sets, create new sets with specific properties, and more.
There also needs to be some way to indicate that a JSON construct is a Set, Map, plain object, or array. You’d want a date/time type as well.
Without breaking existing JSON parsers, the way to do that is to add metadata like a _type field to an object, or to add a “sidecar” object like superjson does. Which works but is ugly IMO.
Then there’s BSON, YAML, JSON Schema, and the one we don’t mention ₓₘₗ. To my knowledge all of those could be extended in a way to support new types, but require the producer and consumer to both understand and follow whatever convention you use. They lack the universal interchangeability of JSON.
Where would/should the mapping happen? Probably not the Set constructor.
JSON.parseSet()
?/edit:
JSON.parseMap()
There also needs to be some way to indicate that a JSON construct is a Set, Map, plain object, or array. You’d want a date/time type as well.
Without breaking existing JSON parsers, the way to do that is to add metadata like a
_type
field to an object, or to add a “sidecar” object like superjson does. Which works but is ugly IMO.Then there’s BSON, YAML, JSON Schema, and the one we don’t mention ₓₘₗ. To my knowledge all of those could be extended in a way to support new types, but require the producer and consumer to both understand and follow whatever convention you use. They lack the universal interchangeability of JSON.