You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
If I have a get-only, preallocated[JsonExtensionData] property on my model, then the dictionary will be serialized successfully, but will silently fail to be populated on deserialization. This seems wrong. Details as follows.
Say I have the following model:
public class Model
{
[JsonExtensionData]
public Dictionary<string, object> ExtensionData { get; } = new Dictionary<string, object>();
}
And the following test method:
public class TestClass
{
[TestMethod]
public void TestReadOnlyExtensionDataProperty()
{
var model1 = new Model { ExtensionData = { { "item", "item value" } } };
var json = JsonSerializer.Serialize(model1);
Debug.WriteLine(json); // Prints {"item":"item value"} as expected
Assert.AreEqual(json, "{\"item\":\"item value\"}"); // Passes.
var model2 = JsonSerializer.Deserialize<Model>(json);
Assert.AreEqual(model1.ExtensionData.Count, model2.ExtensionData.Count, "model2.ExtensionData.Count"); // FAILS!
}
}
Then the first assert will succeed, because model1 is correctly serialized as {"item":"item value"}. However, the second assert fails, because model2.ExtensionData is empty when deserialized.
Conversely, Json.NET is able to round-trip this model successfully, see demo fiddle 2 here.
And if I modify Model.ExtensionData to have a setter that throws an exception, then deserialization also succeeds:
public class Model
{
readonly Dictionary<string, object> extensionData = new Dictionary<string, object>();
[JsonExtensionData]
public Dictionary<string, object> ExtensionData { get => extensionData; set => throw new NotImplementedException(); }
}
This seems wrong. Since the dictionary is preallocated it should be populated successfully during deserialization even without a setter -- especially since the setter isn't actually called.
The text was updated successfully, but these errors were encountered:
If I have a get-only, preallocated
[JsonExtensionData]
property on my model, then the dictionary will be serialized successfully, but will silently fail to be populated on deserialization. This seems wrong. Details as follows.Say I have the following model:
And the following test method:
Then the first assert will succeed, because
model1
is correctly serialized as{"item":"item value"}
. However, the second assert fails, becausemodel2.ExtensionData
is empty when deserialized.Demo fiddle 1 here reproducing the problem.
Conversely, Json.NET is able to round-trip this model successfully, see demo fiddle 2 here.
And if I modify
Model.ExtensionData
to have a setter that throws an exception, then deserialization also succeeds:Demo fiddle 3 here, which passes.
This seems wrong. Since the dictionary is preallocated it should be populated successfully during deserialization even without a setter -- especially since the setter isn't actually called.
The text was updated successfully, but these errors were encountered: