primus/ejson

Adding support for Maps

luzlab opened this issue · 1 comments

I'd like to add support for Map objects. I have some working code, but it's not clear where I should add code for new types. I also saw an issue on the meteor repo for this, and would like to submit a PR here so I can EJSON Maps in plain Node apps as well.

Any thoughts on how to add additional types?

import { Meteor } from 'meteor/meteor';
import { EJSON } from 'meteor/ejson';

Map.prototype.toJSONValue = function () {
  return JSON.stringify([...this]);
};

Map.prototype.typeName = function () {
  return 'Map';
};

EJSON.addType('Map', (jsonStr) => {
  return new Map(JSON.parse(jsonStr));
});

I created an npm module ejson-extras that extends EJSON to support Maps (and more) types.