Sets plus JSON

Rick Waldron waldron.rick at gmail.com
Wed Oct 3 11:29:02 PDT 2012


On Wed, Oct 3, 2012 at 2:12 PM, Axel Rauschmayer <axel at rauschma.de> wrote:

> I always find it next to impossible to guess use cases for non-trivial
> features. So having those would help. Often YAGNI applies. Two
> possibilities:
> - Subtype Map and override toJSON
> - Use JSON.stringify() with a replacer
>
> As Brandon Benvie mention, we could standardize on an array of pairs, but
> my (not very educated) guess would be that converting to objects makes more
> sense.
>
>

Map.prototype.toJSON = function() {
  // somehow iterate keys and values into an array pair?
  // for now, I'll fake it.
  return '[ [ { "foo": "whatever" }, [ 1, 2, 3, 4, 5 ] ], [ {}, "my key is
a plain object" ] ]';
};

function Key(foo) {
  this.foo = foo;
}

var map = new Map(),
    key = new Key("whatever"),
    obj = {};


map.set(key, [ 1, 2, 3, 4, 5 ]);
map.set(obj, "my key is a plain object");

console.log( map.get(key) ); // [ 1, 2, 3, 4, 5 ]
console.log( map.get(obj) ); // "my key is a plain object"

console.log( map.toJSON() );

// [ [ { "foo": "whatever" }, [ 1, 2, 3, 4, 5 ] ], [ {}, "my key is a plain
object" ] ]

Now imagine this map is stored in some kind of NoSQL
document store and will be later pulled out by some
other functionality in our application.

How are the map keys revived _into_scope_?

console.log( JSON.parse(map.toJSON()) );

This just makes an array of pairs.


Rick







> On Oct 3, 2012, at 19:57 , Rick Waldron <waldron.rick at gmail.com> wrote:
>
>
>
> On Wed, Oct 3, 2012 at 1:35 PM, Axel Rauschmayer <axel at rauschma.de> wrote:
>
>> 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.
>>
>>
>> The default should probably be to convert to an object who’s keys are the
>> results of applying String() to the map’s keys. Additionally, one could
>> introduce a method toPairArray() that converts a Map into an array of pairs
>> (2-element arrays) – that can be JSON-ified. The alternatives are:
>>
>> 1. Switch to pairs if the keys are not strings.
>> 2. Allow a map to be configured which of the two representations should
>> be used for toJSON.
>> 3. Give toJSON a parameter whose default is to produce an object.
>>
>> Given JSON.toStringify(), I’m not sure that #2 is necessary and that #3
>> is useful. #1 might work, but seems like a big change in representation and
>> it takes time to check all the keys.
>>
>>
> My concern was actually about the references (for keys) that would be
> broken, how would you get those back? There is no reasonably sane way that
> doesn't involve magic scope memory tables and polluters.
>
> Unless I'm looking at it the wrong way...
>
> Rick
>
>
>
>
>
>
>>         --
>> Dr. Axel Rauschmayer
>> axel at rauschma.de
>>
>> home: rauschma.de
>> twitter: twitter.com/rauschma
>> blog: 2ality.com
>>
>>
>
>         --
> Dr. Axel Rauschmayer
> axel at rauschma.de
>
> home: rauschma.de
> twitter: twitter.com/rauschma
> blog: 2ality.com
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20121003/2dbf3d95/attachment-0001.html>


More information about the es-discuss mailing list