serialization of a property which was defined in several interfaces (#1586)

This commit is contained in:
testfirstcoder
2018-01-19 16:31:03 +13:00
committed by James Newton-King
parent 6dfc5c46e5
commit e1dfefe00b
2 changed files with 42 additions and 1 deletions
@@ -1817,6 +1817,41 @@ namespace Newtonsoft.Json.Tests.Serialization
Assert.AreEqual("444-1212", newName.pNumbers[1].phoneNumber); Assert.AreEqual("444-1212", newName.pNumbers[1].phoneNumber);
} }
[TestFixture]
public class MultipleDefinedPropertySerialization
{
[Test]
public void SerializePropertyDefinedInMultipleInterfaces()
{
const string propertyValue = "value";
var list = new List<ITestInterface> { new TestClass { Property = propertyValue } };
var json = JsonConvert.SerializeObject(list);
StringAssert.AreEqual($"[{{\"Property\":\"{propertyValue}\"}}]", json);
}
public interface IFirstInterface
{
string Property { get; set; }
}
public interface ISecondInterface
{
string Property { get; set; }
}
public interface ITestInterface : IFirstInterface, ISecondInterface
{
}
public class TestClass : ITestInterface
{
public string Property { get; set; }
}
}
[Test] [Test]
public void CustomCollectionSerialization() public void CustomCollectionSerialization()
{ {
@@ -104,6 +104,12 @@ namespace Newtonsoft.Json.Serialization
// current property is hidden by the existing so don't add it // current property is hidden by the existing so don't add it
return; return;
} }
if (_type.ImplementInterface(existingProperty.DeclaringType) && _type.ImplementInterface(property.DeclaringType))
{
// current property was already defined on another interface
return;
}
} }
} }