Beispiel #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);
        }
Beispiel #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));
        }
Beispiel #3
0
        private static object DeserializeKeyOrValue(XSerializerXmlReader reader, IXmlSerializerInternal serializer, ISerializeOptions options, out bool shouldIssueRead)
        {
            var deserialized = serializer.DeserializeObject(reader, options);

            shouldIssueRead = true;

            return(deserialized);
        }
Beispiel #4
0
        private void SetElementPropertyValue(XSerializerXmlReader reader, bool hasInstanceBeenCreated, ISerializeOptions options, IDictionary <string, object> expando)
        {
            var propertyName = reader.Name;
            var serializer   = DynamicSerializer.GetSerializer <object>(null, _options.WithRootElementName(reader.Name));
            var value        = serializer.DeserializeObject(reader, options);

            expando[propertyName] = value;
        }
Beispiel #5
0
        public static object DeserializeObject(this IXmlSerializerInternal serializer, TextReader textReader, ISerializeOptions options)
        {
            options = options.WithNewSerializationState();

            var xmlReader = new XmlTextReader(textReader);
            var reader    = new XSerializerXmlReader(xmlReader, options.GetEncryptionMechanism(), options.EncryptKey, options.SerializationState);

            return(serializer.DeserializeObject(reader, options));
        }
Beispiel #6
0
        internal static bool ReadIfNeeded(this XSerializerXmlReader reader, bool shouldRead)
        {
            if (shouldRead)
            {
                return(reader.Read());
            }

            return(true);
        }
Beispiel #7
0
        /// <summary>
        /// Maybe sets the <see cref="XSerializerXmlTextWriter.IsEncryptionEnabled"/> property of
        /// <paramref name="reader"/> to true. Returns true if the value was changed to true, false
        /// if it was not changed to true.
        /// </summary>
        internal static bool MaybeSetIsDecryptionEnabledToTrue(this XSerializerXmlReader reader,
                                                               EncryptAttribute encryptAttribute, ISerializeOptions options)
        {
            if (options.ShouldEncrypt && encryptAttribute != null && !reader.IsDecryptionEnabled)
            {
                reader.IsDecryptionEnabled = true;
                return(true);
            }

            return(false);
        }
 public DefaultConstructorHelper(
     Func <T> createInstance,
     Dictionary <string, SerializableProperty> caseSensitiveSerializableProperties,
     SerializableProperty textNodeProperty,
     IDictionary <string, SerializableProperty> attributeProperties,
     XSerializerXmlReader reader)
 {
     _reader = reader;
     _caseSensitiveSerializableProperties = caseSensitiveSerializableProperties;
     _textNodeProperty    = textNodeProperty;
     _attributeProperties = attributeProperties;
     _instance            = createInstance();
 }
        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;
        }
Beispiel #10
0
        private static object DeserializeItem(XSerializerXmlReader reader, IXmlSerializerInternal serializer, bool hasInstanceBeenCreated, ISerializeOptions options, out bool shouldIssueRead)
        {
            if (!hasInstanceBeenCreated)
            {
                throw new InvalidOperationException("Deserialization error: attempted to deserialize an item before creating its list.");
            }

            var deserialized = serializer.DeserializeObject(reader, options);

            shouldIssueRead = true;

            return(deserialized);
        }
        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);
        }
 public NonDefaultConstructorHelper(
     IEnumerable <ConstructorWrapper> constructors,
     IEnumerable <SerializableProperty> serializableProperties,
     SerializableProperty textNodeProperty,
     IDictionary <string, SerializableProperty> attributeProperties,
     IDictionary <string, SerializableProperty> caseSensitiveSerializableProperties,
     XSerializerXmlReader reader)
 {
     _constructors           = constructors;
     _serializableProperties = serializableProperties;
     _textNodeProperty       = textNodeProperty;
     _attributeProperties    = attributeProperties;
     _caseSensitiveSerializableProperties = caseSensitiveSerializableProperties;
     _reader = reader;
 }
