Пример #1
0
        public static void SerializeObject(
            this IXmlSerializerInternal serializer,
            Stream stream,
            object instance,
            Encoding encoding,
            Formatting formatting,
            ISerializeOptions options)
        {
            options = options.WithNewSerializationState();

            StreamWriter streamWriter = null;

            try
            {
                streamWriter = new StreamWriter(stream, encoding ?? Encoding.UTF8);

                var xmlWriter = new XSerializerXmlTextWriter(streamWriter, options)
                {
                    Formatting = formatting
                };

                serializer.SerializeObject(xmlWriter, instance, options);
                xmlWriter.Flush();
            }
            finally
            {
                if (streamWriter != null)
                {
                    streamWriter.Flush();
                }
            }
        }
Пример #2
0
        private static object DeserializeKeyOrValue(XSerializerXmlReader reader, IXmlSerializerInternal serializer, ISerializeOptions options, out bool shouldIssueRead)
        {
            var deserialized = serializer.DeserializeObject(reader, options);

            shouldIssueRead = true;

            return(deserialized);
        }
Пример #3
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));
        }
Пример #4
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);
        }
Пример #5
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));
                    }
                }
            }
        }
Пример #6
0
        public static void SerializeObject(
            this IXmlSerializerInternal serializer,
            TextWriter writer,
            object instance,
            Formatting formatting,
            ISerializeOptions options)
        {
            options = options.WithNewSerializationState();

            var xmlWriter = new XSerializerXmlTextWriter(writer, options)
            {
                Formatting = formatting
            };

            serializer.SerializeObject(xmlWriter, instance, options);
            xmlWriter.Flush();
        }
Пример #7
0
        protected DictionarySerializer(EncryptAttribute encryptAttribute, IXmlSerializerOptions options)
        {
            // ReSharper disable DoNotCallOverridableMethodsInConstructor

            _encryptAttribute =
                encryptAttribute
                ?? KeyType.GetCustomAttribute <EncryptAttribute>()
                ?? ValueType.GetCustomAttribute <EncryptAttribute>();

            _options         = options;
            _keySerializer   = XmlSerializerFactory.Instance.GetSerializer(KeyType, null, _options.WithRootElementName("Key").WithRedactAttribute(null));
            _valueSerializer = XmlSerializerFactory.Instance.GetSerializer(ValueType, null, _options.WithRootElementName("Value"));

            if (DictionaryType.IsReadOnlyDictionary())
            {
                _createDictionary   = DefaultDictionaryType.CreateDefaultConstructorFunc <object>();
                _finalizeDictionary = FinalizeCollectionIntoReadOnlyDictionary;
            }
            else if (DictionaryType.IsInterface || DictionaryType.IsAbstract)
            {
                if (DictionaryType.IsAssignableFrom(DefaultDictionaryType))
                {
                    _createDictionary = DefaultDictionaryType.CreateDefaultConstructorFunc <object>();
                }
                else
                {
                    var dictionaryInheritorType =
                        _options.ExtraTypes.FirstOrDefault(t =>
                                                           !t.IsInterface &&
                                                           !t.IsAbstract &&
                                                           DictionaryType.IsAssignableFrom(t) &&
                                                           t.HasDefaultConstructor());
                    _createDictionary = dictionaryInheritorType.CreateDefaultConstructorFunc <object>();
                }
            }
            else if (DictionaryType.HasDefaultConstructor())
            {
                _createDictionary = DictionaryType.CreateDefaultConstructorFunc <object>();
            }
            else
            {
                throw new ArgumentException("Unable to find suitable dictionary to create.");
            }
            // ReSharper restore DoNotCallOverridableMethodsInConstructor
        }
