7 ms·
IMO it would also be really nice to have non-string keys in objects, like: { [1, 2]: "a", [3, 4]: "b" }
by uDontKnowMe 6y ago
IMO it would also be really nice to have non-string keys in objects, like:
{
[1, 2]: "a",
[3, 4]: "b"
}
- resonantjacket5 6y agoit's usually a pretty bad idea to have a mutable object (the array) as a key. Though I guess if you consider it as a 'tuple' since when in json form it can't be modified I guess it'd be technically possible, though with some heavy caveats.
- chrisseaton 6y agoThere’s no concept of mutability in JSON. A JSON parser could give you immutable arrays and maps if it wanted to.
- resonantjacket5 6y agoYes, but json by itself doesn't actually do anything. It'd always be used in context of javascript/python/ etc.. As I said you could interpret it as a tuple or I guess a frozen array for javascript? I don't think there is a native immmutable array in javascript. If it's a map I'm a bit more unsure how you'd check to find the object (quickly). You're basically getting into the how to store a struct/class as a map key. Either way its a bit more involved than just having json parser return an immutable array and map, and the more I think about it the more edges cases there are.
- recursive 6y ago> I don't think there is a native immmutable array in javascript. I'm here to tell you that `Object.freeze()` works just fine on arrays.
- resonantjacket5 6y agoNice. Though I don't think that solves the rest of the issues I outlined sadly. I guess you could have this json+ convert into some custom javascript class that allows arrays/objects as map keys rather than the normal javascript object that only accepts strings as map keys.
- chrisseaton 6y ago> I guess you could have this json+ convert into some custom javascript class that allows arrays/objects as map keys rather than the normal javascript object that only accepts strings as map keys. Sure - why not? JSON doesn't have to map directly to JavaScript objects.
- xpe 6y agoYou realize this would push outside of what JavaScript provides? 'Welcome to Node.js v15.7.0. Type ".help" for more information. > { [1, 2]: "a" } ({ [1, 2]: "a" }) ^ Uncaught SyntaxError: Unexpected token ',' Thar' be dragons! https://stackoverflow.com/questions/32660188/using-array-objects-as-key-for-es6-map https://stackoverflow.com/questions/32660188/using-array-obj...
- ledauphin 6y agoit's true JS doesn't support this, and it's also true that any JSON that supported it would therefore no longer be _JavaScript_ object notation. However, it's equally true that a better language and object notation would support non-string scalar keys.
- kevingadd 6y agoJavaScript wouldn't be able to consume those resulting objects, so it would be pretty diminished as an alternative to JSON. Object keys are all strings.
- xpe 6y agoI can see why you want non-string scalar keys. In general, I do too. (I'm writing a data language that has them.) However, I don't think it is useful to prod JSON (or a variant) to go in this direction.
- redisman 6y agoWhy?
- wruza 6y agoOne of the uses is mapping values to objects instead of strings, so you don’t have to use ids: ob = {...} somemap[ob] = value // vs somemap[ob.id] = value In languages which allow weak keys in such maps, a garbage collector may even reclaim whole key-value pairs once ob falls out of existence. But to my opinion, in json it is barely useful for a number of reasons. It is more programming technique than data transfer format, and these features (and reference loops) should be encoded at a higher level than json.