diff options
author | JamesNK <james@newtonking.com> | 2010-10-16 14:45:36 +0400 |
---|---|---|
committer | JamesNK <james@newtonking.com> | 2010-10-16 14:45:36 +0400 |
commit | 1703992773b08532d2adad1bf073c849b35a8f88 (patch) | |
tree | 71e967060e7172f85431b001cd090b4b71d47bc4 /Src | |
parent | 336f14b3d2cd59a854a3b8565d99bdc3ff0d8c1c (diff) |
-Added covariance to IJEnumerable type parameter
-Fixed inaccessible private getter/setters on base classes
Diffstat (limited to 'Src')
-rw-r--r-- | Src/Newtonsoft.Json.Tests/Linq/LinqToJsonTest.cs | 15 | ||||
-rw-r--r-- | Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs | 49 | ||||
-rw-r--r-- | Src/Newtonsoft.Json/Linq/IJEnumerable.cs | 6 | ||||
-rw-r--r-- | Src/Newtonsoft.Json/Utilities/ReflectionUtils.cs | 17 |
4 files changed, 83 insertions, 4 deletions
diff --git a/Src/Newtonsoft.Json.Tests/Linq/LinqToJsonTest.cs b/Src/Newtonsoft.Json.Tests/Linq/LinqToJsonTest.cs index 749d8b3..fcaf7f0 100644 --- a/Src/Newtonsoft.Json.Tests/Linq/LinqToJsonTest.cs +++ b/Src/Newtonsoft.Json.Tests/Linq/LinqToJsonTest.cs @@ -698,6 +698,21 @@ keyword such as type of business."" Assert.AreEqual(new DateTime(2000, 10, 15, 5, 5, 5, DateTimeKind.Utc), d);
}
+#if !(NET20 || NET35 || SILVERLIGHT)
+ [Test]
+ public void CovariantIJEnumerable()
+ {
+ IEnumerable<JObject> o = new[]
+ {
+ JObject.FromObject(new {First = 1, Second = 2}),
+ JObject.FromObject(new {First = 1, Second = 2})
+ };
+
+ IJEnumerable<JToken> values = o.Properties();
+ Assert.AreEqual(4, values.Count());
+ }
+#endif
+
[Test]
public void ChildrenExtension()
{
diff --git a/Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs b/Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs index 74d89f5..8178da5 100644 --- a/Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs +++ b/Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs @@ -3697,5 +3697,54 @@ keyword such as type of business."" Assert.AreEqual(2, obj.Position.Y);
Assert.AreEqual(3, obj.Position.Z);
}
+
+ [JsonObject(MemberSerialization.OptIn)]
+ public class Derived : Base
+ {
+ [JsonProperty]
+ public string IDoWork { get; private set; }
+
+ private Derived() { }
+
+ internal Derived(string dontWork, string doWork)
+ : base(dontWork)
+ {
+ IDoWork = doWork;
+ }
+ }
+
+ [JsonObject(MemberSerialization.OptIn)]
+ public class Base
+ {
+ [JsonProperty]
+ public string IDontWork { get; private set; }
+
+ protected Base() { }
+
+ internal Base(string dontWork)
+ {
+ IDontWork = dontWork;
+ }
+ }
+
+ [Test]
+ public void PrivateSetterOnBaseClassProperty()
+ {
+ var derived = new Derived("meh", "woo");
+
+ var settings = new JsonSerializerSettings
+ {
+ TypeNameHandling = TypeNameHandling.Objects,
+ ConstructorHandling = ConstructorHandling.AllowNonPublicDefaultConstructor
+ };
+
+ string json = JsonConvert.SerializeObject(derived, Formatting.Indented, settings);
+
+ var meh = JsonConvert.DeserializeObject<Base>(json, settings);
+
+ Assert.AreEqual(((Derived)meh).IDoWork, "woo");
+ Assert.AreEqual(meh.IDontWork, "meh");
+ }
+
}
}
\ No newline at end of file diff --git a/Src/Newtonsoft.Json/Linq/IJEnumerable.cs b/Src/Newtonsoft.Json/Linq/IJEnumerable.cs index a758fcb..6291e99 100644 --- a/Src/Newtonsoft.Json/Linq/IJEnumerable.cs +++ b/Src/Newtonsoft.Json/Linq/IJEnumerable.cs @@ -9,7 +9,11 @@ namespace Newtonsoft.Json.Linq /// Represents a collection of <see cref="JToken"/> objects.
/// </summary>
/// <typeparam name="T">The type of token</typeparam>
- public interface IJEnumerable<T> : IEnumerable<T> where T : JToken
+ public interface IJEnumerable<
+#if !(NET20 || NET35 || SILVERLIGHT)
+ out
+#endif
+ T> : IEnumerable<T> where T : JToken
{
/// <summary>
/// Gets the <see cref="IJEnumerable{JToken}"/> with the specified key.
diff --git a/Src/Newtonsoft.Json/Utilities/ReflectionUtils.cs b/Src/Newtonsoft.Json/Utilities/ReflectionUtils.cs index 7419ba8..e2bb83b 100644 --- a/Src/Newtonsoft.Json/Utilities/ReflectionUtils.cs +++ b/Src/Newtonsoft.Json/Utilities/ReflectionUtils.cs @@ -880,10 +880,21 @@ namespace Newtonsoft.Json.Utilities {
ValidationUtils.ArgumentNotNull(targetType, "targetType");
- List<MemberInfo> propertyInfos = new List<MemberInfo>(targetType.GetProperties(bindingAttr));
+ List<PropertyInfo> propertyInfos = new List<PropertyInfo>(targetType.GetProperties(bindingAttr));
GetChildPrivateProperties(propertyInfos, targetType, bindingAttr);
- return propertyInfos.Cast<PropertyInfo>();
+ // a base class private getter/setter will be inaccessable unless the property was gotten from the base class
+ for (int i = 0; i < propertyInfos.Count; i++)
+ {
+ PropertyInfo member = propertyInfos[i];
+ if (member.DeclaringType != targetType)
+ {
+ PropertyInfo declaredMember = member.DeclaringType.GetProperty(member.Name, bindingAttr);
+ propertyInfos[i] = declaredMember;
+ }
+ }
+
+ return propertyInfos;
}
public static BindingFlags RemoveFlag(this BindingFlags bindingAttr, BindingFlags flag)
@@ -893,7 +904,7 @@ namespace Newtonsoft.Json.Utilities : bindingAttr;
}
- private static void GetChildPrivateProperties(IList<MemberInfo> initialProperties, Type targetType, BindingFlags bindingAttr)
+ private static void GetChildPrivateProperties(IList<PropertyInfo> initialProperties, Type targetType, BindingFlags bindingAttr)
{
// fix weirdness with private PropertyInfos only being returned for the current Type
// find base type properties and add them to result
|