in System.Runtime.Serialization.Json:
authorChris Toshok <toshok@novell.com>
Sat, 12 Dec 2009 00:14:23 +0000 (00:14 -0000)
committerChris Toshok <toshok@novell.com>
Sat, 12 Dec 2009 00:14:23 +0000 (00:14 -0000)
2009-12-11  Chris Toshok  <toshok@ximian.com>

* TypeMap.cs (CreateDefaultTypeMap): only include non-public
property info when dealing with KeyValuePair<,>.  This is *not*
the way MS handles it, but we emulate things much better with this
hack.

in test/System.Runtime.Serialization.Json/:
2009-12-11  Chris Toshok  <toshok@ximian.com>

* DataContractJsonSerializerTest.cs: add a test case for
non-public properties.

svn path=/trunk/mcs/; revision=148184

mcs/class/System.ServiceModel.Web/System.Runtime.Serialization.Json/ChangeLog
mcs/class/System.ServiceModel.Web/System.Runtime.Serialization.Json/TypeMap.cs
mcs/class/System.ServiceModel.Web/Test/System.Runtime.Serialization.Json/ChangeLog
mcs/class/System.ServiceModel.Web/Test/System.Runtime.Serialization.Json/DataContractJsonSerializerTest.cs

index 9e4a854e68a7e5deca8716f94fe7e71f4637c429..3aaeda496b5883fc33dc63a5e39bbb82c985407d 100644 (file)
@@ -1,3 +1,10 @@
+2009-12-11  Chris Toshok  <toshok@ximian.com>
+
+       * TypeMap.cs (CreateDefaultTypeMap): only include non-public
+       property info when dealing with KeyValuePair<,>.  This is *not*
+       the way MS handles it, but we emulate things much better with this
+       hack.
+
 2009-12-11  Atsushi Enomoto  <atsushi@ximian.com>
 
        * JsonReader.cs : e- and e+ was resulting in wrong parse error.
index 3d9d675fbb8a267b579b773129e742c3174d55b9..15b5895eba77f306bba76d3341d7323b6b22b886 100644 (file)
@@ -84,9 +84,28 @@ namespace System.Runtime.Serialization.Json
                        var l = new List<TypeMapMember> ();
                        foreach (var fi in type.GetFields ())
                                l.Add (new TypeMapField (fi, null));
-                       foreach (var pi in type.GetProperties (BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance))
+
+                       PropertyInfo[] properties;
+
+                       // FIXME: this hack for property visibility
+                       // emulates some of the special case code MS
+                       // has.  a stack trace seen in testing showed
+                       // a method called
+                       // ReadKeyValuePairOfstringstringFromJson, so
+                       // presumably they're dynamically creating
+                       // methods to deserialize certain (possibly
+                       // all generic?) types from json.
+
+                       // see DataContractJsonSerializerTest.TestNonpublicDeserialization.
+                       if (type == typeof (KeyValuePair<,>))
+                               properties = type.GetProperties (BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
+                       else 
+                               properties = type.GetProperties ();
+
+                       foreach (var pi in properties) {
                                if (pi.CanRead && pi.CanWrite)
                                        l.Add (new TypeMapProperty (pi, null));
+                       }
                        l.Sort ((x, y) => x.Order != y.Order ? x.Order - y.Order : String.Compare (x.Name, y.Name, StringComparison.Ordinal));
                        return new TypeMap (type, null, l.ToArray ());
                }
@@ -280,6 +299,7 @@ namespace System.Runtime.Serialization.Json
 
                public override void SetMemberValue (object owner, object value)
                {
+                       Console.Error.WriteLine ("SetMemberValue ({0},{1}", property, value);
                        property.SetValue (owner, value, null);
                }
        }
index b0e984b93970dde48dbf155858f69f99fbac0d3f..9a66e73812915a849bbf1366eb7435c606cd245b 100644 (file)
@@ -1,3 +1,8 @@
+2009-12-11  Chris Toshok  <toshok@ximian.com>
+
+       * DataContractJsonSerializerTest.cs: add a test case for
+       non-public properties.
+
 2009-12-11  Atsushi Enomoto  <atsushi@ximian.com>
 
        * JsonReaderTest.cs : another number parse case.
index caee64a688707e572d8a9f9b620260bf3a84582d..46a07eb652d08834acdfae4d16a7ba98bbdc2616 100644 (file)
@@ -1256,6 +1256,22 @@ namespace MonoTests.System.Runtime.Serialization.Json
                        Assert.AreEqual (@"{""Bar"":null,""Foo"":""foo""}", s, "#1");
                }
 
+               [Test]
+               public void TestNonpublicDeserialization ()
+               {
+                       string s1= @"{""Bar"":""bar"", ""Foo"":""foo"", ""Baz"":""baz""}";
+                       TestData o1 = ((TestData)(new DataContractJsonSerializer (typeof (TestData)).ReadObject (JsonReaderWriterFactory.CreateJsonReader (Encoding.UTF8.GetBytes (s1), new XmlDictionaryReaderQuotas ()))));
+
+                       Assert.AreEqual (null, o1.Baz, "#1");
+
+                        string s2 = @"{""TestData"":[{""key"":""key1"",""value"":""value1""}]}";
+                        KeyValueTestData o2 = ((KeyValueTestData)(new DataContractJsonSerializer (typeof (KeyValueTestData)).ReadObject (JsonReaderWriterFactory.CreateJsonReader (Encoding.UTF8.GetBytes (s2), new XmlDictionaryReaderQuotas ()))));
+
+                       Assert.AreEqual (1, o2.TestData.Count, "#2");
+                       Assert.AreEqual ("key1", o2.TestData[0].Key, "#3");
+                       Assert.AreEqual ("value1", o2.TestData[0].Value, "#4");
+               }
+
                // [Test] use this case if you want to check lame silverlight parser behavior. Seealso #549756
                public void QuotelessDeserialization ()
                {
@@ -1418,6 +1434,12 @@ namespace MonoTests.System.Runtime.Serialization.Json
                [DataMember]
                string Member1 = "foo";
        }
+
+       [Serializable]
+       public class KeyValueTestData {
+               public List<KeyValuePair<string,string>> TestData = new List<KeyValuePair<string,string>>();
+       }
+
 }
 
 [DataContract]