From 7a7027a77908f6bdc40b24bfa456ba6a8f49b66b Mon Sep 17 00:00:00 2001 From: James Newton-King Date: Sat, 18 Feb 2017 19:15:38 +1300 Subject: [PATCH] -Obsoleted FormatterAssemblyStyle in non-full .NET assemblies -Tests --- Build/build.ps1 | 18 --- .../Newtonsoft.Json.Tests.Net40.project.json | 3 +- ...ewtonsoft.Json.Tests.Portable.project.json | 3 +- .../Newtonsoft.Json.Tests.Roslyn.csproj | 3 +- .../Newtonsoft.Json.Tests.project.json | 3 +- .../Serialization/JsonSerializerTest.cs | 106 +++++++++++++----- .../Serialization/TypeNameHandlingTests.cs | 31 +++++ Src/Newtonsoft.Json/FormatterAssemblyStyle.cs | 1 + 8 files changed, 120 insertions(+), 48 deletions(-) diff --git a/Build/build.ps1 b/Build/build.ps1 index 7967688d..223fa83b 100644 --- a/Build/build.ps1 +++ b/Build/build.ps1 @@ -368,24 +368,6 @@ function Edit-XmlNodes { } } -function Update-Project { - param ( - [string] $projectPath, - [string] $sign - ) - - $file = switch($sign) { $true { $signKeyPath } default { $null } } - $signed = switch($sign) { $true { ";SIGNED" } default { "" } } - $constants = "CODE_ANALYSIS;TRACE$signed" - $json = (Get-Content $projectPath) -join "`n" | ConvertFrom-Json - $options = @{"warningsAsErrors" = $true; "xmlDoc" = $true; "keyFile" = $file; "define" = ($constants -split ";") } - Add-Member -InputObject $json -MemberType NoteProperty -Name "buildOptions" -Value $options -Force - - $json.version = GetNuGetVersion - - ConvertTo-Json $json -Depth 10 | Set-Content $projectPath -} - function Execute-Command($command) { $currentRetry = 0 $success = $false diff --git a/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.Net40.project.json b/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.Net40.project.json index 8ce5c394..842b95d1 100644 --- a/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.Net40.project.json +++ b/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.Net40.project.json @@ -1,7 +1,8 @@ { "dependencies": { "NUnit": "2.6.2", - "FSharp.Core": "4.0.0.1" + "FSharp.Core": "4.0.0.1", + "System.ValueTuple": "4.3.0" }, "frameworks": { "net40": {} diff --git a/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.Portable.project.json b/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.Portable.project.json index b370a58b..b12fe9d2 100644 --- a/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.Portable.project.json +++ b/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.Portable.project.json @@ -2,7 +2,8 @@ "dependencies": { "FSharp.Core": "4.0.0.1", "NUnit": "2.6.2", - "System.Collections.Immutable": "1.1.37" + "System.Collections.Immutable": "1.1.37", + "System.ValueTuple": "4.3.0" }, "frameworks": { "net45": {} diff --git a/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.Roslyn.csproj b/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.Roslyn.csproj index e73f10ec..fff2423d 100644 --- a/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.Roslyn.csproj +++ b/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.Roslyn.csproj @@ -97,7 +97,8 @@ - + + diff --git a/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.project.json b/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.project.json index f9804862..e3c90a67 100644 --- a/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.project.json +++ b/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.project.json @@ -4,7 +4,8 @@ "FSharp.Core": "4.0.0.1", "NUnit": "2.6.2", "System.Buffers": "4.0.0", - "System.Collections.Immutable": "1.1.37" + "System.Collections.Immutable": "1.1.37", + "System.ValueTuple": "4.3.0" }, "frameworks": { "net45": {} diff --git a/Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs b/Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs index d88a7d86..ffcbcd81 100644 --- a/Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs +++ b/Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs @@ -764,6 +764,47 @@ namespace Newtonsoft.Json.Tests.Serialization Assert.AreEqual("Name!", c2.Name); } +#if !(NET20 || NET35) + [Test] + public void SerializeValueTuple() + { + ValueTuple t = ValueTuple.Create(1, 2, "string"); + + string json = JsonConvert.SerializeObject(t, Formatting.Indented); + + StringAssert.AreEqual(@"{ + ""Item1"": 1, + ""Item2"": 2, + ""Item3"": ""string"" +}", json); + + ValueTuple t2 = JsonConvert.DeserializeObject>(json); + + Assert.AreEqual(1, t2.Item1); + Assert.AreEqual(2, t2.Item2); + Assert.AreEqual("string", t2.Item3); + } +#endif + + [Test] + public void DeserializeStructWithConstructorAttribute() + { + ImmutableStructWithConstructorAttribute result = JsonConvert.DeserializeObject("{ \"Value\": \"working\" }"); + + Assert.AreEqual("working", result.Value); + } + + public struct ImmutableStructWithConstructorAttribute + { + [JsonConstructor] + public ImmutableStructWithConstructorAttribute(string value) + { + Value = value; + } + + public string Value { get; } + } + #if !(DNXCORE50 || NET20) [MetadataType(typeof(CustomerValidation))] public partial class CustomerWithMetadataType @@ -780,9 +821,10 @@ namespace Newtonsoft.Json.Tests.Serialization [Test] public void SerializeMetadataType() { - CustomerWithMetadataType c = new CustomerWithMetadataType(); - c.UpdatedBy_Id = Guid.NewGuid(); - + CustomerWithMetadataType c = new CustomerWithMetadataType() + { + UpdatedBy_Id = Guid.NewGuid() + }; string json = JsonConvert.SerializeObject(c); Assert.AreEqual("{}", json); @@ -841,9 +883,14 @@ namespace Newtonsoft.Json.Tests.Serialization [Test] public void SerializeMetadataType2() { - FaqItem c = new FaqItem(); - c.FaqId = 1; - c.Sections.Add(new FaqSection()); + FaqItem c = new FaqItem() + { + FaqId = 1, + Sections = + { + new FaqSection() + } + }; string json = JsonConvert.SerializeObject(c, Formatting.Indented); @@ -4734,11 +4781,12 @@ Path '', line 1, position 1."); [Test] public void DataContractJsonSerializerTest() { - DataContractJsonSerializerTestClass c = new DataContractJsonSerializerTestClass(); - c.TimeSpanProperty = new TimeSpan(200, 20, 59, 30, 900); - c.GuidProperty = new Guid("66143115-BE2A-4a59-AF0A-348E1EA15B1E"); - c.AnimalProperty = new Human() { Ethnicity = "European" }; - + DataContractJsonSerializerTestClass c = new DataContractJsonSerializerTestClass() + { + TimeSpanProperty = new TimeSpan(200, 20, 59, 30, 900), + GuidProperty = new Guid("66143115-BE2A-4a59-AF0A-348E1EA15B1E"), + AnimalProperty = new Human() { Ethnicity = "European" } + }; MemoryStream ms = new MemoryStream(); DataContractJsonSerializer serializer = new DataContractJsonSerializer( typeof(DataContractJsonSerializerTestClass), @@ -5833,9 +5881,10 @@ Path '', line 1, position 1."); Assert.AreEqual("{\"Offset\":\"2000-01-01T00:00:00+06:00\"}", serializeObject); - JsonTextReader reader = new JsonTextReader(new StringReader(serializeObject)); - reader.DateParseHandling = DateParseHandling.None; - + JsonTextReader reader = new JsonTextReader(new StringReader(serializeObject)) + { + DateParseHandling = DateParseHandling.None + }; JsonSerializer serializer = new JsonSerializer(); var deserializeObject = serializer.Deserialize(reader); @@ -6124,13 +6173,16 @@ Path '', line 1, position 1."); [Test] public void DeserializeNullableStruct() { - NullableStructPropertyClass nullableStructPropertyClass = new NullableStructPropertyClass(); - nullableStructPropertyClass.Foo1 = new StructISerializable() { Name = "foo 1" }; - nullableStructPropertyClass.Foo2 = new StructISerializable() { Name = "foo 2" }; - - NullableStructPropertyClass barWithNull = new NullableStructPropertyClass(); - barWithNull.Foo1 = new StructISerializable() { Name = "foo 1" }; - barWithNull.Foo2 = null; + NullableStructPropertyClass nullableStructPropertyClass = new NullableStructPropertyClass() + { + Foo1 = new StructISerializable() { Name = "foo 1" }, + Foo2 = new StructISerializable() { Name = "foo 2" } + }; + NullableStructPropertyClass barWithNull = new NullableStructPropertyClass() + { + Foo1 = new StructISerializable() { Name = "foo 1" }, + Foo2 = null + }; //throws error on deserialization because bar1.Foo2 is of type Foo? string s = JsonConvert.SerializeObject(nullableStructPropertyClass); @@ -7603,11 +7655,13 @@ Path '', line 1, position 1."); [Test] public void MetroBlogPost() { - Product product = new Product(); - product.Name = "Apple"; - product.ExpiryDate = new DateTime(2012, 4, 1); - product.Price = 3.99M; - product.Sizes = new[] { "Small", "Medium", "Large" }; + Product product = new Product() + { + Name = "Apple", + ExpiryDate = new DateTime(2012, 4, 1), + Price = 3.99M, + Sizes = new[] { "Small", "Medium", "Large" } + }; string json = JsonConvert.SerializeObject(product); //{ diff --git a/Src/Newtonsoft.Json.Tests/Serialization/TypeNameHandlingTests.cs b/Src/Newtonsoft.Json.Tests/Serialization/TypeNameHandlingTests.cs index 088c8140..586d339f 100644 --- a/Src/Newtonsoft.Json.Tests/Serialization/TypeNameHandlingTests.cs +++ b/Src/Newtonsoft.Json.Tests/Serialization/TypeNameHandlingTests.cs @@ -61,6 +61,37 @@ namespace Newtonsoft.Json.Tests.Serialization [TestFixture] public class TypeNameHandlingTests : TestFixtureBase { +#if !(NET20 || NET35) + [Test] + public void SerializeValueTupleWithTypeName() + { + string tupleRef = ReflectionUtils.GetTypeName(typeof(ValueTuple), TypeNameAssemblyFormatHandling.Simple, null); + + ValueTuple t = ValueTuple.Create(1, 2, "string"); + + string json = JsonConvert.SerializeObject(t, Formatting.Indented, new JsonSerializerSettings + { + TypeNameHandling = TypeNameHandling.All + }); + + StringAssert.AreEqual(@"{ + ""$type"": """ + tupleRef + @""", + ""Item1"": 1, + ""Item2"": 2, + ""Item3"": ""string"" +}", json); + + ValueTuple t2 = (ValueTuple)JsonConvert.DeserializeObject(json, new JsonSerializerSettings + { + TypeNameHandling = TypeNameHandling.All + }); + + Assert.AreEqual(1, t2.Item1); + Assert.AreEqual(2, t2.Item2); + Assert.AreEqual("string", t2.Item3); + } +#endif + #if !(NET20 || NET35 || NET40) public class KnownAutoTypes { diff --git a/Src/Newtonsoft.Json/FormatterAssemblyStyle.cs b/Src/Newtonsoft.Json/FormatterAssemblyStyle.cs index a405908d..db9972df 100644 --- a/Src/Newtonsoft.Json/FormatterAssemblyStyle.cs +++ b/Src/Newtonsoft.Json/FormatterAssemblyStyle.cs @@ -6,6 +6,7 @@ namespace System.Runtime.Serialization.Formatters /// /// Indicates the method that will be used during deserialization for locating and loading assemblies. /// + [Obsolete("FormatterAssemblyStyle is obsolete. Use TypeNameAssemblyFormatHandling instead.")] public enum FormatterAssemblyStyle { ///