ObjectID representation as string not working properly

I have a Atlas Device Sync app. Since the built in .NET serializer System.Text.JSON cannot deserialize ObjectId I tried different approaches. I created an custom converter. But the fields that I get from the JSON are only working with deprecated constructors. Dead end.

Next thing I tried was using the annotations that tell the compiler to represent the ObjectId as string. This kind of works, but the DataType that will be synced to the MongoDB instance is string and not ObjectId. Does anybody have a similar experience?

This is how I decorated the property.

    public string Id { get; set; } = ObjectId.GenerateNewId().ToString();

This is how the ID is represented in MongoDB. For me it looks like the representation did not work properly.

The [BsonId] and [BsonRepresentation] attributes are not part of the Realm SDK and as such are not taken into consideration when determining how to store the data on disk (and respectively, how to synchronize it). When you store the data as a string, it will get persisted in MongoDB as a string like on the screenshot.

If you want this to be stored as ObjectId, you should declare your Id property in the model as such. What is the use case for deserializing it with System.Text.JSON - what are you deserializing it from and what are the errors that you get?

Thanks for your reply. The problem was that I created a WebAPI that provides my Models as JSON documents. When deserializing those documents I got the errors. I wanted to use System.Text.JSON as it is built in.

I solved this with creating an Interface IFoobar<T> and made the ID T ID . The WebAPI now gets the ID as ObjectId and converts it for further processing to string representation. When I want to store those objects back to Realm I convert back. In my application internally I do not use the ObjectId. Since I already used two models (one for realm and one for further usage) this was no hard work.