-Fixed passing default values to constructors

This commit is contained in:
James Newton-King
2015-05-03 14:17:25 +12:00
parent fbb74d57ef
commit 7e04b45894
5 changed files with 119 additions and 6 deletions
+1 -1
View File
@@ -92,13 +92,13 @@
<Compile Include="JsonDictionaryAttribute.cs" />
<Compile Include="JsonException.cs" />
<Compile Include="JsonExtensionDataAttribute.cs" />
<Compile Include="JsonRequiredAttribute.cs" />
<Compile Include="JsonIgnoreAttribute.cs" />
<Compile Include="JsonObjectAttribute.cs" />
<Compile Include="JsonPosition.cs" />
<Compile Include="JsonPropertyAttribute.cs" />
<Compile Include="JsonReader.cs" />
<Compile Include="JsonReaderException.cs" />
<Compile Include="JsonRequiredAttribute.cs" />
<Compile Include="JsonSerializationException.cs" />
<Compile Include="JsonSerializer.cs" />
<Compile Include="JsonSerializerSettings.cs" />
@@ -155,7 +155,7 @@ namespace Newtonsoft.Json.Serialization
{
foreach (JsonProperty property in Properties)
{
if (property.Required != Required.Default || ((property.DefaultValueHandling & DefaultValueHandling.Populate) == DefaultValueHandling.Populate) && property.Writable)
if (property.Required != Required.Default || (property.DefaultValueHandling & DefaultValueHandling.Populate) == DefaultValueHandling.Populate)
{
_hasRequiredOrDefaultValueProperties = true;
break;
@@ -1662,6 +1662,34 @@ namespace Newtonsoft.Json.Serialization
}
}
// handle giving default values to creator parameters
// this needs to happen before the call to creator
if (propertiesPresence != null)
{
foreach (KeyValuePair<JsonProperty, PropertyPresence> propertyPresence in propertiesPresence)
{
JsonProperty property = propertyPresence.Key;
PropertyPresence presence = propertyPresence.Value;
if (!property.Ignored && (presence == PropertyPresence.None || presence == PropertyPresence.Null))
{
if (property.PropertyContract == null)
property.PropertyContract = GetContractSafe(property.PropertyType);
if (HasFlag(property.DefaultValueHandling.GetValueOrDefault(Serializer._defaultValueHandling), DefaultValueHandling.Populate))
{
// have to use the name because property presence doesn't use creator properties
int i = contract.CreatorParameters.IndexOf(p => p.PropertyName == property.PropertyName);
if (i != -1)
{
creatorParameterValues[i] = EnsureType(reader, property.GetResolvedDefaultValue(), CultureInfo.InvariantCulture, property.PropertyContract, property.PropertyType);
}
}
}
}
}
object createdObject = creator(creatorParameterValues);
if (id != null)