Пример #8
0
        protected DictionarySerializer(EncryptAttribute encryptAttribute, IXmlSerializerOptions options)
        {
            // ReSharper disable DoNotCallOverridableMethodsInConstructor

            _encryptAttribute =
                encryptAttribute
                ?? (EncryptAttribute)Attribute.GetCustomAttribute(KeyType, typeof(EncryptAttribute))
                ?? (EncryptAttribute)Attribute.GetCustomAttribute(ValueType, typeof(EncryptAttribute));

            _options = options;
            _keySerializer = XmlSerializerFactory.Instance.GetSerializer(KeyType, null, _options.WithRootElementName("Key").WithRedactAttribute(null));
            _valueSerializer = XmlSerializerFactory.Instance.GetSerializer(ValueType, null, _options.WithRootElementName("Value"));

            if (DictionaryType.IsReadOnlyDictionary())
            {
                _createDictionary = DefaultDictionaryType.CreateDefaultConstructorFunc<object>();
                _finalizeDictionary = FinalizeCollectionIntoReadOnlyDictionary;
            }
            else if (DictionaryType.IsInterface || DictionaryType.IsAbstract)
            {
                if (DictionaryType.IsAssignableFrom(DefaultDictionaryType))
                {
                    _createDictionary = DefaultDictionaryType.CreateDefaultConstructorFunc<object>();
                }
                else
                {
                    var dictionaryInheritorType =
                        _options.ExtraTypes.FirstOrDefault(t =>
                            !t.IsInterface
                            && !t.IsAbstract
                            && DictionaryType.IsAssignableFrom(t)
                            && t.HasDefaultConstructor());
                    _createDictionary = dictionaryInheritorType.CreateDefaultConstructorFunc<object>();
                }
            }
            else if (DictionaryType.HasDefaultConstructor())
            {
                _createDictionary = DictionaryType.CreateDefaultConstructorFunc<object>();
            }
            else
            {
                throw new ArgumentException("Unable to find suitable dictionary to create.");
            }
            // ReSharper restore DoNotCallOverridableMethodsInConstructor
        }
Пример #9
0
        internal XmlSerializationProviderBase(
            IXmlSerializerInternal serializer, String elementName, Boolean asAttribute = false, XmlItemDefAttributeCollection itemDefAttributes = null
            )
        {
            if (serializer == null)
            {
                throw new ArgumentNullException();
            }
            if (String.IsNullOrEmpty(elementName))
            {
                throw new ArgumentNullException();
            }

            this.serializer        = serializer;
            this.elementName       = elementName;
            this.asAttribute       = asAttribute;
            this.itemDefAttributes = itemDefAttributes;
        }
Пример #10
0
        // ReSharper disable DoNotCallOverridableMethodsInConstructor
        protected ListSerializer(EncryptAttribute encryptAttribute, IXmlSerializerOptions options, string itemElementName)
        {
            _encryptAttribute = encryptAttribute ?? ItemType.GetCustomAttribute<EncryptAttribute>();
            _options = options;
            _itemElementName = string.IsNullOrEmpty(itemElementName) ? DefaultItemElementName : itemElementName;
            _itemSerializer = XmlSerializerFactory.Instance.GetSerializer(ItemType, null, _options.WithRootElementName(_itemElementName).AlwaysEmitNil());

            if (CollectionType.IsArray)
            {
                _createCollection = DefaultCollectionType.CreateDefaultConstructorFunc<object>();
            }
            else if (CollectionType.IsReadOnlyCollection())
            {
                _createCollection = DefaultCollectionType.CreateDefaultConstructorFunc<object>();
            }
            else if (CollectionType.IsInterface || CollectionType.IsAbstract)
            {
                if (CollectionType.IsAssignableFrom(DefaultCollectionType))
                {
                    _createCollection = DefaultCollectionType.CreateDefaultConstructorFunc<object>();
                }
                else
                {
                    var collectionInheritorType =
                        _options.ExtraTypes.FirstOrDefault(t =>
                            !t.IsInterface
                            && !t.IsAbstract
                            && CollectionType.IsAssignableFrom(t)
                            && t.HasDefaultConstructor());
                    _createCollection = collectionInheritorType.CreateDefaultConstructorFunc<object>();
                }
            }
            else if (CollectionType.HasDefaultConstructor())
            {
                _createCollection = CollectionType.CreateDefaultConstructorFunc<object>();
            }
            else
            {
                throw new ArgumentException("Unable to find suitable collection to create.");
            }
        }
