Ejemplo n.º 1
0
        public object DeserializeObject(XSerializerXmlReader reader, ISerializeOptions options)
        {
            if (ValueTypes.IsRegistered(typeof(T)))
            {
                while (reader.NodeType != XmlNodeType.Element)
                {
                    reader.Read();
                }
            }

            if (reader.IsNil())
            {
                return(default(T));
            }

            var setIsDecryptionEnabledBackToFalse = reader.MaybeSetIsDecryptionEnabledToTrue(_encryptAttribute, options);

            var value = reader.ReadString();

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

            return(_valueConverter.ParseString(value, options));
        }
Ejemplo n.º 2
0
        public object DeserializeObject(XSerializerXmlReader reader, ISerializeOptions options)
        {
            var setIsDecryptionEnabledBackToFalse = reader.MaybeSetIsDecryptionEnabledToTrue(_encryptAttribute, options);

            var value = _valueConverter.ParseString(reader.Value, options);

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

            return(value);
        }
Ejemplo n.º 3
0
        public object DeserializeObject(XSerializerXmlReader reader, ISerializeOptions options)
        {
            var setIsDecryptionEnabledBackToFalse = reader.MaybeSetIsDecryptionEnabledToTrue(_encryptAttribute, options);

            var value = _valueConverter.ParseString(reader.Value, options);

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

            return value;
        }
Ejemplo n.º 4
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, 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, out decimalValue))
                    {
                        instance = decimalValue;
                        break;
                    }

                    DateTime dateTimeValue;
                    if (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.");
        }
Ejemplo n.º 5
0
        public object DeserializeObject(XSerializerXmlReader reader, ISerializeOptions options)
        {
            object dictionary = null;

            var hasInstanceBeenCreated = false;
            var isInsideItemElement    = false;

            object currentKey   = null;
            object currentValue = null;
            bool   shouldIssueRead;

            var setIsDecryptionEnabledBackToFalse = false;

            Func <bool> isAtRootElement;

            {
                var hasOpenedRootElement = false;

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

                    return(false);
                };
            }

            do
            {
                shouldIssueRead = true;

                switch (reader.NodeType)
                {
                case XmlNodeType.Element:
                    if (isAtRootElement())
                    {
                        if (reader.IsNil())
                        {
                            if (reader.IsEmptyElement)
                            {
                                return(null);
                            }

                            dictionary             = null;
                            hasInstanceBeenCreated = true;
                        }
                        else
                        {
                            setIsDecryptionEnabledBackToFalse = reader.MaybeSetIsDecryptionEnabledToTrue(_encryptAttribute, options);

                            dictionary             = _createDictionary();
                            hasInstanceBeenCreated = true;

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

                                return(_finalizeDictionary(dictionary));
                            }
                        }
                    }
                    else if (reader.Name == "Item" && hasInstanceBeenCreated)
                    {
                        isInsideItemElement = true;
                    }
                    else if (isInsideItemElement)
                    {
                        if (reader.Name == "Key")
                        {
                            currentKey = DeserializeKeyOrValue(reader, _keySerializer, options, out shouldIssueRead);
                        }
                        else if (reader.Name == "Value")
                        {
                            currentValue = DeserializeKeyOrValue(reader, _valueSerializer, options, out shouldIssueRead);
                        }
                    }

                    break;

                case XmlNodeType.EndElement:
                    if (isInsideItemElement && reader.Name == "Item")
                    {
                        AddItemToDictionary(dictionary, currentKey, currentValue);
                        currentKey          = null;
                        currentValue        = null;
                        isInsideItemElement = false;
                    }
                    else if (reader.Name == _options.RootElementName)
                    {
                        if (setIsDecryptionEnabledBackToFalse)
                        {
                            reader.IsDecryptionEnabled = false;
                        }

                        return(CheckAndReturn(hasInstanceBeenCreated, _finalizeDictionary(dictionary)));
                    }

                    break;
                }
            } while (reader.ReadIfNeeded(shouldIssueRead));

            throw new InvalidOperationException("Deserialization error: reached the end of the document without returning a value.");
        }