Beispiel #13
0
        public static object DeserializeObject(this IXmlSerializerInternal serializer, string xml, ISerializeOptions options)
        {
            options = options.WithNewSerializationState();

            using (var stringReader = new StringReader(xml))
            {
                using (var xmlReader = new XmlTextReader(stringReader))
                {
                    using (var reader = new XSerializerXmlReader(xmlReader, options.GetEncryptionMechanism(), options.EncryptKey, options.SerializationState))
                    {
                        return(serializer.DeserializeObject(reader, options));
                    }
                }
            }
        }
        public override bool Read()
        {
            if (_surrogateReader != null)
            {
                if (_surrogateReader.Read() && !IsAtDummyNodeEndElement())
                {
                    // If this isn't the closing dummy node, return true.
                    return(true);
                }

                // When we're done with surrogateReader, get rid of it and go back to primaryReader.
                _surrogateReader.Close();
                _surrogateReader = null;
                _currentReader   = _primaryReader;
            }

            var read = _primaryReader.Read();

            if (!read)
            {
                return(false);
            }

            // If we're decrypting, and our node contains text that starts with '<', then
            // assume that the text contains one or more encrypted child elements. In this
            // case, load the decrypted contents into a surrogate xml reader and use that
            // reader until it reads to its end. Then switch back to the primary reader.
            if (IsDecryptionEnabled && NodeType == XmlNodeType.Text)
            {
                var value = Value;

                if (_isStartElementRegex.IsMatch(value))
                {
                    // The xml fragment contained in the Value property may contain multiple
                    // top-level elements. To ensure valid xml, wrap the fragment in a dummy node.
                    var xml = _dummyNodeStartElement + value + _dummyNodeEndElement;

                    _surrogateReader = new XSerializerXmlReader(xml, _encryptionMechanism, _encryptKey,
                                                                _serializationState);
                    _currentReader = _surrogateReader;

                    _surrogateReader.Read();         // Advance to the opening dummy node
                    return(_surrogateReader.Read()); // Advance to the first decrypted node.
                }
            }

            return(true);
        }
        public override bool Read()
        {
            if (_surrogateReader != null)
            {
                if (_surrogateReader.Read() && !IsAtDummyNodeEndElement())
                {
                    // If this isn't the closing dummy node, return true.
                    return true;
                }

                // When we're done with surrogateReader, get rid of it and go back to primaryReader.
                _surrogateReader.Close();
                _surrogateReader = null;
                _currentReader = _primaryReader;
            }

            var read = _primaryReader.Read();

            if (!read)
            {
                return false;
            }

            // If we're decrypting, and our node contains text that starts with '<', then
            // assume that the text contains one or more encrypted child elements. In this
            // case, load the decrypted contents into a surrogate xml reader and use that
            // reader until it reads to its end. Then switch back to the primary reader.
            if (IsDecryptionEnabled && NodeType == XmlNodeType.Text)
            {
                var value = Value;

                if (_isStartElementRegex.IsMatch(value))
                {
                    // The xml fragment contained in the Value property may contain multiple
                    // top-level elements. To ensure valid xml, wrap the fragment in a dummy node.
                    var xml = _dummyNodeStartElement + value + _dummyNodeEndElement;

                    _surrogateReader = new XSerializerXmlReader(xml, _encryptionMechanism, _encryptKey,
                        _serializationState);
                    _currentReader = _surrogateReader;

                    _surrogateReader.Read(); // Advance to the opening dummy node
                    return _surrogateReader.Read(); // Advance to the first decrypted node.
                }
            }

            return true;
        }
            public IHelper CreateHelper(Type type, XSerializerXmlReader reader)
            {
                var temp = type;
                Lazy <Func <XSerializerXmlReader, IHelper> > createHelper;

                while (!_createHelperFuncs.TryGetValue(temp, out createHelper))
                {
                    temp = temp.BaseType;

                    if (temp == null)
                    {
                        throw new InvalidOperationException("Unable to find serializable properties for type " + type);
                    }
                }

                return(createHelper.Value(reader));
            }
