Пример #1
0
        private static object ParseStringForNullableTimeSpan(string value, ISerializeOptions options)
        {
            if (string.IsNullOrEmpty(value))
            {
                return(null);
            }

            return(TimeSpan.Parse(value, options.GetCulture()));
        }
Пример #2
0
        private static object ParseStringForNullableDateTimeOffset(string value, ISerializeOptions options)
        {
            if (string.IsNullOrEmpty(value))
            {
                return(null);
            }

            return(DateTimeOffset.Parse(
                       value,
                       options.GetCulture(),
                       DateTimeStyles.RoundtripKind));
        }
Пример #3
0
        private static string GetStringFromTimeSpan(object value, ISerializeOptions options)
        {
            var timeSpan = (TimeSpan)value;

            return(timeSpan.ToString("G", options.GetCulture()));
        }
Пример #4
0
        private static string GetStringFromDateTimeOffset(object value, ISerializeOptions options)
        {
            var dateTimeOffset = (DateTimeOffset)value;

            return(dateTimeOffset.ToString("O", options.GetCulture()));
        }
Пример #5
0
        private dynamic DeserializeToDynamic(XSerializerXmlReader reader, ISerializeOptions options)
        {
            object instance = null;
            var    hasInstanceBeenCreated = false;

            var setIsDecryptionEnabledBackToFalse = false;

            Func <bool> isAtRootElement;

            {
                var hasOpenedRootElement = false;

                isAtRootElement = () =>
                {
                    if (!hasOpenedRootElement && reader.Name == _options.RootElementName)
                    {
                        hasOpenedRootElement = true;
                        return(true);
                    }

                    return(false);
                };
            }

            do
            {
                switch (reader.NodeType)
                {
                case XmlNodeType.Element:
                    if (isAtRootElement())
                    {
                        setIsDecryptionEnabledBackToFalse = reader.MaybeSetIsDecryptionEnabledToTrue(_encryptAttribute, options);

                        instance = new ExpandoObject();
                        hasInstanceBeenCreated = true;

                        if (reader.IsEmptyElement)
                        {
                            if (_options.TreatEmptyElementAsString)
                            {
                                instance = "";
                            }

                            if (setIsDecryptionEnabledBackToFalse)
                            {
                                reader.IsDecryptionEnabled = false;
                            }

                            return(instance);
                        }
                    }
                    else
                    {
                        SetElementPropertyValue(reader, hasInstanceBeenCreated, options, (ExpandoObject)instance);
                    }
                    break;

                case XmlNodeType.Text:
                    var stringValue = (string)new XmlTextSerializer(typeof(string), _options.RedactAttribute, null, _options.ExtraTypes).DeserializeObject(reader, options);
                    hasInstanceBeenCreated = true;

                    bool boolValue;
                    if (bool.TryParse(stringValue, out boolValue))
                    {
                        instance = boolValue;
                        break;
                    }

                    int intValue;
                    if (int.TryParse(stringValue, NumberStyles.Integer, options.GetCulture(), out intValue))
                    {
                        // If this is a number with leading zeros, treat it as a string so we don't lose those leading zeros.
                        if (stringValue[0] == '0' && stringValue.Length > 1)
                        {
                            instance = stringValue;
                        }
                        else
                        {
                            instance = intValue;
                        }

                        break;
                    }

                    decimal decimalValue;
                    if (decimal.TryParse(stringValue, NumberStyles.Float, options.GetCulture(), out decimalValue))
                    {
                        instance = decimalValue;
                        break;
                    }

                    DateTime dateTimeValue;
                    if (DateTime.TryParse(stringValue, options.GetCulture(), DateTimeStyles.None, out dateTimeValue) ||
                        DateTime.TryParse(stringValue, out dateTimeValue))
                    {
                        instance = dateTimeValue.ToUniversalTime();
                        break;
                    }

                    // TODO: add more types to check?

                    instance = stringValue;
                    break;

                case XmlNodeType.EndElement:
                    if (reader.Name == _options.RootElementName)
                    {
                        if (_options.TreatEmptyElementAsString)
                        {
                            var instanceAsExpando = instance as IDictionary <string, object>;
                            if (instanceAsExpando != null && instanceAsExpando.Count == 0)
                            {
                                instance = "";
                            }
                        }

                        if (setIsDecryptionEnabledBackToFalse)
                        {
                            reader.IsDecryptionEnabled = false;
                        }

                        return(CheckAndReturn(hasInstanceBeenCreated, instance));
                    }
                    break;
                }
            } while (reader.Read());

            throw new InvalidOperationException("Deserialization error: reached the end of the document without returning a value.");
        }