[System] Fixes UdpClient.Receive with IPv6 endpoint
[mono.git] / mcs / class / System.ServiceModel.Web / System.Runtime.Serialization.Json / TypeMap.cs
index 9bf3583ebcdd3dbc658f9b934cd3478025b9d8c4..8378a70c220dcf9f7cba75b9347ccb19a6e91d31 100644 (file)
@@ -31,7 +31,10 @@ using System.Collections.Generic;
 using System.Collections.ObjectModel;
 using System.Globalization;
 using System.IO;
+using System.Linq;
 using System.Reflection;
+using System.Runtime.CompilerServices;
+using System.Runtime.Serialization;
 using System.Text;
 using System.Xml;
 
@@ -80,20 +83,51 @@ 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 (true).IsStatic && pi.GetIndexParameters ().Length == 0)
                                        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 ());
                }
 
-               static bool IsCollection (Type type)
+               internal static bool IsDictionary (Type type)
                {
-                       if (type.GetInterface ("System.Collections.IList") != null)
+                       Type inter;
+                       inter = type.GetInterface ("System.Collections.IDictionary", false);
+                       if (inter != null
+                               && type.GetMethod ("Add", new Type[] { typeof (object), typeof (object) }) != null)
                                return true;
-                       if (type.GetInterface ("System.Collections.Generic.IList`1") != null)
+                       
+                       inter = type.GetInterface ("System.Collections.Generic.IDictionary`2", false);
+                       if (inter != null
+                               && type.GetMethod ("Add", new Type[] { inter.GetGenericArguments() [0], 
+                                                                          inter.GetGenericArguments() [1] }) != null)
                                return true;
-                       if (type.GetInterface ("System.Collections.Generic.ICollection`1") != null)
+                       return false;
+               }
+
+               internal static bool IsEnumerable (Type type)
+               {
+                       if (type.IsGenericType && 
+                               type.GetGenericTypeDefinition() == typeof (LinkedList<>))
+                               return true;
+                       
+                       if (IsPrimitiveType (type) || IsDictionary (type))
+                               return false;
+                       
+                       Type inter;
+                       inter = type.GetInterface ("System.Collections.Generic.IReadOnlyCollection`1", false);
+                       if (inter != null)
+                               return true;
+                       
+                       inter = type.GetInterface ("System.Collections.IEnumerable", false);
+                       if (inter != null && type.GetMethod ("Add", new Type[] { typeof (object) }) != null)
+                               return true;
+                       
+                       inter = type.GetInterface ("System.Collections.Generic.IEnumerable`1", false);
+                       if (inter != null && type.GetMethod ("Add", new Type[] { inter.GetGenericArguments() [0] }) != null)
                                return true;
                        return false;
                }
@@ -105,7 +139,9 @@ namespace System.Runtime.Serialization.Json
 
                        List<TypeMapMember> members = new List<TypeMapMember> ();
 
-                       foreach (FieldInfo fi in type.GetFields (BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)) {
+                       foreach (FieldInfo fi in type.GetFields (binding_flags)) {
+                               if (fi.GetCustomAttributes (typeof (CompilerGeneratedAttribute), false).Length > 0)
+                                       continue;
                                if (dca != null) {
                                        object [] atts = fi.GetCustomAttributes (typeof (DataMemberAttribute), true);
                                        if (atts.Length == 0)
@@ -113,20 +149,20 @@ namespace System.Runtime.Serialization.Json
                                        DataMemberAttribute dma = (DataMemberAttribute) atts [0];
                                        members.Add (new TypeMapField (fi, dma));
                                } else {
-                                       if (fi.GetCustomAttributes (typeof (NonSerializedAttribute), false).Length > 0)
+                                       if (fi.GetCustomAttributes (typeof (IgnoreDataMemberAttribute), false).Length > 0)
                                                continue;
                                        members.Add (new TypeMapField (fi, null));
                                }
                        }
 
                        if (dca != null) {
-                               foreach (PropertyInfo pi in type.GetProperties (BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)) {
-                                               continue;
+                               foreach (PropertyInfo pi in type.GetProperties (binding_flags)) {
                                        object [] atts = pi.GetCustomAttributes (typeof (DataMemberAttribute), true);
                                        if (atts.Length == 0)
                                                continue;
                                        if (pi.GetIndexParameters ().Length > 0)
-                                       if (IsCollection (pi.PropertyType)) {
+                                               continue;
+                                       if (IsEnumerable (pi.PropertyType) || IsDictionary (pi.PropertyType)) {
                                                if (!pi.CanRead)
                                                        throw new InvalidDataContractException (String.Format ("Property {0} must have a getter", pi));
                                        }
@@ -137,7 +173,7 @@ namespace System.Runtime.Serialization.Json
                                }
                        }
 
-                       members.Sort (delegate (TypeMapMember m1, TypeMapMember m2) { return m1.Order - m2.Order; });
+                       members.Sort (delegate (TypeMapMember m1, TypeMapMember m2) { return m1.Order != m2.Order ? m1.Order - m2.Order : String.CompareOrdinal (m1.Name, m2.Name); });
                        return new TypeMap (type, dca == null ? null : dca.Name, members.ToArray ());
                }
 
@@ -145,15 +181,38 @@ 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;
+                               else if (mi.GetCustomAttributes (typeof (OnSerializingAttribute), false).Length > 0)
+                                       OnSerializing = mi;
+                               else if (mi.GetCustomAttributes (typeof (OnSerializedAttribute), false).Length > 0)
+                                       OnSerialized = mi;
+                       }
                }
 
