Skip to content
This repository has been archived by the owner on Jun 29, 2021. It is now read-only.

EJSON should serialize Set and Map objects #106

Open
klaussner opened this issue Jun 13, 2017 · 1 comment
Open

EJSON should serialize Set and Map objects #106

klaussner opened this issue Jun 13, 2017 · 1 comment

Comments

@klaussner
Copy link

klaussner commented Jun 13, 2017

EJSON should serialize Set and Map objects so that they can be returned from methods and used as method arguments. This can be implemented in apps by adding the necessary functions to the Set and Map prototypes and adding custom EJSON types, but it would be nice to have that functionality by default:

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

Set.prototype.toJSONValue = function () {
  return Array.from(this);
};

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

EJSON.addType('Set', (json) => {
  return new Set(json);
});

Meteor.methods({
  getSet() {
    const set = new Set;
    set.add('Meteor');
    set.add('❤️');

    return set;
  }
});

Related issue: meteor/meteor#8789 (@priyadarshy)

@luzlab
Copy link

luzlab commented Sep 7, 2017

I wrote this code for de/serializing Maps based on online sources.

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'd make a PR if someone could suggest how to write it. I looked over the ejson code, but there doesn't seem to be an obvious place to add support for a custom type.

# for free to subscribe to this conversation on GitHub. Already have an account? #.
Projects
None yet
Development

No branches or pull requests

3 participants