[CSHARP-3034] How to Mimic the [JsonProperty(IsReference = true)] feature of JSON NewtonSoft .NET C# Created: 30/Mar/20  Updated: 27/Oct/23  Resolved: 07/Apr/20

Status: Closed
Project: C# Driver
Component/s: Serialization
Affects Version/s: 2.10.2
Fix Version/s: None

Type: Improvement Priority: Major - P3
Reporter: Michael Fyffe Assignee: Robert Stam
Resolution: Works as Designed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

We are currently migrating from RavenDB to MongoDB and are unable to find an equivalent feature of NewtonSofts [JsonProperty(IsReference = true)] .

https://www.newtonsoft.com/json/help/html/PreserveObjectReferences.htm 1

Does MongoDB Currently have a built in way to mimic this? If not how would i go about doing such a custom implementation myself with the drivers currenty customization hooks?



 Comments   
Comment by Robert Stam [ 07/Apr/20 ]

The MongoDB C# driver does not currently have a feature similar to Json.NET's IsReference option and we have no plans at the moment to support that.

You are able to write and register custom serializers, so there might be a way to accomplish this, but I don't have any concrete advice on how you might do that. Seems like the main technical issue is that when the C# driver calls a a serializer (for example a PersonSerializer for a Person object using the example from the Json.NET documentation) the PersonSerializer has no way of knowing anything about the surrounding context it was called in, so not sure how it would know to add a `$id` to the first Person object serialized and to serialize a `$ref` instead of a full Person document when called with a duplicate instance.

Sorry I don't have better news for you.

Generated at Wed Feb 07 21:44:11 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.