I'm getting error [TypeError: JSON.stringify cannot serialize cyclic structures.] object
when i try to convert realm object to json object using
JSON.parse(JSON.stringify(item))
This is not a bug in Realm. This is due to the circular structure within item.
In [email protected] & [email protected] we've reworked Realm.Object.toJSON() & introduced toJSON on Realm.Collection.
We've also introduced Realm.JsonSerializationReplacer, for serializing circular structures. But as it seems like you are trying to convert into a plain JS object (so just adding Realm.JsonSerializationReplacer won't help you), perhaps try updating & use item.toJSON() instead of JSON.parse(JSON.stringify(item)) - this returns a plain JS object (used for serialization).
Note: if you are using class models, make sure your models extends Realm.Object.
I think this issue still persists on 6.1.0.
Yes, adding just Realm.JsonSerializationReplacer did not help.
So, using item.toJSON() on my own end still did not help either, item.toJSON() basically return the same structure like it has not been applied.
E.g:
realm.objects('Chat').forEach((element) => {
console.log("LOGGER:: ", element.toJSON());
});
So, I just downgraded to 6.0.3 and it worked fine, but I will be missing the update fix.
Am I missing anything ?
Yes toJSON() returns the same structure, where instances of Realm.Object & Realm.Collection has been replaced by flat JS objects & arrays. For more info on toJSON, see MDN toJSON.
Realm.JsonSerializationReplacer is intended as a replacer (2nd argument) for JSON.stringify, is this how you utilized it? (see example bellow).
const chats = realm.objects('Chat');
const serialized = JSON.stringify(chats, Realm.JsonSerializationReplacer)
If downgrading to [email protected] fixed the circular structure error, could I ask you to supply some code or a small project (node/RN version used), where the error occurs in 6.1.0 and not in 6.0.3?
@steffenagger @Whales1992
for me now it works with below code but sometime its also add some "#ref" at the end of every object.
export const copyRealmObject = (item: any) => {
return JSON.parse(JSON.stringify(item , Realm.JsonSerializationReplacer)) //refReplacer(item)
}
@mahipalsingh-syt Realm.JsonSerializationReplacer is only intended for serialization to a string, where serialization is not possible due to circular references (e.g. a JSON export) - the added $ref is to indicate, in a readable manner, to where the circular reference points.
In other words: parsing it would require additional logic.
If you're _"just"_ trying to make a copy of the Realm.Object as plain JS (your naming highly indicates this), then please try this instead (only works on 6.1.0+):
/** Converts a Realm.Object into a plain JS object */
export const copyRealmObject = (item: Realm.Object) => item.toJSON()
/** Converts a Realm.Collection, Realm.List or Realm.Results into a plain JS array, containing plain JS objects */
export const copyRealmCollection = (collection: Realm.Collection) => collection.toJSON()
This should keep the circular references through the objects.
Let me know if it yields problems.
Most helpful comment
Yes
toJSON()returns the same structure, where instances ofRealm.Object&Realm.Collectionhas been replaced by flat JS objects & arrays. For more info ontoJSON, see MDN toJSON.Realm.JsonSerializationReplaceris intended as a replacer (2nd argument) forJSON.stringify, is this how you utilized it? (see example bellow).If downgrading to [email protected] fixed the circular structure error, could I ask you to supply some code or a small project (node/RN version used), where the error occurs in 6.1.0 and not in 6.0.3?