Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/mono/Newtonsoft.Json.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJamesNK <james@newtonking.com>2012-01-16 12:00:48 +0400
committerJamesNK <james@newtonking.com>2012-01-16 12:00:48 +0400
commit2941344044b1340d982a5f7c5edfd2b8882f7840 (patch)
tree5ff04704923684170e31129f6bc3efc010b55057
parent45cbd34daa10f8ceea480e20278f487947a6b117 (diff)
-Fixed serializing types with hidden properties
-rw-r--r--Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs27
-rw-r--r--Src/Newtonsoft.Json/Serialization/DefaultContractResolver.cs1
-rw-r--r--Src/Newtonsoft.Json/Serialization/JsonProperty.cs6
-rw-r--r--Src/Newtonsoft.Json/Serialization/JsonPropertyCollection.cs28
4 files changed, 57 insertions, 5 deletions
diff --git a/Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs b/Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs
index 14df3b6..7d5d738 100644
--- a/Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs
+++ b/Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs
@@ -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
diff --git a/Src/Newtonsoft.Json/Serialization/DefaultContractResolver.cs b/Src/Newtonsoft.Json/Serialization/DefaultContractResolver.cs
index 7505033..b3a5fb2 100644
--- a/Src/Newtonsoft.Json/Serialization/DefaultContractResolver.cs
+++ b/Src/Newtonsoft.Json/Serialization/DefaultContractResolver.cs
@@ -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;
diff --git a/Src/Newtonsoft.Json/Serialization/JsonProperty.cs b/Src/Newtonsoft.Json/Serialization/JsonProperty.cs
index e0d055b..f2240ec 100644
--- a/Src/Newtonsoft.Json/Serialization/JsonProperty.cs
+++ b/Src/Newtonsoft.Json/Serialization/JsonProperty.cs
@@ -42,6 +42,12 @@ namespace Newtonsoft.Json.Serialization
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>
/// <value>The numeric order of serialization or deserialization.</value>
diff --git a/Src/Newtonsoft.Json/Serialization/JsonPropertyCollection.cs b/Src/Newtonsoft.Json/Serialization/JsonPropertyCollection.cs
index 5c2d525..6274659 100644
--- a/Src/Newtonsoft.Json/Serialization/JsonPropertyCollection.cs
+++ b/Src/Newtonsoft.Json/Serialization/JsonPropertyCollection.cs
@@ -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;
+ }
+
+ 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;
+ }
+ }
- // remove ignored property so it can be replaced in collection
- Remove(existingProperty);
+ 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);