Beispiel #17
0
        internal static bool IsNil(this XSerializerXmlReader reader)
        {
            var nilFound = false;

            while (reader.MoveToNextAttribute())
            {
                if (reader.LocalName == "nil" && reader.NamespaceURI == "http://www.w3.org/2001/XMLSchema-instance")
                {
                    nilFound = true;
                    break;
                }
            }

            reader.MoveToElement();

            return(nilFound);
        }
        private static object DeserializeItem(XSerializerXmlReader reader, IXmlSerializerInternal serializer, bool hasInstanceBeenCreated, ISerializeOptions options, out bool shouldIssueRead)
        {
            if (!hasInstanceBeenCreated)
            {
                throw new InvalidOperationException("Deserialization error: attempted to deserialize an item before creating its list.");
            }

            var deserialized = serializer.DeserializeObject(reader, options);

            shouldIssueRead = true;

            return deserialized;
        }
Beispiel #19
0
 public object DeserializeObject(XSerializerXmlReader reader, ISerializeOptions options)
 {
     return((ExpandoObject)_serializer.DeserializeToDynamic(reader, options));
 }
        private static object DeserializeKeyOrValue(XSerializerXmlReader reader, IXmlSerializerInternal serializer, ISerializeOptions options, out bool shouldIssueRead)
        {
            var deserialized = serializer.DeserializeObject(reader, options);

            shouldIssueRead = true;

            return deserialized;
        }
Beispiel #21
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.");
        }
 public void ReadValue(XSerializerXmlReader reader, object instance, ISerializeOptions options)
 {
     SetValue(instance, ReadValue(reader, options));
 }
 public object ReadValue(XSerializerXmlReader reader, ISerializeOptions options)
 {
     return(_serializer.Value.DeserializeObject(reader, options));
 }
            public IHelper CreateHelper(Type type, XSerializerXmlReader reader)
            {
                var temp = type;
                Lazy<Func<XSerializerXmlReader, IHelper>> createHelper;

                while (!_createHelperFuncs.TryGetValue(temp, out createHelper))
                {
                    temp = temp.BaseType;

                    if (temp == null)
                    {
                        throw new InvalidOperationException("Unable to find serializable properties for type " + type);
                    }
                }

                return createHelper.Value(reader);
            }
Beispiel #25
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.");
        }
