I\'m using the official Mongo C# Driver, and RestSharp to call a Rest Api with Json.NET to perform my serialization/deserialization. Say I have a Person class as follows, wh
You are implementing the ReadJson
method of the converter incorrectly. The existingValue
parameter does not give you the deserialized value read from the JSON, it gives you the existing value of the object that you will be replacing. In most cases this will be null or empty. What you need to do is use the reader
to get the value from the JSON, convert it as needed, then return the converted value.
Assuming your ObjectId
class has a constructor that accepts a hex string, here is how you would implement the ReadJson
method:
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
JToken token = JToken.Load(reader);
return new ObjectId(token.ToObject<string>());
}