Sets plus JSON

Rick Waldron waldron.rick at gmail.com
Thu Oct 4 21:02:12 PDT 2012




On Thursday, October 4, 2012 at 9:29 PM, Allen Wirfs-Brock wrote:

> 
> On Oct 4, 2012, at 5:32 PM, Rick Waldron wrote:
> > 
> > 
> > On Thu, Oct 4, 2012 at 8:16 PM, Brendan Eich <brendan at mozilla.org (mailto:brendan at mozilla.org)> wrote:
> > > Nicholas C. Zakas wrote:
> > > > I agree, I'm not sure there is a rational default for Map, but I think there is one for Set as an array (and it seems like most people agreed).
> > > 
> > > As with Set, I claim the default JSON for Map should be
> > > 
> > >   [[key1, value1], ~~~ [keyN, valueN]]
> > 
> > 
> > I'm still curious about my question from yesterday; instead of repasting, I put it in a gist:
> > 
> > https://gist.github.com/3837337 
> > 
> 
> 
> Something like: 
> 
>           {"<kind>" : "Map",
>            "<mapData>" : [
>                       [ key1, value1],
>                       [ keyN, valueN]
>                       ]
>              }
> 
> is arguably a better because then you can write a  reviver function that recognizes it:
> 
> function mapReviver(key, value) {
> if (typeof value != "object") return value;
> switch (value["<kind>"]) {
> case undefined: return value;
> case "Map": {
> let newValue = new Map;
> let mapData = value["<mapData>"];
> if (!mapData) return value;
> mapData.forEach(function(e){newValue.set(e[0], e[1])});
> return newValue;
> }
> default: return value;
> }
> }
> function mapReviver(key, value) {
> if (typeof value != "object") return value;
> switch (value["<kind>"]) {
> case undefined: return value;
> case "Map": {
> let newValue = new Map;
> let mapData = value["<mapData>"];
> if (!mapData) return value;
> mapData.forEach(function(e){newValue.set(e[0], e[1])});
> return newValue;
> }
> default: return value;
> }
> }
> function mapReviver(key, value) {
> if (typeof value != "object") return value;
> switch (value["<kind>"]) {
> case undefined: return value;
> case "Map": {
> let newValue = new Map;
> let mapData = value["<mapData>"];
> if (!mapData) return value;
> mapData.forEach(function(e){newValue.set(e[0], e[1])});
> return newValue;
> }
> default: return value;
> }
> }
> function mapReviver(key, value) {
> if (typeof value != "object") return value;
> switch (value["<kind>"]) {
> case undefined: return value;
> case "Map": {
> let newValue = new Map;
> let mapData = value["<mapData>"];
> if (!mapData) return value;
> mapData.forEach(function(e){newValue.set(e[0], e[1])});
> return newValue;
> }
> default: return value;
> }
> }
> function mapReviver(key, value) {
> if (typeof value != "object") return value;
> switch (value["<kind>"]) {
> case undefined: return value;
> case "Map": {
> let newValue = new Map;
> let mapData = value["<mapData>"];
> if (!mapData) return value;
> mapData.forEach(function(e){newValue.set(e[0], e[1])});
> return newValue;
> }
> default: return value;
> }
> }
> function mapReviver(key, value) {
> if (typeof value != "object") return value;
> switch (value["<kind>"]) {
> case undefined: return value;
> case "Map": {
> let newValue = new Map;
> let mapData = value["<mapData>"];
> if (!mapData) return value;
> mapData.forEach(function(e){newValue.set(e[0], e[1])});
> return newValue;
> }
> default: return value;
> }
> }
> function mapReviver(key, value) { if (typeof value != "object") return value; switch (value["<kind>"]){ case undefined: return value; case "Map": { let newValue = new Map; let mapData = value["<mapData>"]; if (!mapData) return value; mapData.forEach(e=>newValue.set(e[0], e[1])); return newValue; } default: return value; } } 
> 
> var tree = JSON.parse(sourceString, mapReviver);

But, this still doesn't explain how an object-as-key gets its reference put back into (the correct) scope :)

Rick



> 
> This should work for  Map's nested at any level of a JSON tree, including Maps nested within Maps.
> 
> Allen
> 
> 
> 
> if (typeof value != "object") return value;
> switch (value["<kind>"]) {
> case undefined: return value;
> case "Map": {
> let newValue = new Map;
> let mapData = value["<mapData>"];
> if (!mapData) return value;
> mapData.forEach(function(e){newValue.set(e[0], e[1])});
> return newValue;
> }
> default: return value;
> }
> }
> 
> 
> 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20121005/64b23fe6/attachment-0001.html>


More information about the es-discuss mailing list