From f304f7215face6705b2e63437e1c54d78dab9204 Mon Sep 17 00:00:00 2001 From: James Newton-King Date: Fri, 11 Oct 2013 20:43:07 +1300 Subject: [PATCH] -Increment version to 5.0.7 -Added default value and required support to constructor deserialization --- Build/Newtonsoft.Json.nuspec | 6 +- Build/build.ps1 | 13 ++--- Build/install.ps1 | 2 +- .../Properties/AssemblyInfo.cs | 2 +- .../Serialization/JsonSerializerTest.cs | 57 +++++++++++++++++++ .../Properties/AssemblyInfo.cs | 2 +- .../JsonSerializerInternalReader.cs | 15 +++++ 7 files changed, 84 insertions(+), 13 deletions(-) diff --git a/Build/Newtonsoft.Json.nuspec b/Build/Newtonsoft.Json.nuspec index b516ac0c..e3a2caf2 100644 --- a/Build/Newtonsoft.Json.nuspec +++ b/Build/Newtonsoft.Json.nuspec @@ -2,13 +2,13 @@ Newtonsoft.Json - 5.0.6 + 5.0.7 Json.NET Json.NET is a popular high-performance JSON framework for .NET James Newton-King en-US - http://james.newtonking.com/projects/json-net.aspx - http://json.codeplex.com/license + http://james.newtonking.com/json + https://github.com/JamesNK/Newtonsoft.Json/blob/master/LICENSE.md json \ No newline at end of file diff --git a/Build/build.ps1 b/Build/build.ps1 index 011bd9a9..976869c5 100644 --- a/Build/build.ps1 +++ b/Build/build.ps1 @@ -1,7 +1,7 @@ properties { - $zipFileName = "Json50r6.zip" + $zipFileName = "Json50r7.zip" $majorVersion = "4.5" - $majorWithReleaseVersion = "5.0.6" + $majorWithReleaseVersion = "5.0.7" $version = GetVersion $majorWithReleaseVersion $signAssemblies = $false $signKeyPath = "D:\Development\Releases\newtonsoft.snk" @@ -76,12 +76,11 @@ task Package -depends Build { if ($buildNuGet) { - New-Item -Path $workingDir\NuGet -ItemType Directory - #New-Item -Path $workingDir\NuGet\tools -ItemType Directory - + New-Item -Path $workingDir\NuGet -ItemType Directory Copy-Item -Path "$buildDir\Newtonsoft.Json.nuspec" -Destination $workingDir\NuGet\Newtonsoft.Json.nuspec -recurse - - #Copy-Item -Path "$buildDir\install.ps1" -Destination $workingDir\NuGet\tools\install.ps1 -recurse + + New-Item -Path $workingDir\NuGet\tools -ItemType Directory + Copy-Item -Path "$buildDir\install.ps1" -Destination $workingDir\NuGet\tools\install.ps1 -recurse foreach ($build in $builds) { diff --git a/Build/install.ps1 b/Build/install.ps1 index 4550d277..dfc78f05 100644 --- a/Build/install.ps1 +++ b/Build/install.ps1 @@ -5,7 +5,7 @@ param($installPath, $toolsPath, $package, $project) try { - $url = "http://www.json.net" + $url = "http://james.newtonking.com/json" $dte2 = Get-Interface $dte ([EnvDTE80.DTE2]) if ($dte2.ActiveWindow.Caption -eq "Package Manager Console") diff --git a/Src/Newtonsoft.Json.Tests/Properties/AssemblyInfo.cs b/Src/Newtonsoft.Json.Tests/Properties/AssemblyInfo.cs index 97774fe0..da510de6 100644 --- a/Src/Newtonsoft.Json.Tests/Properties/AssemblyInfo.cs +++ b/Src/Newtonsoft.Json.Tests/Properties/AssemblyInfo.cs @@ -75,4 +75,4 @@ using System.Security; // You can specify all the values or you can default the Revision and Build Numbers // by using the '*' as shown below: [assembly: AssemblyVersion("4.5.0.0")] -[assembly: AssemblyFileVersion("5.0.6.16606")] +[assembly: AssemblyFileVersion("5.0.7.16611")] diff --git a/Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs b/Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs index 7fa12ddd..202a6cbc 100644 --- a/Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs +++ b/Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs @@ -24,6 +24,7 @@ #endregion using System; +using System.ComponentModel; #if !(NET35 || NET20 || SILVERLIGHT) using System.Collections.Concurrent; #endif @@ -2468,6 +2469,62 @@ keyword such as type of business."" Assert.AreEqual("lastName", o.lastName); } + public sealed class ConstructorAndDefaultValueAttributeTestClass + { + public ConstructorAndDefaultValueAttributeTestClass(string testProperty1) + { + this.TestProperty1 = testProperty1; + } + + public string TestProperty1 { get; set; } + + [DefaultValue(21)] + public int TestProperty2 { get; set; } + + } + + [Test] + public void PopulateDefaultValueWhenUsingConstructor() + { + string json = "{ 'testProperty1': 'value' }"; + + ConstructorAndDefaultValueAttributeTestClass c = JsonConvert.DeserializeObject(json, new JsonSerializerSettings + { + DefaultValueHandling = DefaultValueHandling.Populate + }); + Assert.AreEqual("value", c.TestProperty1); + Assert.AreEqual(21, c.TestProperty2); + + c = JsonConvert.DeserializeObject(json, new JsonSerializerSettings + { + DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate + }); + Assert.AreEqual("value", c.TestProperty1); + Assert.AreEqual(21, c.TestProperty2); + } + + public sealed class ConstructorAndRequiredTestClass + { + public ConstructorAndRequiredTestClass(string testProperty1) + { + this.TestProperty1 = testProperty1; + } + + public string TestProperty1 { get; set; } + + [JsonProperty(Required = Required.AllowNull)] + public int TestProperty2 { get; set; } + } + + [Test] + public void RequiredWhenUsingConstructor() + { + string json = "{ 'testProperty1': 'value' }"; + + ExceptionAssert.Throws("Required property 'TestProperty2' not found in JSON. Path '', line 1, position 28.", + () => JsonConvert.DeserializeObject(json)); + } + [Test] public void DeserializePropertiesOnToNonDefaultConstructor() { diff --git a/Src/Newtonsoft.Json/Properties/AssemblyInfo.cs b/Src/Newtonsoft.Json/Properties/AssemblyInfo.cs index 640de9c9..8e04a229 100644 --- a/Src/Newtonsoft.Json/Properties/AssemblyInfo.cs +++ b/Src/Newtonsoft.Json/Properties/AssemblyInfo.cs @@ -92,6 +92,6 @@ using System.Security; // You can specify all the values or you can default the Revision and Build Numbers // by using the '*' as shown below: [assembly: AssemblyVersion("4.5.0.0")] -[assembly: AssemblyFileVersion("5.0.6.16606")] +[assembly: AssemblyFileVersion("5.0.7.16611")] [assembly: CLSCompliant(true)] diff --git a/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs b/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs index bccf9687..0a3ff215 100644 --- a/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs +++ b/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs @@ -1444,6 +1444,11 @@ To fix this error either change the environment to be fully trusted, change the { ValidationUtils.ArgumentNotNull(constructorInfo, "constructorInfo"); + // only need to keep a track of properies presence if they are required or a value should be defaulted if missing + Dictionary propertiesPresence = (contract.HasRequiredOrDefaultValueProperties || HasFlag(Serializer._defaultValueHandling, DefaultValueHandling.Populate)) + ? contract.Properties.ToDictionary(m => m, m => PropertyPresence.None) + : null; + Type objectType = contract.UnderlyingType; if (TraceWriter != null && TraceWriter.LevelFilter >= TraceLevel.Info) @@ -1461,6 +1466,14 @@ To fix this error either change the environment to be fully trusted, change the constructorParameters[matchingConstructorParameter] = propertyValue.Value; else remainingPropertyValues.Add(propertyValue); + + if (propertiesPresence != null) + { + // map from constructor property to normal property + var property = propertiesPresence.Keys.FirstOrDefault(p => p.PropertyName == propertyValue.Key.PropertyName); + if (property != null) + propertiesPresence[property] = (propertyValue.Value == null) ? PropertyPresence.Null : PropertyPresence.Value; + } } object createdObject = constructorInfo.Invoke(constructorParameters.Values.ToArray()); @@ -1520,6 +1533,8 @@ To fix this error either change the environment to be fully trusted, change the } } + EndObject(createdObject, reader, contract, reader.Depth, propertiesPresence); + OnDeserialized(reader, contract, createdObject); return createdObject; }