Browse Source

2010-07-06 Atsushi Enomoto <[email protected]>

	* TypeMap.cs :
	  Do not try to add static members as serialization targets.
	  Support OnDeserializing and OnDeserialized. Fixed bug #615800.

	* DataContractJsonSerializerTest.cs : add test for bug #615800.


svn path=/trunk/mcs/; revision=159958
Atsushi Eno 15 years ago
parent
commit
3e59168e9d

+ 6 - 0
mcs/class/System.ServiceModel.Web/System.Runtime.Serialization.Json/ChangeLog

@@ -1,3 +1,9 @@
+2010-07-06  Atsushi Enomoto  <[email protected]>
+
+	* TypeMap.cs :
+	  Do not try to add static members as serialization targets.
+	  Support OnDeserializing and OnDeserialized. Fixed bug #615800.
+
 2010-07-06  Atsushi Enomoto  <[email protected]>
 
 	* JsonSerializationWriter.cs : it cannot serialize DateTime in

+ 20 - 2
mcs/class/System.ServiceModel.Web/System.Runtime.Serialization.Json/TypeMap.cs

@@ -82,9 +82,10 @@ namespace System.Runtime.Serialization.Json
 		{
 			var l = new List<TypeMapMember> ();
 			foreach (var fi in type.GetFields ())
-				l.Add (new TypeMapField (fi, null));
+				if (!fi.IsStatic)
+					l.Add (new TypeMapField (fi, null));
 			foreach (var pi in type.GetProperties ())
-				if (pi.CanRead && pi.CanWrite)
+				if (pi.CanRead && pi.CanWrite && !pi.GetGetMethod ().IsStatic)
 					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 ());
@@ -148,13 +149,26 @@ namespace System.Runtime.Serialization.Json
 		string element;
 		TypeMapMember [] members;
 
+		static readonly Type [] deser_methods_args = new Type [] { typeof (StreamingContext) };
+		const BindingFlags binding_flags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance;
+
 		public TypeMap (Type type, string element, TypeMapMember [] orderedMembers)
 		{
 			this.type = type;
 			this.element = element;
 			this.members = orderedMembers;
+
+			foreach (var mi in type.GetMethods (binding_flags)) {
+				if (mi.GetCustomAttributes (typeof (OnDeserializingAttribute), false).Length > 0)
+					OnDeserializing = mi;
+				else if (mi.GetCustomAttributes (typeof (OnDeserializedAttribute), false).Length > 0)
+					OnDeserialized = mi;
+			}
 		}
 
+		public MethodInfo OnDeserializing { get; set; }
+		public MethodInfo OnDeserialized { get; set; }
+
 		public void Serialize (JsonSerializationWriter outputter, object graph)
 		{
 			foreach (TypeMapMember member in members) {
@@ -172,6 +186,8 @@ namespace System.Runtime.Serialization.Json
 			bool isNull = reader.GetAttribute ("type") == "null";
 
 			object ret = isNull ? null : FormatterServices.GetUninitializedObject (type);
+			if (ret != null && OnDeserializing != null)
+				OnDeserializing.Invoke (ret, new object [] {new StreamingContext (StreamingContextStates.All)});
 			Dictionary<TypeMapMember,bool> filled = new Dictionary<TypeMapMember,bool> ();
 
 			reader.ReadStartElement ();
@@ -192,6 +208,8 @@ namespace System.Runtime.Serialization.Json
 					reader.Skip ();
 			}
 			reader.ReadEndElement ();
+			if (ret != null && OnDeserialized != null)
+				OnDeserialized.Invoke (ret, new object [] {new StreamingContext (StreamingContextStates.All)});
 			return ret;
 		}
 	}

+ 4 - 0
mcs/class/System.ServiceModel.Web/Test/System.Runtime.Serialization.Json/ChangeLog

@@ -1,3 +1,7 @@
+2010-07-06  Atsushi Enomoto  <[email protected]>
+
+	* DataContractJsonSerializerTest.cs : add test for bug #615800.
+
 2010-07-06  Atsushi Enomoto  <[email protected]>
 
 	* DataContractJsonSerializerTest.cs : add test for bug #615801.

+ 46 - 0
mcs/class/System.ServiceModel.Web/Test/System.Runtime.Serialization.Json/DataContractJsonSerializerTest.cs

@@ -1373,6 +1373,20 @@ namespace MonoTests.System.Runtime.Serialization.Json
 			a = (ClassA) ds.ReadObject (stream);
 			Assert.IsNull (a.B, "#1");
 		}
+
+		[Test]
+		public void OnDeserializationMethods ()
+		{
+			var ds = new DataContractJsonSerializer (typeof (GSPlayerListErg));
+			var obj = new GSPlayerListErg ();
+			var ms = new MemoryStream ();
+			ds.WriteObject (ms, obj);
+			ms.Position = 0;
+			ds.ReadObject (ms);
+			Assert.IsTrue (GSPlayerListErg.A, "A");
+			Assert.IsTrue (GSPlayerListErg.B, "B");
+			Assert.IsTrue (GSPlayerListErg.C, "C");
+		}
 	}
 
 	public class TestData
@@ -1553,6 +1567,38 @@ namespace MonoTests.System.Runtime.Serialization.Json
 	public class ClassB
 	{
 	}
+
+	public class GSPlayerListErg
+	{
+		public GSPlayerListErg ()
+		{
+			Init ();
+		}
+
+		void Init ()
+		{
+			C = true;
+		}
+
+		[OnDeserializing]
+		public void OnDeserializing (StreamingContext c)
+		{
+			A = true;
+			Init ();
+		}
+
+		[OnDeserialized]
+		void OnDeserialized (StreamingContext c)
+		{
+			B = true;
+		}
+
+		public static bool A, B, C;
+
+		[DataMember (Name = "T")]
+		public long CodedServerTimeUTC { get; set; }
+		public DateTime ServerTimeUTC { get; set; }
+	}
 }
 
 [DataContract]