Beispiel #26
0
        public static Type GetXsdType <T>(this XSerializerXmlReader reader, Type[] extraTypes)
        {
            string typeName = null;

            while (reader.MoveToNextAttribute())
            {
                if (reader.LocalName == "type" && reader.LookupNamespace(reader.Prefix) == "http://www.w3.org/2001/XMLSchema-instance")
                {
                    typeName = reader.Value;
                    break;
                }
            }

            reader.MoveToElement();

            if (typeName == null)
            {
                return(null);
            }

            Type typeFromXsdType;

            if (_xsdTypeToTypeMap.TryGetValue(typeName, out typeFromXsdType))
            {
                return(typeFromXsdType);
            }

            return(_xsdTypeToTypeCache.GetOrAdd(
                       Tuple.Create(typeof(T), typeName),
                       _ =>
            {
                Type type = null;

                //// try REAL hard to get the type. (holy crap, this is UUUUUGLY!!!!)

                if (extraTypes != null)
                {
                    var matchingExtraTypes = extraTypes.Where(t => t.Name == typeName && typeof(T).IsAssignableFrom(t)).ToList();
                    if (matchingExtraTypes.Count == 1)
                    {
                        type = matchingExtraTypes[0];
                    }
                }

                if (type == null)
                {
                    var typeNameWithPossibleNamespace = typeName;

                    if (!typeName.Contains('.'))
                    {
                        typeNameWithPossibleNamespace = typeof(T).Namespace + "." + typeName;
                    }

                    var checkPossibleNamespace = typeName != typeNameWithPossibleNamespace;

                    type = Type.GetType(typeName);
                    type = typeof(T).IsAssignableFrom(type) ? type : null;

                    if (type == null)
                    {
                        type = checkPossibleNamespace ? Type.GetType(typeNameWithPossibleNamespace) : null;
                        type = typeof(T).IsAssignableFrom(type) ? type : null;

                        if (type == null)
                        {
                            type = typeof(T).Assembly.GetType(typeName);
                            type = typeof(T).IsAssignableFrom(type) ? type : null;

                            if (type == null)
                            {
                                type = checkPossibleNamespace ? typeof(T).Assembly.GetType(typeNameWithPossibleNamespace) : null;
                                type = typeof(T).IsAssignableFrom(type) ? type : null;

                                if (type == null)
                                {
                                    var matches = typeof(T).Assembly.GetTypes().Where(t => t.Name == typeName && typeof(T).IsAssignableFrom(t)).ToList();
                                    if (matches.Count == 1)
                                    {
                                        type = matches.Single();
                                    }

                                    var entryAssembly = Assembly.GetEntryAssembly();
                                    if (entryAssembly != null)
                                    {
                                        type = entryAssembly.GetType(typeName);
                                        type = typeof(T).IsAssignableFrom(type) ? type : null;

                                        if (type == null)
                                        {
                                            type = checkPossibleNamespace ? entryAssembly.GetType(typeNameWithPossibleNamespace) : null;
                                            type = typeof(T).IsAssignableFrom(type) ? type : null;
                                        }

                                        if (type == null)
                                        {
                                            matches = entryAssembly.GetTypes().Where(t => t.Name == typeName && typeof(T).IsAssignableFrom(t)).ToList();
                                            if (matches.Count == 1)
                                            {
                                                type = matches.Single();
                                            }
                                        }
                                    }

                                    if (type == null)
                                    {
                                        matches = AppDomain.CurrentDomain.GetAssemblies().SelectMany(a =>
                                        {
                                            try
                                            {
                                                return a.GetTypes();
                                            }
                                            catch
                                            {
                                                return Enumerable.Empty <Type>();
                                            }
                                        }).Where(t => t.Name == typeName && typeof(T).IsAssignableFrom(t)).ToList();

                                        if (matches.Count == 1)
                                        {
                                            type = matches.Single();
                                        }
                                        else if (matches.Count > 1)
                                        {
                                            throw new SerializationException(string.Format("More than one type matches '{0}'. Consider decorating your type with the XmlIncludeAttribute, or pass in the type into the serializer as an extra type.", typeName));
                                        }
                                    }
                                }
                            }
                        }
                    }
                }

                if (type == null)
                {
                    throw new SerializationException(string.Format("No suitable type matches '{0}'. Consider decorating your type with the XmlIncludeAttribute, or pass in the type into the serializer as an extra type.", typeName));
                }

                return type;
            }));
        }
        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.");
        }
 public DefaultConstructorHelper(
     Func<T> createInstance,
     Dictionary<string, SerializableProperty> caseSensitiveSerializableProperties,
     SerializableProperty textNodeProperty,
     IDictionary<string, SerializableProperty> attributeProperties,
     XSerializerXmlReader reader)
 {
     _reader = reader;
     _caseSensitiveSerializableProperties = caseSensitiveSerializableProperties;
     _textNodeProperty = textNodeProperty;
     _attributeProperties = attributeProperties;
     _instance = createInstance();
 }
 public NonDefaultConstructorHelper(
     IEnumerable<ConstructorWrapper> constructors,
     IEnumerable<SerializableProperty> serializableProperties,
     SerializableProperty textNodeProperty,
     IDictionary<string, SerializableProperty> attributeProperties,
     IDictionary<string, SerializableProperty> caseSensitiveSerializableProperties,
     XSerializerXmlReader reader)
 {
     _constructors = constructors;
     _serializableProperties = serializableProperties;
     _textNodeProperty = textNodeProperty;
     _attributeProperties = attributeProperties;
     _caseSensitiveSerializableProperties = caseSensitiveSerializableProperties;
     _reader = reader;
 }
Beispiel #30
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.");
        }
        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.");
        }