|
I don't think any changes are required in the driver. I used your example and after fixing two small issues the tests pass fine with no changes to the driver.
- The JSON string in the Deserialize_StructAsPropertyFromModel_DefaultValue test method used the wrong field name (should be "Number" instead of "Value"). I refactored the method to:
[Fact]
|
public void Deserialize_StructAsPropertyFromModel_DefaultValue()
|
{
|
var address = BsonSerializer.Deserialize<Address>(@"{ ""Number"": null }");
|
Assert.Equal(default, address.Number);
|
}
|
2. In your custom serializer you must "consume" the value from the reader even if you don't want to "read" it, otherwise the reader is still positioned at the value. I refactored HouseNumberSerializer.Deserialize to:
public override HouseNumber Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
|
{
|
var bsonType = context.Reader.GetCurrentBsonType();
|
|
if (bsonType == BsonType.Int32)
|
{
|
return new HouseNumber(context.Reader.ReadInt32());
|
}
|
else
|
{
|
context.Reader.SkipValue(); // if you choose not to read the value you must skip it
|
return default;
|
}
|
}
|
With those two changes the tests in your example pass without changes to the driver.
|