Realm-js: Serializing and deserializing a User

Created on 1 Sep 2017  路  1Comment  路  Source: realm/realm-js

Goals

In a multi process environment, like an electron app, it would be valuable to be able to pass users between processes in a serialized form.

Expected Results

I would expect that when logging in to ROS and serializing the resulting user I would get an object as follows:

Realm.Sync.User.register("http://localhost:9080", username, password, (err, user) => {
    const userString = JSON.stringify(user);
    // user === "{token: "...", identity: "some-username", server: "http://localhost:9080", isAdmin: false }"

This could be fixed by implementing a toJSON method on the Realm.Sync.User class: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#toJSON()_behavior

In addition to this, it would be valuable to create a Realm.Sync.User from a JSON representation. This could be implemented with a fromJSON method on the user.

Actual Results

Serializing a Realm.Sync.User (JSON.stringify(user)) gives you the string representation of an empty object ({}) and the fields of a User are all implemented as getters that cannot be set.

Steps to Reproduce

  1. Login or register a user through the Realm Object Server.
  2. Serialize the user using JSON.stringify(user)
  3. Observe the disappointing result

Code Sample

See "Expected Results"

Version of Realm and Tooling

  • Realm JS SDK Version: 2.0.0-rc5
T-Enhancement

Most helpful comment

a toJSON in realm objects and collections would be great. Useful for debugging and serialization support.

>All comments

a toJSON in realm objects and collections would be great. Useful for debugging and serialization support.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

fever324 picture fever324  路  4Comments

jmartindivmedianet picture jmartindivmedianet  路  3Comments

emrehayirci picture emrehayirci  路  3Comments

max-zu picture max-zu  路  3Comments

Chris-Petty picture Chris-Petty  路  3Comments