-Improve TypeNameHandling.Auto to skip adding an unneeded type name for collection interfaces
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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()
|
||||
{
|
||||
|
||||
@@ -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)]
|
||||
|
||||
@@ -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)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user