-
Type: Task
-
Resolution: Fixed
-
Priority: Major - P3
-
Affects Version/s: 2.8.1
-
Component/s: Serialization
-
None
Serializers for anonymous types are created automatically with little or no chance of user configuration of the serializer. Currently, the automatically created serializer requires that the document being deserialized contain an element for every property of the anonymous class.
It would be desirable for the serializer for an anonymous class to simply use default values for any missing elements rather than throw an exception.
This is a little tricky to reproduce because it involves anonymous classes, but the following code using a generic local function reproduces the issue. Ideally all 4 JSON inputs would be successfully deserialized.
public static class Program { public static void Main(string[] args) { var prototype = new { X = 1, Y = 2 }; T deserialize<T>(T dummy, string json) { var serializer = BsonSerializer.LookupSerializer<T>(); using (var reader = new JsonReader(json)) { var context = BsonDeserializationContext.CreateRoot(reader); return serializer.Deserialize(context); } } var inputs = new[] { "{ }", "{ X : 3 }", "{ Y : 4 }", "{ X : 3, Y : 4 }" }; foreach (var json in inputs) { var a = deserialize(prototype, json); } } }
- is depended on by
-
CSHARP-1339 Adding array member to collection model causes error during subsequent deserialisation
- Closed