コード例 #1
0
        public object DeserializeObject(XSerializerXmlReader reader, ISerializeOptions options)
        {
            var isNil = reader.IsNil();

            if (isNil && reader.IsEmptyElement)
            {
                return(null);
            }

            object deserializedObject;

            var type = reader.GetXsdType <object>(_options.ExtraTypes);

            if (type != null)
            {
                var serializer = XmlSerializerFactory.Instance.GetSerializer(type, _encryptAttribute, _options.WithRootElementName(reader.Name));
                deserializedObject = serializer.DeserializeObject(reader, options);
            }
            else
            {
                deserializedObject = DeserializeToDynamic(reader, options);
            }

            return
                (isNil
                    ? null
                    : deserializedObject);
        }
コード例 #2
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));
        }
コード例 #3
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.");
        }
コード例 #4
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.");
        }
コード例 #5
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.");
        }
コード例 #6
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.");
        }