-Fixed objects created with paramatized constructors to not set properties that aren't in JSON

This commit is contained in:
JamesNK
2011-04-21 23:18:19 +00:00
parent 1e75151dda
commit 2c190ec438
2 changed files with 34 additions and 2 deletions
@@ -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);
}
}
}
@@ -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