Skip to content
This repository has been archived by the owner on Dec 4, 2018. It is now read-only.

Better preparation for cloned object serialization #134

Merged
merged 1 commit into from
Mar 1, 2018

Conversation

bengourley
Copy link
Contributor

@bengourley bengourley commented Feb 23, 2018

When Utils.cloneObject() was called on an instance of a class, such as the mongo driver's ObjectId instance, the copy would bring across its enumerable private properties, but none of its methods, resulting in a bad serialization like so:

  "id": {
    "id": {
      "0": 88,
      "1": 129,
      "2": 222,
      "3": 117,
      "4": 187,
      "5": 221,
      "6": 189,
      "7": 57,
      "8": 86,
      "9": 86,
      "10": 65,
      "11": 202
    },
    "bsontype": "ObjectID"
  }

Now just before cloning, objects are checked for a toJSON() method. If they have one, the result of that is used for the clone resulting in better serialization, such as:

  { "oid": "5a9045f5be123c8c5d811d35" }

When Utils.cloneObject() was called on an instance of a class,
such as the mongo driver's `ObjectId` instance, the copy would
bring across its enumerable private properties, but none of its
methods, resulting in a bad serialization like so:

      "id": {
        "id": {
          "0": 88,
          "1": 129,
          "2": 222,
          "3": 117,
          "4": 187,
          "5": 221,
          "6": 189,
          "7": 57,
          "8": 86,
          "9": 86,
          "10": 65,
          "11": 202
        },
        "bsontype": "ObjectID"
      }

Now just before cloning, objects are checked for a toJSON() method.
If they have one, the result of that is used for the clone resulting
in better serialization, such as:

      { "oid": "5a9045f5be123c8c5d811d35" }
@bengourley bengourley requested a review from bullmo February 23, 2018 17:16
@bengourley bengourley merged commit 1d6bf8c into master Mar 1, 2018
@kattrali kattrali deleted the mongo-objectid-serialization branch April 13, 2018 18:30
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants