Пример #1
0
        ///-------------------------------------------------------------------------------------------------
        /// <summary>
        ///     Deserializes.
        /// </summary>
        ///
        /// <exception cref="NotImplementedException">
        ///     Thrown when the requested operation is unimplemented.
        /// </exception>
        ///
        /// <param name="type">
        ///     The type.
        /// </param>
        /// <param name="value">
        ///     The value.
        /// </param>
        ///
        /// <returns>
        ///     .
        /// </returns>
        ///-------------------------------------------------------------------------------------------------
        public object Deserialize(Type type, string value)
        {
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }

            IReflectionType reflectionType = Reflector.Get(type);

            object instance = reflectionType.CreateInstance();

            if (!string.IsNullOrWhiteSpace(value))
            {
                XDocument document = XDocument.Parse(value, LoadOptions.SetLineInfo);

                var root = document.Root;

                if (root != null)
                {
                    this.SetRootValue(reflectionType, instance, root);
                }
            }

            return(instance);
        }
Пример #2
0
        protected virtual async Task <RestResponse <T> > ExecuteAsync <T>(RestRequest request, MethodType method) where T : new()
        {
            request.Method = method;
            RestResponse <T> response = new RestResponse <T>(await GetResponseAsync(request));

            try
            {
                if (!string.IsNullOrWhiteSpace(response.Content))
                {
                    switch (response.ResponseMode)
                    {
                    case ResponseMode.Json:
                        IJsonSerializer jsonSerializer = Container.Get <IJsonSerializer>();

                        response.ContentObject = jsonSerializer.Deserialize <T>(response.Content);
                        break;

                    case ResponseMode.Xml:
                        IXmlSerializer xmlSerializer = Container.Get <IXmlSerializer>();
                        response.ContentObject = xmlSerializer.Deserialize <T>(response.Content);
                        break;
                    }

                    if (typeof(ISerializable).IsAssignableFrom(typeof(T)))
                    {
                        if (response.ContentObject == null)
                        {
                            IReflectionType reflectionType = Reflector.Get <T>();
                            response.ContentObject = (T)reflectionType.CreateInstance();
                            ((ISerializable)response.ContentObject).Deserialize(response.Content);
                        }
                    }
                }
            }
            catch (Exception exception)
            {
                response.Status         = ResponseStatus.Error;
                response.ErrorMessage   = exception.Message;
                response.ErrorException = new RestException(exception.Message, response.StatusCode, response.ErrorException);
            }

            return(response);
        }