/// <summary> /// Packs the data and into a response object and sends it to the callback /// </summary> /// <typeparam name="T"></typeparam> private T PackAndSendResponse <T>(string rawData) where T : AsanaObject { var u = AsanaObject.Create(typeof(T)); Parsing.Deserialize(GetDataAsDict(rawData), u, _host); return((T)u); }
/// <summary> /// Packs the data and into a response object and sends it to the callback /// </summary> /// <typeparam name="T"></typeparam> /// <param name="callback"></param> internal void PackAndSendResponse <T>(string rawData, AsanaResponseEventHandler callback) where T : AsanaObject { var u = AsanaObject.Create(typeof(T)); Parsing.Deserialize(GetDataAsDict(rawData), u, this); callback(u); }
/// <summary> /// Creates a new T without requiring a public constructor /// </summary> /// <param name="t"></param> /// <param name="ID"></param> /// <returns></returns> internal static AsanaObject Create(Type t, Int64 ID) { AsanaObject o = Create(t); o.ID = ID; return(o); }
static public Task Save(this AsanaObject obj, AsanaFunction function) { if (obj.Host == null) { throw new NullReferenceException("This AsanaObject does not have a host associated with it so you must specify one when saving."); } return(obj.Host.Save(obj, function)); }
private T PackOriginalContent <T>(string rawData) where T : AsanaObject { var data = Json.Deserialize(rawData) as Dictionary <string, object>; var u = AsanaObject.Create(typeof(T)); Parsing.Deserialize(data, u, _host); return((T)u); }
public static Task Delete(this AsanaObject obj) { if (obj.Host == null) { throw new NullReferenceException("This AsanaObject does not have a host associated with it so you must specify one when saving."); } return(obj.Host.Delete(obj)); }
static public void Save(this AsanaObject obj) { if (obj.Host == null) { throw new NullReferenceException("This AsanaObject does not have a host associated with it so you must specify one when saving."); } obj.Host.Save(obj, null); }
/// <summary> /// Creates a new T without requiring a public constructor /// </summary> /// <param name="t"></param> internal static AsanaObject Create(Type t) { try { AsanaObject o = (AsanaObject)Activator.CreateInstance(t, true); return(o); } catch (System.Exception) { return(null); } }
/// <summary> /// /// </summary> /// <typeparam name="T"></typeparam> /// <param name="source"></param> /// <param name="name"></param> /// <returns></returns> static private AsanaObject SafeAssignAsanaObject <T>(Dictionary <string, object> source, string name, Asana host) where T : AsanaObject { T value = null; if (source.ContainsKey(name) && source[name] != null) { var obj = source[name] as Dictionary <string, object>; value = (T)AsanaObject.Create(typeof(T)); Parsing.Deserialize(obj, (value as AsanaObject), host); } return(value); }
/// <summary> /// Packs the data and into a collection object and sends it to the callback /// </summary> /// <typeparam name="T"></typeparam> /// <param name="callback"></param> internal void PackAndSendResponseCollection <T>(string rawData, AsanaCollectionResponseEventHandler callback) where T : AsanaObject { var k = GetDataAsDictArray(rawData); AsanaObjectCollection collection = new AsanaObjectCollection(); foreach (var j in k) { var t = AsanaObject.Create(typeof(T)); Parsing.Deserialize(j, t, this); collection.Add(t); } callback(collection); }
/// <summary> /// Packs the data and into a collection object and sends it to the callback /// </summary> /// <typeparam name="T"></typeparam> private IAsanaObjectCollection <T> PackAndSendResponseCollection <T>(string rawData) where T : AsanaObject { var k = GetDataAsDictArray(rawData); AsanaObjectCollection <T> collection = new AsanaObjectCollection <T>(); foreach (var j in k) { var t = (T)AsanaObject.Create(typeof(T)); Parsing.Deserialize(j, t, _host); collection.Add(t); } return(collection); }
/// <summary> /// /// </summary> /// <typeparam name="T"></typeparam> /// <param name="source"></param> /// <param name="name"></param> /// <returns></returns> static private T[] SafeAssignAsanaObjectArray <T>(Dictionary <string, object> source, string name, Asana host) where T : AsanaObject { T[] value = null; if (source.ContainsKey(name) && source[name] != null) { var list = source[name] as List <object>; value = new T[list.Count]; for (int i = 0; i < list.Count; ++i) { T newObj = (T)AsanaObject.Create(typeof(T)); Parsing.Deserialize(list[i] as Dictionary <string, object>, (newObj as AsanaObject), host); value[i] = newObj; } } return(value); }
static public void Save(this AsanaObject obj, Asana host, AsanaFunction function) { host.Save(obj, function); }
public static Task Delete(this AsanaObject obj, Asana host) { return(host.Delete(obj)); }
public static Task Save(this AsanaObject obj, Asana host) { return(host.Save(obj, null)); }
public static Task Save(this AsanaObject obj, Asana host, AsanaFunction function) { return(host.Save(obj, function)); }
//static internal T SetValueOnBaseType<T> /// <summary> /// Deserializes a dictionary based on AsanaDataAttributes /// </summary> /// <param name="data"></param> /// <param name="obj"></param> static internal Dictionary <string, object> Serialize(AsanaObject obj, bool asString, bool dirtyOnly) { var dict = new Dictionary <string, object>(); foreach (var p in obj.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance)) { try { var cas = p.GetCustomAttributes(typeof(AsanaDataAttribute), false); if (cas.Length == 0) { continue; } AsanaDataAttribute ca = cas[0] as AsanaDataAttribute; if (ca.Flags.HasFlag(SerializationFlags.Omit)) { continue; } bool required = ca.Flags.HasFlag(SerializationFlags.Required); object value = p.GetValue(obj, new object[] { }); if (dirtyOnly && !obj.IsDirty(ca.Name, value)) { continue; } bool present = ValidateSerializableValue(ref value, ca, p); if (present == false) { if (!required) { continue; } else { throw new MissingFieldException("Couldn't save object because it was missing a required field: " + p.Name); } } if (value.GetType().IsArray) { int count = 0; foreach (var x in (object[])value) { dict.Add(ca.Name + "[" + count + "]", asString ? x.ToString() : x); count++; } } else { dict.Add(ca.Name, asString ? value.ToString() : value); } } catch (Exception) { } } return(dict); }
static public void Save(this AsanaObject obj, Asana host) { host.Save(obj, null); }
/// <summary> /// Deserializes a dictionary based on AsanaDataAttributes /// </summary> /// <param name="data"></param> /// <param name="obj"></param> static internal void Deserialize(Dictionary <string, object> data, AsanaObject obj, Asana host) { foreach (var p in obj.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance)) { if (p.Name == "Host") { if (obj.Host != host) { p.SetValue(obj, host, new object[] { }); } continue; } try { var cas = p.GetCustomAttributes(typeof(AsanaDataAttribute), false); if (cas.Length == 0) { continue; } AsanaDataAttribute ca = cas[0] as AsanaDataAttribute; if (!data.ContainsKey(ca.Name)) { continue; } if (p.PropertyType == typeof(string)) { p.SetValue(obj, SafeAssignString(data, ca.Name), null); } else { Type t = p.PropertyType.IsArray ? p.PropertyType.GetElementType() : p.PropertyType; MethodInfo method = null; if (typeof(AsanaObject).IsAssignableFrom(t)) { method = typeof(Parsing).GetMethod(p.PropertyType.IsArray ? "SafeAssignAsanaObjectArray" : "SafeAssignAsanaObject", BindingFlags.NonPublic | BindingFlags.Static).MakeGenericMethod(new Type[] { t }); } else { method = typeof(Parsing).GetMethod(p.PropertyType.IsArray ? "SafeAssignArray" : "SafeAssign", BindingFlags.NonPublic | BindingFlags.Static).MakeGenericMethod(new Type[] { t }); } var methodResult = method.Invoke(null, new object[] { data, ca.Name, host }); // this check handle base-class properties if (p.DeclaringType != obj.GetType()) { var p2 = p.DeclaringType.GetProperty(p.Name); p2.SetValue(obj, methodResult, null); } else { p.SetValue(obj, methodResult, null); } } } catch (Exception) { } } }