From b5f5db5624e67fc803523bd1696a6d3a9530d508 Mon Sep 17 00:00:00 2001 From: JamesNK Date: Sat, 1 Oct 2011 11:10:11 +0000 Subject: [PATCH] -Improve TypeNameHandling.Auto to skip adding an unneeded type name for collection interfaces --- .../Properties/AssemblyInfo.cs | 2 +- .../Serialization/TypeNameHandlingTests.cs | 46 +++++++++++++++++++ .../Properties/AssemblyInfo.cs | 5 +- .../JsonSerializerInternalWriter.cs | 11 ++++- 4 files changed, 57 insertions(+), 7 deletions(-) diff --git a/Src/Newtonsoft.Json.Tests/Properties/AssemblyInfo.cs b/Src/Newtonsoft.Json.Tests/Properties/AssemblyInfo.cs index a83aef1d..d9aade55 100644 --- a/Src/Newtonsoft.Json.Tests/Properties/AssemblyInfo.cs +++ b/Src/Newtonsoft.Json.Tests/Properties/AssemblyInfo.cs @@ -66,5 +66,5 @@ using System.Runtime.InteropServices; // by using the '*' as shown below: [assembly: AssemblyVersion("4.0.3.0")] #if !PocketPC -[assembly: AssemblyFileVersion("4.0.3.14201")] +[assembly: AssemblyFileVersion("4.0.3.14202")] #endif diff --git a/Src/Newtonsoft.Json.Tests/Serialization/TypeNameHandlingTests.cs b/Src/Newtonsoft.Json.Tests/Serialization/TypeNameHandlingTests.cs index dc71f3fc..64193804 100644 --- a/Src/Newtonsoft.Json.Tests/Serialization/TypeNameHandlingTests.cs +++ b/Src/Newtonsoft.Json.Tests/Serialization/TypeNameHandlingTests.cs @@ -41,6 +41,52 @@ namespace Newtonsoft.Json.Tests.Serialization { public class TypeNameHandlingTests : TestFixtureBase { + public class Wrapper + { + public IList Array { get; set; } + public IDictionary Dictionary { get; set; } + } + + [Test] + public void sdfsdf() + { + Wrapper wrapper = new Wrapper(); + wrapper.Array = new List + { + new EmployeeReference() + }; + wrapper.Dictionary = new Dictionary + { + { "First", new EmployeeReference() } + }; + + string json = JsonConvert.SerializeObject(wrapper, Formatting.Indented, new JsonSerializerSettings + { + TypeNameHandling = TypeNameHandling.Auto + }); + + Assert.AreEqual(@"{ + ""Array"": [ + { + ""$id"": ""1"", + ""Name"": null, + ""Manager"": null + } + ], + ""Dictionary"": { + ""First"": { + ""$id"": ""2"", + ""Name"": null, + ""Manager"": null + } + } +}", json); + + Wrapper w2 = JsonConvert.DeserializeObject(json); + Assert.IsInstanceOfType(typeof(List), w2.Array); + Assert.IsInstanceOfType(typeof(Dictionary), w2.Dictionary); + } + [Test] public void WriteTypeNameForObjects() { diff --git a/Src/Newtonsoft.Json/Properties/AssemblyInfo.cs b/Src/Newtonsoft.Json/Properties/AssemblyInfo.cs index 35ab33f8..ce577bd6 100644 --- a/Src/Newtonsoft.Json/Properties/AssemblyInfo.cs +++ b/Src/Newtonsoft.Json/Properties/AssemblyInfo.cs @@ -55,11 +55,8 @@ using System.Security; [assembly: InternalsVisibleTo("Newtonsoft.Json.Tests, PublicKey=0024000004800000940000000602000000240000525341310004000001000100f561df277c6c0b497d629032b410cdcf286e537c054724f7ffa0164345f62b3e642029d7a80cc351918955328c4adc8a048823ef90b0cf38ea7db0d729caf2b633c3babe08b0310198c1081995c19029bc675193744eab9d7345b8a67258ec17d112cebdbbb2a281487dceeafb9d83aa930f32103fbe1d2911425bc5744002c7")] #endif - [assembly: InternalsVisibleTo("Newtonsoft.Json.Dynamic, PublicKey=0024000004800000940000000602000000240000525341310004000001000100cbd8d53b9d7de30f1f1278f636ec462cf9c254991291e66ebb157a885638a517887633b898ccbcf0d5c5ff7be85a6abe9e765d0ac7cd33c68dac67e7e64530e8222101109f154ab14a941c490ac155cd1d4fcba0fabb49016b4ef28593b015cab5937da31172f03f67d09edda404b88a60023f062ae71d0b2e4438b74cc11dc9")] - - [assembly: AssemblyDescription("")] [assembly: AssemblyConfiguration("")] [assembly: AssemblyCompany("Newtonsoft")] @@ -88,7 +85,7 @@ using System.Security; // by using the '*' as shown below: [assembly: AssemblyVersion("4.0.3.0")] #if !PocketPC -[assembly: AssemblyFileVersion("4.0.3.14201")] +[assembly: AssemblyFileVersion("4.0.3.14202")] #endif [assembly: CLSCompliant(true)] diff --git a/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalWriter.cs b/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalWriter.cs index 8608fead..b5514192 100644 --- a/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalWriter.cs +++ b/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalWriter.cs @@ -540,8 +540,15 @@ namespace Newtonsoft.Json.Serialization if (member != null) { - if ((member.TypeNameHandling ?? Serializer.TypeNameHandling) == TypeNameHandling.Auto && contract.UnderlyingType != member.PropertyType) - return true; + if ((member.TypeNameHandling ?? Serializer.TypeNameHandling) == TypeNameHandling.Auto + // instance and property type are different + && contract.UnderlyingType != member.PropertyType) + { + JsonContract memberTypeContract = Serializer.ContractResolver.ResolveContract(member.PropertyType); + // instance type and the property's type's contract default type are different (no need to put the type in JSON because the type will be created by default) + if (contract.UnderlyingType != memberTypeContract.CreatedType) + return true; + } } else if (collectionValueContract != null) {