예제 #1
0
        /// <summary>
        /// De-serializes the passed string to an object
        /// </summary>
        /// <returns>Concrete class</returns>
        public override T Deserialize()
        {
            T      returnValue = TypeExtension.InvokeConstructorOrDefault <T>();
            object result      = null;

            try
            {
                if (entityString == string.Empty && this.EmptyStringAndNullSupported == false)
                {
                    throw new System.ArgumentNullException("Passed parameter is empty. Unable to deserialize empty strings.");
                }
                foreach (var format in DateTimeExtension.FormatList)
                {
                    result = DeserializeWorker(format);
                    if (result != null)
                    {
                        returnValue = (T)result;
                        break;
                    }
                }
            }
            catch
            {
                if (ThrowException)
                {
                    throw;
                }
            }

            return(returnValue);
        }
예제 #2
0
        /// <summary>
        /// De-serializes the passed string to an object
        /// </summary>
        /// <param name="stringToDeserialize">Object to deserialize</param>
        /// <returns>Concrete class</returns>
        public override T Deserialize(string stringToDeserialize)
        {
            T returnValue = TypeExtension.InvokeConstructorOrDefault <T>();

            try
            {
                if (stringToDeserialize == TypeExtension.DefaultString && this.EmptyStringAndNullSupported == false)
                {
                    throw new System.ArgumentNullException("Passed parameter is empty. Unable to deserialize empty strings.");
                }
                System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
                var byteArray    = (byte[])encoding.GetBytes(stringToDeserialize);
                var memoryStream = new MemoryStream(byteArray);
                System.Xml.Serialization.XmlSerializer xs = new System.Xml.Serialization.XmlSerializer(typeof(T));
                returnValue = (T)xs.Deserialize(memoryStream);
            }
            catch (System.InvalidOperationException)
            {
                returnValue = (T)Activator.CreateInstance(typeof(T));
            }
            catch
            {
                if (ThrowException)
                {
                    throw;
                }
            }

            return(returnValue);
        }
예제 #3
0
        /// <summary>
        /// De-serializes the passed string to an object
        /// </summary>
        /// <param name="stringToDeserialize">Object to deserialize</param>
        /// <returns>Concrete class</returns>
        public override T Deserialize(string stringToDeserialize)
        {
            T returnValue = TypeExtension.InvokeConstructorOrDefault <T>();

            Byte[] bytes = null;
            DataContractJsonSerializer serializer;

            try
            {
                if (stringToDeserialize == TypeExtension.DefaultString && this.EmptyStringAndNullSupported == false)
                {
                    throw new System.ArgumentNullException("Passed parameter is empty. Unable to deserialize empty strings.");
                }
                serializer = new DataContractJsonSerializer(typeof(T), new DataContractJsonSerializerSettings()
                {
                    EmitTypeInformation = this.EmitTypeInformation, DateTimeFormat = this.DateTimeFormatString, KnownTypes = this.KnownTypes
                });
                bytes = Encoding.Unicode.GetBytes(stringToDeserialize);
                using (MemoryStream stream = new MemoryStream(bytes))
                {
                    returnValue = (T)serializer.ReadObject(stream);
                }
            }
            catch
            {
                if (ThrowException)
                {
                    throw;
                }
            }

            return(returnValue);
        }