See my StackOverflow question here for background:
https://stackoverflow.com/questions/44085267/mongo-c-sharp-driver-and-objectid-json-string-format-in-net-core.
The core is that if I parse the following JSON as a BsonDocument, I can't get it back into this form without doing a ton of extra boiler plate work:
You cannot override any of the BsonValue derived types:
/// <summary>
/// Writes a BSON ObjectId to the writer.
/// </summary>
/// <param name="objectId">The ObjectId.</param>
public override void WriteObjectId(ObjectId objectId)
{
if (Disposed) { throw new ObjectDisposedException("JsonWriter"); }
if (State != BsonWriterState.Value && State != BsonWriterState.Initial)
{
ThrowInvalidState("WriteObjectId", BsonWriterState.Value, BsonWriterState.Initial);
}
WriteNameHelper(Name);
switch (_jsonWriterSettings.OutputMode)
{
case JsonOutputMode.Strict:
_textWriter.Write("{{ \"$oid\" : \"{0}\" }}", objectId.ToString());
break;
case JsonOutputMode.Shell:
default:
_textWriter.Write("ObjectId(\"{0}\")", objectId.ToString());
break;
}
State = GetNextState();
}
If this is all there is at the moment and I'm not missing something that will accomplish the desired behavior, then I'd like to propose two options:
(a) Let's change the default text writer. I'm not sure why anyone would want to pass around $oid or $date objects in json. ObjectIds are easily represented as strings, and dates are typically represented in the well defined JavaScript `toISOString()` format: "2017-05-27T10:59:49.955Z".
(b) Let's add some sort of extensibility hook that allows for overwriting this default behavior. The BsonSerializer approach is tough because it depends on the Writer type, which I think is a good idea, but it takes away all the power and flexibility a developer might need in this use case. Furthermore it's simply disabled for the most important types.
Let me know thoughts! Also, please answer my question on StackOverflow if there is a better, supported way.
Thanks
Matt Davis