Class ObjectIdConverter
Provides a JSON converter for serializing and deserializing MongoDB.Bson.ObjectId values.
public class ObjectIdConverter : JsonConverter
- Inheritance
-
JsonConverterObjectIdConverter
- Inherited Members
-
JsonConverter.CanReadJsonConverter.CanWrite
- Extension Methods
Remarks
Inherits from Newtonsoft.Json.JsonConverter to customize JSON representation for the type it targets.
Methods
CanConvert(Type)
Determines whether the converter can convert the specified type by checking if the type is assignable from MongoDB.Bson.ObjectId.
public override bool CanConvert(Type objectType)
Parameters
objectTypeTypeThe type to evaluate for compatibility with the converter.
Returns
ReadJson(JsonReader, Type, object?, JsonSerializer)
Reads a JSON value and converts it to an MongoDB.Bson.ObjectId instance. When the token is a non-empty string different from the default ObjectId representation and parses successfully, the parsed value is returned; otherwise the method falls back to MongoDB.Bson.ObjectId.Empty for non-nullable target types or null for nullable target types.
public override object? ReadJson(JsonReader reader, Type objectType, object? existingValue, JsonSerializer serializer)
Parameters
readerJsonReaderThe JSON reader positioned on the value to deserialize.
objectTypeTypeThe target type expected by the deserializer, used to decide between returning MongoDB.Bson.ObjectId.Empty or
nullwhen the value cannot be parsed.existingValueobjectThe existing value being populated, passed through from the deserializer.
serializerJsonSerializerThe JSON serializer invoking this converter.
Returns
- object
An MongoDB.Bson.ObjectId instance when the value can be parsed or when the target type is non-nullable;
nullwhen the target type is nullable and no valid value is found.
WriteJson(JsonWriter, object?, JsonSerializer)
Serializes an MongoDB.Bson.ObjectId instance to JSON by writing its string representation; if the value is not an MongoDB.Bson.ObjectId, writes a JSON null instead.
public override void WriteJson(JsonWriter writer, object? value, JsonSerializer serializer)
Parameters
writerJsonWriterThe Newtonsoft.Json.JsonWriter used to emit the JSON output.
valueobjectThe value to serialize, expected to be an MongoDB.Bson.ObjectId instance.
serializerJsonSerializerThe Newtonsoft.Json.JsonSerializer invoking this converter.