Uploaded image for project: 'Realm JavaScript SDK'
  1. Realm JavaScript SDK
  2. RJS-2860

toJSON() serialization of BSON.ObjectId to String

      Problem

      I'm using toJSON() on my RealmObject's to get serializable state which I can use in Redux.

      BSON.ObjectId's appear to be serialized to an Object which when called with toString() returns the hex string of the ID.

      However I was expecting to get a String instead, since I would be able to use a string as key in a Map, to allow fast lookups, whereas the Object comparisons don't work for equality when retrieved from different documents.

      Solution

      I'm not sure if there's an intent behind serializing the BSON.ObjectId as an Object, but it would be great if they could be serialized as strings instead, similar to BSON.ObjectId.toHexString().

      Alternatives

      Currently I'm overriding toJSON() on my RealmObjects's and manually serializing to string:

      Unable to find source-code formatter for language: ts. Available languages are: actionscript, ada, applescript, bash, c, c#, c++, cpp, css, erlang, go, groovy, haskell, html, java, javascript, js, json, lua, none, nyan, objc, perl, php, python, r, rainbow, ruby, scala, sh, sql, swift, visualbasic, xml, yaml
        toJSON() {
          const o = super.toJSON() as MyObject;
          o._id = o._id.toString();
          o.category._id = o.category._id.toString();
          return o;
        }
      

      How important is this improvement for you?

      I would like to have it but have a workaround

      Feature would mainly be used with

      Atlas Device Sync

            Assignee:
            Unassigned Unassigned
            Reporter:
            unitosyncbot Unito Sync Bot
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated: