+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.
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 ());
}
public override void SetMemberValue (object owner, object value)
{
+ Console.Error.WriteLine ("SetMemberValue ({0},{1}", property, value);
property.SetValue (owner, value, null);
}
}
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 ()
{
[DataMember]
string Member1 = "foo";
}
+
+ [Serializable]
+ public class KeyValueTestData {
+ public List<KeyValuePair<string,string>> TestData = new List<KeyValuePair<string,string>>();
+ }
+
}
[DataContract]