Sets plus JSON

Allen Wirfs-Brock allen at wirfs-brock.com
Thu Oct 4 18:29:25 PDT 2012


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> 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);

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/20121004/90162761/attachment.html>


More information about the es-discuss mailing list