-               public void Serialize (JsonSerializationWriter outputter, object graph)
+               public MethodInfo OnDeserializing { get; set; }
+               public MethodInfo OnDeserialized { get; set; }
+               public MethodInfo OnSerializing { get; set; }
+               public MethodInfo OnSerialized { get; set; }
+
+               public virtual void Serialize (JsonSerializationWriter outputter, object graph, string type)
                {
+                       if (OnSerializing != null)
+                               OnSerializing.Invoke (graph, new object [] {new StreamingContext (StreamingContextStates.All)});
+
+                       outputter.Writer.WriteAttributeString ("type", type);
                        foreach (TypeMapMember member in members) {
                                object memberObj = member.GetMemberOf (graph);
                                // FIXME: consider EmitDefaultValue
@@ -161,17 +220,36 @@ namespace System.Runtime.Serialization.Json
                                outputter.WriteObjectContent (memberObj, false, false);
                                outputter.Writer.WriteEndElement ();
                        }
+
+                       if (OnSerialized != null)
+                               OnSerialized.Invoke (graph, new object [] {new StreamingContext (StreamingContextStates.All)});
+               }
+
+               internal static object CreateInstance (Type type)
+               {
+                       if (TypeMap.IsDictionary (type)) {
+                               if (type.IsGenericType)
+                                       return Activator.CreateInstance (typeof (Dictionary<,>).MakeGenericType (type.GetGenericArguments ()));
+                               else
+                                       return new Hashtable ();
+                       } else if (TypeMap.IsEnumerable (type)) {
+                               if (type.IsGenericType)
+                                       return Activator.CreateInstance (typeof (List<>).MakeGenericType (type.GetGenericArguments ()));
+                               else
+                                       return new ArrayList ();
+                       }
+                       else
+                               return FormatterServices.GetUninitializedObject (type);
                }
 
-               public object Deserialize (JsonSerializationReader jsr)
+               public virtual object Deserialize (JsonSerializationReader jsr, object o)
                {
                        XmlReader reader = jsr.Reader;
-#if NET_2_1
-                       // should it reject non-public constructor?
-                       object ret = Activator.CreateInstance (type);
-#else
-                       object ret = Activator.CreateInstance (type, true);
-#endif
+                       bool isNull = reader.GetAttribute ("type") == "null";
+
+                       object ret = isNull ? null : CreateInstance (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 ();
@@ -182,7 +260,7 @@ namespace System.Runtime.Serialization.Json
                                        if (mm.Name == reader.LocalName && reader.NamespaceURI == String.Empty) {
                                                if (filled.ContainsKey (mm))
                                                        throw new SerializationException (String.Format ("Object content '{0}' for '{1}' already appeared in the reader", reader.LocalName, type));
-                                               mm.SetMemberValue (ret, jsr.ReadObject (mm.Type));
+                                               mm.SetMemberValue (ret, jsr);
                                                filled [mm] = true;
                                                consumed = true;
                                                break;
@@ -192,6 +270,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;
                }
        }
@@ -211,10 +291,9 @@ namespace System.Runtime.Serialization.Json
                        get { return dma == null ? mi.Name : dma.Name ?? mi.Name; }
                }
 
-               // FIXME: Fill 3.5 member in s.r.serialization.
-//             public bool EmitDefaultValue {
-//                     get { return dma != null && dma.EmitDefaultValue; }
-//             }
+               public bool EmitDefaultValue {
+                       get { return dma != null && dma.EmitDefaultValue; }
+               }
 
                public bool IsRequired {
                        get { return dma != null && dma.IsRequired; }
@@ -228,7 +307,7 @@ namespace System.Runtime.Serialization.Json
 
                public abstract object GetMemberOf (object owner);
 
-               public abstract void SetMemberValue (object owner, object value);
+               public abstract void SetMemberValue (object owner, JsonSerializationReader value);
        }
 
        class TypeMapField : TypeMapMember
@@ -249,10 +328,10 @@ namespace System.Runtime.Serialization.Json
                {
                        return field.GetValue (owner);
                }
-
-               public override void SetMemberValue (object owner, object value)
+               
+               public override void SetMemberValue (object owner, JsonSerializationReader jsr)
                {
-                       field.SetValue (owner, value);
+                       field.SetValue (owner, jsr.ReadObject (this.Type));
                }
        }
 
@@ -275,9 +354,21 @@ namespace System.Runtime.Serialization.Json
                        return property.GetValue (owner, null);
                }
 
-               public override void SetMemberValue (object owner, object value)
+               public override void SetMemberValue (object owner, JsonSerializationReader jsr)
                {
-                       property.SetValue (owner, value, null);
+                       var pSetter = this.property.GetSetMethod (true);
+                       if (pSetter != null) {
+                               property.SetValue (owner, jsr.ReadObject (this.Type), null);
+                               
+                       } else { // no setter
+                               var oldValue = property.GetValue (owner, null);
+                               try {
+                                       jsr.ReadObject (this.Type, oldValue);
+                               } catch (MissingMethodException e) {
+                                       throw new InvalidDataContractException (string.Format ("No set method for property '{0}' "
+                                               + "in type '{1}'.", this.property.Name, this.property.PropertyType.FullName), e);
+                               }
+                       }
                }
        }
 }