Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Option to deserialize ObjectId into strict format #8

Open
HappyNomad opened this issue Feb 22, 2018 · 0 comments
Open

Option to deserialize ObjectId into strict format #8

HappyNomad opened this issue Feb 22, 2018 · 0 comments

Comments

@HappyNomad
Copy link

Mongo's BsonBinaryWriter serializes my complex document on the server, and Json.NET's BsonDataReader deserializes it to a JObject on the client. After deserialization, I replace _id ObjectIds with their string representations and then do the following for references:

foreach ( var reference in rootJObject.Descendants().OfType<JValue>().Where( v => v.Type == JTokenType.Bytes ).ToList() )
    reference.Replace( new JObject( new JProperty( "$ref", (string)reference ) ) );

This seems to work but relying on v.Type == JTokenType.Bytes to recognize the ObjectIds concerns me. Bytes is too general and could lead to false positives. I propose to improve the deserialization of BSON to provide a more sure way to recognize the ObjectIds.

In the original JSON with which I seeded the Mongo database, I represented ObjectIds in Mongo's strict JSON format of {$oid: "someObjectId"}. Upon deserializing to JObject, I propose an option that the ObjectIds is represented as such instead of the byte representation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant