-Fixed serializing types with hidden properties
This commit is contained in:
@@ -5112,6 +5112,23 @@ keyword such as type of business.""
|
||||
Assert.AreEqual(d1.Count, d2.Count);
|
||||
Assert.AreEqual(d1[0], d2[0]);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void SerializeInheritanceHierarchyWithDuplicateProperty()
|
||||
{
|
||||
Bb b = new Bb();
|
||||
b.no = true;
|
||||
Aa a = b;
|
||||
a.no = int.MaxValue;
|
||||
|
||||
string json = JsonConvert.SerializeObject(b);
|
||||
|
||||
Assert.AreEqual(@"{""no"":true}", json);
|
||||
|
||||
Bb b2 = JsonConvert.DeserializeObject<Bb>(json);
|
||||
|
||||
Assert.AreEqual(true, b2.no);
|
||||
}
|
||||
}
|
||||
|
||||
public class DecimalTestClass
|
||||
@@ -5146,6 +5163,16 @@ keyword such as type of business.""
|
||||
public Uri Uri { get; set; }
|
||||
}
|
||||
|
||||
class Aa
|
||||
{
|
||||
public int no;
|
||||
}
|
||||
|
||||
class Bb : Aa
|
||||
{
|
||||
public bool no;
|
||||
}
|
||||
|
||||
#if !(NET35 || NET20 || SILVERLIGHT || WINDOWS_PHONE)
|
||||
[JsonObject(MemberSerialization.OptIn)]
|
||||
public class GameObject
|
||||
|
||||
@@ -808,6 +808,7 @@ namespace Newtonsoft.Json.Serialization
|
||||
{
|
||||
JsonProperty property = new JsonProperty();
|
||||
property.PropertyType = ReflectionUtils.GetMemberUnderlyingType(member);
|
||||
property.DeclaringType = member.DeclaringType;
|
||||
property.ValueProvider = CreateMemberValueProvider(member);
|
||||
|
||||
bool allowNonPublicAccess;
|
||||
|
||||
@@ -41,6 +41,12 @@ namespace Newtonsoft.Json.Serialization
|
||||
/// <value>The name of the property.</value>
|
||||
public string PropertyName { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the type that declared this property.
|
||||
/// </summary>
|
||||
/// <value>The type that declared this property.</value>
|
||||
public Type DeclaringType { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the order of serialization and deserialization of a member.
|
||||
/// </summary>
|
||||
|
||||
@@ -73,12 +73,30 @@ namespace Newtonsoft.Json.Serialization
|
||||
|
||||
JsonProperty existingProperty = this[property.PropertyName];
|
||||
|
||||
if (!existingProperty.Ignored)
|
||||
throw new JsonSerializationException(
|
||||
"A member with the name '{0}' already exists on '{1}'. Use the JsonPropertyAttribute to specify another name.".FormatWith(CultureInfo.InvariantCulture, property.PropertyName, _type));
|
||||
if (existingProperty.Ignored)
|
||||
{
|
||||
// remove ignored property so it can be replaced in collection
|
||||
Remove(existingProperty);
|
||||
return;
|
||||
}
|
||||
|
||||
// remove ignored property so it can be replaced in collection
|
||||
Remove(existingProperty);
|
||||
if (property.DeclaringType != null && existingProperty.DeclaringType != null)
|
||||
{
|
||||
if (property.DeclaringType.IsSubclassOf(existingProperty.DeclaringType))
|
||||
{
|
||||
// current property is on a derived class and hides the existing
|
||||
Remove(existingProperty);
|
||||
return;
|
||||
}
|
||||
if (existingProperty.DeclaringType.IsSubclassOf(property.DeclaringType))
|
||||
{
|
||||
// current property is hidden by the existing so don't add it
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
throw new JsonSerializationException(
|
||||
"A member with the name '{0}' already exists on '{1}'. Use the JsonPropertyAttribute to specify another name.".FormatWith(CultureInfo.InvariantCulture, property.PropertyName, _type));
|
||||
}
|
||||
|
||||
Add(property);
|
||||
|
||||
Reference in New Issue
Block a user