Пример #11
0
        protected ListSerializer(EncryptAttribute encryptAttribute, IXmlSerializerOptions options, string itemElementName)                                                             // ReSharper disable DoNotCallOverridableMethodsInConstructor
        {
            _encryptAttribute = encryptAttribute ?? (EncryptAttribute)Attribute.GetCustomAttribute(ItemType, typeof(EncryptAttribute));
            _options          = options;
            _itemElementName  = string.IsNullOrEmpty(itemElementName) ? DefaultItemElementName : itemElementName;
            _itemSerializer   = XmlSerializerFactory.Instance.GetSerializer(ItemType, null, _options.WithRootElementName(_itemElementName).AlwaysEmitNil());

            if (CollectionType.IsArray)
            {
                _createCollection = DefaultCollectionType.CreateDefaultConstructorFunc <object>();
            }
            else if (CollectionType.IsReadOnlyCollection())
            {
                _createCollection = DefaultCollectionType.CreateDefaultConstructorFunc <object>();
            }
            else if (CollectionType.IsInterface || CollectionType.IsAbstract)
            {
                if (CollectionType.IsAssignableFrom(DefaultCollectionType))
                {
                    _createCollection = DefaultCollectionType.CreateDefaultConstructorFunc <object>();
                }
                else
                {
                    var collectionInheritorType =
                        _options.ExtraTypes.FirstOrDefault(t =>
                                                           !t.IsInterface &&
                                                           !t.IsAbstract &&
                                                           CollectionType.IsAssignableFrom(t) &&
                                                           t.HasDefaultConstructor());
                    _createCollection = collectionInheritorType.CreateDefaultConstructorFunc <object>();
                }
            }
            else if (CollectionType.HasDefaultConstructor())
            {
                _createCollection = CollectionType.CreateDefaultConstructorFunc <object>();
            }
            else
            {
                throw new ArgumentException("Unable to find suitable collection to create.");
            }
        }                                                                                                                                           // ReSharper restore DoNotCallOverridableMethodsInConstructor
Пример #12
0
        public static string SerializeObject(
            this IXmlSerializerInternal serializer,
            object instance,
            Encoding encoding,
            Formatting formatting,
            ISerializeOptions options)
        {
            options = options.WithNewSerializationState();

            var sb = new StringBuilder();

            using (var stringWriter = new StringWriterWithEncoding(sb, encoding ?? Encoding.UTF8))
            {
                using (var xmlWriter = new XSerializerXmlTextWriter(stringWriter, options))
                {
                    xmlWriter.Formatting = formatting;
                    serializer.SerializeObject(xmlWriter, instance, options);
                }
            }

            return(sb.ToString());
        }
Пример #13
0
        internal XmlSerializationProvider(
            IXmlSerializerInternal serializer, XmlWriter writer, String elementName, Boolean asAttribute = false,
            XmlItemDefAttributeCollection itemDefAttributes = null
            ) : base(serializer, elementName, asAttribute, itemDefAttributes)
        {
            if (serializer == null)
            {
                throw new ArgumentNullException();
            }
            if (writer == null)
            {
                throw new ArgumentNullException();
            }
            if (String.IsNullOrEmpty(elementName))
            {
                throw new ArgumentNullException();
            }
            if (writer.WriteState != WriteState.Closed)
            {
                throw new ArgumentException();
            }

            this.xmlWriter = writer;
        }
Пример #14
0
        internal XmlDeserializationProvider(
            IXmlSerializerInternal serializer, XmlReader reader, String elementName, Boolean asAttribute = false,
            XmlItemDefAttributeCollection itemDefAttributes = null
            ) : base(serializer, elementName, asAttribute, itemDefAttributes)
        {
            if (serializer == null)
            {
                throw new ArgumentNullException();
            }
            if (reader == null)
            {
                throw new ArgumentNullException();
            }
            if (String.IsNullOrEmpty(elementName))
            {
                throw new ArgumentNullException();
            }
            if (reader.ReadState == ReadState.Closed || reader.ReadState == ReadState.Error)
            {
                throw new ArgumentException();
            }

            this.xmlReader = reader;
        }
Пример #15
0
        private static object DeserializeKeyOrValue(XSerializerXmlReader reader, IXmlSerializerInternal serializer, ISerializeOptions options, out bool shouldIssueRead)
        {
            var deserialized = serializer.DeserializeObject(reader, options);

            shouldIssueRead = true;

            return deserialized;
        }
Пример #16
0
 public static object DeserializeObject(this IXmlSerializerInternal serializer, string xml)
 {
     return(serializer.DeserializeObject(xml, new TestSerializeOptions()));
 }
Пример #17
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;
        }