Ejemplo n.º 6
0
        public object DeserializeObject(XSerializerXmlReader reader, ISerializeOptions options)
        {
            object collection = null;

            var hasInstanceBeenCreated = false;

            bool shouldIssueRead;

            var setIsDecryptionEnabledBackToFalse = false;

            if (_options.RootElementName == null)
            {
                setIsDecryptionEnabledBackToFalse = reader.MaybeSetIsDecryptionEnabledToTrue(_encryptAttribute, options);

                collection = _createCollection();
                hasInstanceBeenCreated = true;
            }

            do
            {
                shouldIssueRead = true;

                switch (reader.NodeType)
                {
                    case XmlNodeType.Element:
                        if (_options.RootElementName != null)
                        {
                            if (reader.Name == _options.RootElementName)
                            {
                                if (reader.IsNil())
                                {
                                    if (reader.IsEmptyElement)
                                    {
                                        return null;
                                    }

                                    collection = null;
                                    hasInstanceBeenCreated = true;
                                }
                                else
                                {
                                    setIsDecryptionEnabledBackToFalse = reader.MaybeSetIsDecryptionEnabledToTrue(_encryptAttribute, options);

                                    collection = _createCollection();
                                    hasInstanceBeenCreated = true;

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

                                        return collection;
                                    }
                                }

                                break;
                            }
                        }
                        else
                        {
                            // If there's no root element, and we encounter another element, we're done - get out!
                            if (reader.Name != _itemElementName)
                            {
                                if (setIsDecryptionEnabledBackToFalse)
                                {
                                    reader.IsDecryptionEnabled = false;
                                }

                                return
                                    collection == null
                                        ? null
                                        : CheckAndReturn(hasInstanceBeenCreated, collection);
                            }
                        }

                        if (reader.Name == _itemElementName)
                        {
                            var item = DeserializeItem(reader, _itemSerializer, hasInstanceBeenCreated, options, out shouldIssueRead);

                            if (collection != null)
                            {
                                AddItemToCollection(collection, item);
                            }
                        }
                        break;
                    case XmlNodeType.EndElement:
                        if (_options.RootElementName != null)
                        {
                            if (reader.Name == _options.RootElementName)
                            {
                                if (setIsDecryptionEnabledBackToFalse)
                                {
                                    reader.IsDecryptionEnabled = false;
                                }

                                return
                                    collection == null
                                        ? null
                                        : CheckAndReturn(hasInstanceBeenCreated, collection);
                            }
                        }
                        else
                        {
                            if (reader.Name != _itemElementName)
                            {
                                if (setIsDecryptionEnabledBackToFalse)
                                {
                                    reader.IsDecryptionEnabled = false;
                                }

                                return
                                    collection == null
                                        ? null
                                        : CheckAndReturn(hasInstanceBeenCreated, collection);
                            }
                        }
                        break;
                }
            } while (reader.ReadIfNeeded(shouldIssueRead));

            throw new InvalidOperationException("Deserialization error: attempted to return a deserialized instance before it was created.");
        }
