using System.IO;
using System.Linq;
using System.Reflection;
+using System.Runtime.CompilerServices;
using System.Runtime.Serialization;
using System.Text;
using System.Xml;
if (IsPrimitiveType (type))
return null;
-#if MOONLIGHT
- if (ExternalTypeMap.HasType (type))
- return new ExternalTypeMap (type);
-#endif
return CreateDefaultTypeMap (type);
}
if (!fi.IsStatic)
l.Add (new TypeMapField (fi, null));
foreach (var pi in type.GetProperties ())
- if (pi.CanRead && pi.CanWrite && !pi.GetGetMethod ().IsStatic)
+ 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", false) != null)
+ if (type.GetInterface ("System.Collections.IDictionary", false) != null)
return true;
- if (type.GetInterface ("System.Collections.Generic.IList`1", false) != null)
+ if (type.GetInterface ("System.Collections.Generic.IDictionary`2", false) != null)
return true;
- if (type.GetInterface ("System.Collections.Generic.ICollection`1", false) != null)
+ return false;
+ }
+
+ internal static bool IsCollection (Type type)
+ {
+ if (IsPrimitiveType (type) || IsDictionary (type))
+ return false;
+ if (type.GetInterface ("System.Collections.IEnumerable", false) != null)
return true;
return false;
}
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)
}
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;
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);
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.IsCollection (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)
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> ();