Exemplo n.º 1
0
        public virtual T Deserialize <T>(IRestResponse response)
        {
            if (string.IsNullOrEmpty(response.Content))
            {
                return(default(T));
            }
            XDocument xdoc = XDocument.Parse(response.Content);
            XElement  root = xdoc.Root;

            if (StringExtensions.HasValue(this.RootElement) && xdoc.Root != null)
            {
                root = xdoc.Root.Element(XmlExtensions.AsNamespaced(this.RootElement, this.Namespace));
            }
            if (!StringExtensions.HasValue(this.Namespace))
            {
                XmlDebuggingDeserializer.RemoveNamespace(xdoc);
            }
            T    instance = Activator.CreateInstance <T>();
            Type type     = instance.GetType();

            return(!ReflectionExtensions.IsSubclassOfRawGeneric(type, typeof(List <>)) ? (T)this.Map((object)instance, root) : (T)this.HandleListDerivative(root, type.Name, type));
        }
Exemplo n.º 2
0
 protected virtual object Map(object x, XElement root)
 {
     foreach (PropertyInfo prop in x.GetType().GetProperties())
     {
         try
         {
             Type type = prop.PropertyType;
             if ((type.IsPublic || type.IsNestedPublic) && prop.CanWrite)
             {
                 object[] customAttributes = prop.GetCustomAttributes(typeof(DeserializeAsAttribute), false);
                 XName    name             = customAttributes.Length <= 0
                     ? XmlExtensions.AsNamespaced(prop.Name, this.Namespace)
                     : XmlExtensions.AsNamespaced(((DeserializeAsAttribute)customAttributes[0]).Name,
                                                  this.Namespace);
                 object valueFromXml = this.GetValueFromXml(root, name, prop);
                 if (valueFromXml == null)
                 {
                     if (type.IsGenericType)
                     {
                         Type     t             = type.GetGenericArguments()[0];
                         XElement elementByName = this.GetElementByName(root, (XName)t.Name);
                         IList    list          = (IList)Activator.CreateInstance(type);
                         if (elementByName != null && root != null)
                         {
                             IEnumerable <XElement> elements = root.Elements(elementByName.Name);
                             this.PopulateListFromElements(t, elements, list);
                         }
                         prop.SetValue(x, (object)list, (object[])null);
                     }
                 }
                 else
                 {
                     if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable <>))
                     {
                         if (string.IsNullOrEmpty(valueFromXml.ToString()))
                         {
                             prop.SetValue(x, (object)null, (object[])null);
                             continue;
                         }
                         type = type.GetGenericArguments()[0];
                     }
                     if (type == typeof(bool))
                     {
                         string s = valueFromXml.ToString().ToLower();
                         prop.SetValue(x, (object)(bool)(XmlConvert.ToBoolean(s) ? true : false),
                                       (object[])null);
                     }
                     else if (type.IsPrimitive)
                     {
                         prop.SetValue(x, ReflectionExtensions.ChangeType(valueFromXml, type, this.Culture),
                                       (object[])null);
                     }
                     else if (type.IsEnum)
                     {
                         object enumValue = ReflectionExtensions.FindEnumValue(type, valueFromXml.ToString(),
                                                                               this.Culture);
                         prop.SetValue(x, enumValue, (object[])null);
                     }
                     else if (type == typeof(Uri))
                     {
                         Uri uri = new Uri(valueFromXml.ToString(), UriKind.RelativeOrAbsolute);
                         prop.SetValue(x, (object)uri, (object[])null);
                     }
                     else if (type == typeof(string))
                     {
                         prop.SetValue(x, valueFromXml, (object[])null);
                     }
                     else if (type == typeof(DateTime))
                     {
                         object obj =
                             (object)
                             (StringExtensions.HasValue(this.DateFormat)
                                     ? DateTime.ParseExact(valueFromXml.ToString(), this.DateFormat,
                                                           (IFormatProvider)this.Culture)
                                     : DateTime.Parse(valueFromXml.ToString(), (IFormatProvider)this.Culture));
                         prop.SetValue(x, obj, (object[])null);
                     }
                     else if (type == typeof(DateTimeOffset))
                     {
                         string str = valueFromXml.ToString();
                         if (!string.IsNullOrEmpty(str))
                         {
                             try
                             {
                                 DateTimeOffset dateTimeOffset = XmlConvert.ToDateTimeOffset(str);
                                 prop.SetValue(x, (object)dateTimeOffset, (object[])null);
                             }
                             catch (Exception ex)
                             {
                                 object result;
                                 if (XmlDebuggingDeserializer.TryGetFromString(str, out result, type))
                                 {
                                     prop.SetValue(x, result, (object[])null);
                                 }
                                 else
                                 {
                                     DateTimeOffset dateTimeOffset = DateTimeOffset.Parse(str);
                                     prop.SetValue(x, (object)dateTimeOffset, (object[])null);
                                 }
                             }
                         }
                     }
                     else if (type == typeof(Decimal))
                     {
                         object obj =
                             (object)Decimal.Parse(valueFromXml.ToString(), (IFormatProvider)this.Culture);
                         prop.SetValue(x, obj, (object[])null);
                     }
                     else if (type == typeof(Guid))
                     {
                         object obj =
                             (object)
                             (string.IsNullOrEmpty(valueFromXml.ToString())
                                     ? Guid.Empty
                                     : new Guid(valueFromXml.ToString()));
                         prop.SetValue(x, obj, (object[])null);
                     }
                     else if (type == typeof(TimeSpan))
                     {
                         TimeSpan timeSpan = XmlConvert.ToTimeSpan(valueFromXml.ToString());
                         prop.SetValue(x, (object)timeSpan, (object[])null);
                     }
                     else if (type.IsGenericType)
                     {
                         Type     t             = type.GetGenericArguments()[0];
                         IList    list          = (IList)Activator.CreateInstance(type);
                         XElement elementByName = this.GetElementByName(root,
                                                                        XmlExtensions.AsNamespaced(prop.Name, this.Namespace));
                         if (elementByName.HasElements)
                         {
                             XElement xelement = Enumerable.FirstOrDefault <XElement>(elementByName.Elements());
                             if (xelement != null)
                             {
                                 IEnumerable <XElement> elements = elementByName.Elements(xelement.Name);
                                 this.PopulateListFromElements(t, elements, list);
                             }
                         }
                         prop.SetValue(x, (object)list, (object[])null);
                     }
                     else if (ReflectionExtensions.IsSubclassOfRawGeneric(type, typeof(List <>)))
                     {
                         object obj = this.HandleListDerivative(root, prop.Name, type);
                         prop.SetValue(x, obj, (object[])null);
                     }
                     else
                     {
                         object result;
                         if (XmlDebuggingDeserializer.TryGetFromString(valueFromXml.ToString(), out result, type))
                         {
                             prop.SetValue(x, result, (object[])null);
                         }
                         else if (root != null)
                         {
                             XElement elementByName = this.GetElementByName(root, name);
                             if (elementByName != null)
                             {
                                 object andMap = this.CreateAndMap(type, elementByName);
                                 prop.SetValue(x, andMap, (object[])null);
                             }
                         }
                     }
                 }
             }
         }
         catch (Exception e)
         {
             throw;
         }
     }
     return(x);
 }