Ejemplo n.º 7
0
        public object DeserializeObject(XSerializerXmlReader reader, ISerializeOptions options)
        {
            object collection = null;

            var hasInstanceBeenCreated = false;

            bool shouldIssueRead;

            var setIsDecryptionEnabledBackToFalse = false;

            if (_options.RootElementName == null)
            {
                setIsDecryptionEnabledBackToFalse = reader.MaybeSetIsDecryptionEnabledToTrue(_encryptAttribute, options);

                collection             = _createCollection();
                hasInstanceBeenCreated = true;
            }

            do
            {
                shouldIssueRead = true;

                switch (reader.NodeType)
                {
                case XmlNodeType.Element:
                    if (_options.RootElementName != null)
                    {
                        if (reader.Name == _options.RootElementName)
                        {
                            if (reader.IsNil())
                            {
                                if (reader.IsEmptyElement)
                                {
                                    return(null);
                                }

                                collection             = null;
                                hasInstanceBeenCreated = true;
                            }
                            else
                            {
                                setIsDecryptionEnabledBackToFalse = reader.MaybeSetIsDecryptionEnabledToTrue(_encryptAttribute, options);

                                collection             = _createCollection();
                                hasInstanceBeenCreated = true;

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

                                    return(collection);
                                }
                            }

                            break;
                        }
                    }
                    else
                    {
                        // If there's no root element, and we encounter another element, we're done - get out!
                        if (reader.Name != _itemElementName)
                        {
                            if (setIsDecryptionEnabledBackToFalse)
                            {
                                reader.IsDecryptionEnabled = false;
                            }

                            return
                                (collection == null
                                        ? null
                                        : CheckAndReturn(hasInstanceBeenCreated, collection));
                        }
                    }

                    if (reader.Name == _itemElementName)
                    {
                        var item = DeserializeItem(reader, _itemSerializer, hasInstanceBeenCreated, options, out shouldIssueRead);

                        if (collection != null)
                        {
                            AddItemToCollection(collection, item);
                        }
                    }
                    break;

                case XmlNodeType.EndElement:
                    if (_options.RootElementName != null)
                    {
                        if (reader.Name == _options.RootElementName)
                        {
                            if (setIsDecryptionEnabledBackToFalse)
                            {
                                reader.IsDecryptionEnabled = false;
                            }

                            return
                                (collection == null
                                        ? null
                                        : CheckAndReturn(hasInstanceBeenCreated, collection));
                        }
                    }
                    else
                    {
                        if (reader.Name != _itemElementName)
                        {
                            if (setIsDecryptionEnabledBackToFalse)
                            {
                                reader.IsDecryptionEnabled = false;
                            }

                            return
                                (collection == null
                                        ? null
                                        : CheckAndReturn(hasInstanceBeenCreated, collection));
                        }
                    }
                    break;
                }
            } while (reader.ReadIfNeeded(shouldIssueRead));

            throw new InvalidOperationException("Deserialization error: attempted to return a deserialized instance before it was created.");
        }
Ejemplo n.º 8
0
        public object DeserializeObject(XSerializerXmlReader reader, ISerializeOptions options)
        {
            object dictionary = null;

            var hasInstanceBeenCreated = false;
            var isInsideItemElement = false;

            object currentKey = null;
            object currentValue = null;
            bool shouldIssueRead;

            var setIsDecryptionEnabledBackToFalse = false;

            Func<bool> isAtRootElement;
            {
                var hasOpenedRootElement = false;
                
                isAtRootElement = () =>
                {
                    if (!hasOpenedRootElement && reader.Name == _options.RootElementName)
                    {
                        hasOpenedRootElement = true;
                        return true;
                    }

                    return false;
                };
            }

            do
            {
                shouldIssueRead = true;

                switch (reader.NodeType)
                {
                    case XmlNodeType.Element:
                        if (isAtRootElement())
                        {
                            if (reader.IsNil())
                            {
                                if (reader.IsEmptyElement)
                                {
                                    return null;
                                }

                                dictionary = null;
                                hasInstanceBeenCreated = true;
                            }
                            else
                            {
                                setIsDecryptionEnabledBackToFalse = reader.MaybeSetIsDecryptionEnabledToTrue(_encryptAttribute, options);

                                dictionary = _createDictionary();
                                hasInstanceBeenCreated = true;

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

                                    return _finalizeDictionary(dictionary);
                                }
                            }
                        }
                        else if (reader.Name == "Item" && hasInstanceBeenCreated)
                        {
                            isInsideItemElement = true;
                        }
                        else if (isInsideItemElement)
                        {
                            if (reader.Name == "Key")
                            {
                                currentKey = DeserializeKeyOrValue(reader, _keySerializer, options, out shouldIssueRead);
                            }
                            else if (reader.Name == "Value")
                            {
                                currentValue = DeserializeKeyOrValue(reader, _valueSerializer, options, out shouldIssueRead);
                            }
                        }

                        break;
                    case XmlNodeType.EndElement:
                        if (isInsideItemElement && reader.Name == "Item")
                        {
                            AddItemToDictionary(dictionary, currentKey, currentValue);
                            currentKey = null;
                            currentValue = null;
                            isInsideItemElement = false;
                        }
                        else if (reader.Name == _options.RootElementName)
                        {
                            if (setIsDecryptionEnabledBackToFalse)
                            {
                                reader.IsDecryptionEnabled = false;
                            }

                            return CheckAndReturn(hasInstanceBeenCreated, _finalizeDictionary(dictionary));
                        }

                        break;
                }
            } while (reader.ReadIfNeeded(shouldIssueRead));

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