[System] Fixes UdpClient.Receive with IPv6 endpoint
[mono.git] / mcs / class / System.ServiceModel.Web / System.Runtime.Serialization.Json / TypeMap.cs
index f83c177fa65f336ce20e7f239e382087753fc757..8378a70c220dcf9f7cba75b9347ccb19a6e91d31 100644 (file)
@@ -33,6 +33,7 @@ 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;
@@ -66,10 +67,6 @@ namespace System.Runtime.Serialization.Json
                        if (IsPrimitiveType (type))
                                return null;
 
-#if MOONLIGHT
-                       if (ExternalTypeMap.HasType (type))
-                               return new ExternalTypeMap (type);
-#endif
                        return CreateDefaultTypeMap (type);
                }
 
@@ -89,7 +86,7 @@ namespace System.Runtime.Serialization.Json
                                if (!fi.IsStatic)
                                        l.Add (new TypeMapField (fi, null));
                        foreach (var pi in type.GetProperties ())
-                               if (pi.CanRead && pi.CanWrite && !pi.GetGetMethod ().IsStatic && pi.GetIndexParameters ().Length == 0)
+                               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 ());
@@ -97,20 +94,40 @@ namespace System.Runtime.Serialization.Json
 
                internal static bool IsDictionary (Type type)
                {
-                       if (type.GetInterface ("System.Collections.IDictionary", false) != 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.IDictionary`2", false) != 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;
                        return false;
                }
 
-               internal static bool IsCollection (Type type)
+               internal static bool IsEnumerable (Type type)
                {
-                       if (type.GetInterface ("System.Collections.IList", false) != null)
+                       if (type.IsGenericType && 
+                               type.GetGenericTypeDefinition() == typeof (LinkedList<>))
                                return true;
-                       if (type.GetInterface ("System.Collections.Generic.IList`1", false) != null)
+                       
+                       if (IsPrimitiveType (type) || IsDictionary (type))
+                               return false;
+                       
+                       Type inter;
+                       inter = type.GetInterface ("System.Collections.Generic.IReadOnlyCollection`1", false);
+                       if (inter != null)
                                return true;
-                       if (type.GetInterface ("System.Collections.Generic.ICollection`1", false) != null)
+                       
+                       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;
                }
@@ -122,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)
@@ -137,13 +156,13 @@ namespace System.Runtime.Serialization.Json
                        }
 
                        if (dca != null) {
-                               foreach (PropertyInfo pi in type.GetProperties (BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)) {
+                               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)
                                                continue;
-                                       if (IsCollection (pi.PropertyType)) {
+                                       if (IsEnumerable (pi.PropertyType) || IsDictionary (pi.PropertyType)) {
                                                if (!pi.CanRead)
                                                        throw new InvalidDataContractException (String.Format ("Property {0} must have a getter", pi));
                                        }
@@ -176,14 +195,23 @@ namespace System.Runtime.Serialization.Json
                                        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 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);
@@ -192,14 +220,34 @@ 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 virtual object Deserialize (JsonSerializationReader jsr)
+               public virtual object Deserialize (JsonSerializationReader jsr, object o)
                {
                        XmlReader reader = jsr.Reader;
                        bool isNull = reader.GetAttribute ("type") == "null";
 
-                       object ret = isNull ? null : FormatterServices.GetUninitializedObject (type);
+                       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> ();
@@ -212,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;
@@ -259,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
@@ -280,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));
                }
        }
 
@@ -306,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);
+                               }
+                       }
                }
        }
 }