diff options
author | JamesNK <james@newtonking.com> | 2011-10-01 15:10:11 +0400 |
---|---|---|
committer | JamesNK <james@newtonking.com> | 2011-10-01 15:10:11 +0400 |
commit | b5f5db5624e67fc803523bd1696a6d3a9530d508 (patch) | |
tree | 7509c50c14fbd1c21c0d9b914a96fd8fd99df97e | |
parent | 3fcf02a653a614ab2d58df0b0c5e099e5f233f5f (diff) |
-Improve TypeNameHandling.Auto to skip adding an unneeded type name for collection interfaces
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 a83aef1..d9aade5 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 dc71f3f..6419380 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<EmployeeReference> Array { get; set; }
+ public IDictionary<string, EmployeeReference> Dictionary { get; set; }
+ }
+
+ [Test]
+ public void sdfsdf()
+ {
+ Wrapper wrapper = new Wrapper();
+ wrapper.Array = new List<EmployeeReference>
+ {
+ new EmployeeReference()
+ };
+ wrapper.Dictionary = new Dictionary<string, EmployeeReference>
+ {
+ { "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<Wrapper>(json);
+ Assert.IsInstanceOfType(typeof(List<EmployeeReference>), w2.Array);
+ Assert.IsInstanceOfType(typeof(Dictionary<string, EmployeeReference>), w2.Dictionary);
+ }
+
[Test]
public void WriteTypeNameForObjects()
{
diff --git a/Src/Newtonsoft.Json/Properties/AssemblyInfo.cs b/Src/Newtonsoft.Json/Properties/AssemblyInfo.cs index 35ab33f..ce577bd 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 8608fea..b551419 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)
{
|