diff options
author | JamesNK <james@newtonking.com> | 2011-04-22 03:18:19 +0400 |
---|---|---|
committer | JamesNK <james@newtonking.com> | 2011-04-22 03:18:19 +0400 |
commit | 2c190ec438187affa34b161bfecf65bcf626cacb (patch) | |
tree | 518e41547e55733e7a77297f007d470dd04cfd05 | |
parent | 1e75151dda175d78d5779b5d821f1f5b1d02d110 (diff) |
-Fixed objects created with paramatized constructors to not set properties that aren't in JSON
-rw-r--r-- | Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs | 33 | ||||
-rw-r--r-- | Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs | 3 |
2 files changed, 34 insertions, 2 deletions
diff --git a/Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs b/Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs index bee8774..634dd71 100644 --- a/Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs +++ b/Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs @@ -4277,5 +4277,38 @@ keyword such as type of business."" Assert.AreEqual(1939, commentTestClass.StartYear);
Assert.AreEqual(63, commentTestClass.Values.Count);
}
+
+ class DTOWithParameterisedConstructor
+ {
+ public DTOWithParameterisedConstructor(string A)
+ {
+ this.A = A;
+ B = 2;
+ }
+
+ public string A { get; set; }
+ public int? B { get; set; }
+ }
+
+ class DTOWithoutParameterisedConstructor
+ {
+ public DTOWithoutParameterisedConstructor()
+ {
+ B = 2;
+ }
+
+ public string A { get; set; }
+ public int? B { get; set; }
+ }
+
+ [Test]
+ public void PopulationBehaviourForOmittedPropertiesIsTheSameForParameterisedConstructorAsForDefaultConstructor()
+ {
+ string json = @"{A:""Test""}";
+
+ var withoutParameterisedConstructor = JsonConvert.DeserializeObject<DTOWithoutParameterisedConstructor>(json);
+ var withParameterisedConstructor = JsonConvert.DeserializeObject<DTOWithParameterisedConstructor>(json);
+ Assert.AreEqual(withoutParameterisedConstructor.B, withParameterisedConstructor.B);
+ }
}
}
\ No newline at end of file diff --git a/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs b/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs index 97b41b5..1ac9ee3 100644 --- a/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs +++ b/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs @@ -865,8 +865,7 @@ namespace Newtonsoft.Json.Serialization Type objectType = contract.UnderlyingType;
- // create a dictionary to put retrieved values into
- IDictionary<JsonProperty, object> propertyValues = contract.Properties.Where(p => !p.Ignored).ToDictionary(kv => kv, kv => (object)null);
+ IDictionary<JsonProperty, object> propertyValues = new Dictionary<JsonProperty, object>();
bool exit = false;
do
|