-Fixed setting default values onto properties already set in constructor when deserializing

This commit is contained in:
James Newton-King
2015-12-06 20:26:55 +13:00
parent 92102f9a33
commit a64616f9f9
2 changed files with 248 additions and 136 deletions
@@ -383,6 +383,79 @@ namespace Newtonsoft.Json.Tests.Serialization
[DefaultValue(6)]
public int IntValue { get; set; }
}
public sealed class FieldExportFormat
{
private string _format;
private ExportFormat? _exportFormat;
[JsonProperty]
public ExportFormat? ExportFormat
{
get { return _exportFormat; }
private set
{
if (!value.HasValue)
{
throw new ArgumentNullException("ExportFormat");
}
_exportFormat = value;
_format = null;
}
}
[JsonProperty]
public string Format
{
get { return _format; }
private set
{
if (value == null)
{
throw new ArgumentNullException("Format");
}
_format = value;
_exportFormat = null;
}
}
public FieldExportFormat(string format)
{
Format = format;
}
public FieldExportFormat(ExportFormat exportFormat)
{
ExportFormat = exportFormat;
}
[JsonConstructor]
private FieldExportFormat(string format, ExportFormat? exportFormat)
{
if (exportFormat.HasValue)
{
ExportFormat = exportFormat;
}
else
{
Format = format;
}
}
}
[Test]
public void DontSetPropertiesDefaultValueUsedInConstructor()
{
string json = @"{""ExportFormat"":0}";
FieldExportFormat o = JsonConvert.DeserializeObject<FieldExportFormat>(json, new JsonSerializerSettings
{
DefaultValueHandling = DefaultValueHandling.Populate
});
Assert.AreEqual(ExportFormat.Default, o.ExportFormat);
Assert.AreEqual(null, o.Format);
}
}
#if !NET20
@@ -529,4 +602,11 @@ namespace Newtonsoft.Json.Tests.Serialization
public object Object { get; set; }
}
#endif
public enum ExportFormat
{
Default = 0,
Currency,
Integer
}
}