Пример #1
0
        private void SerializeExpandoObject(XSerializerXmlTextWriter writer, IDictionary <string, object> expando, ISerializeOptions options)
        {
            if (expando == null && !options.ShouldEmitNil)
            {
                return;
            }

            writer.WriteStartDocument();
            writer.WriteStartElement(_options.RootElementName);
            writer.WriteDefaultDocumentNamespaces();

            using (writer.WriteDefaultNamespace(_options.DefaultNamespace))
            {
                if (expando == null)
                {
                    writer.WriteNilAttribute();
                    writer.WriteEndElement();
                    return;
                }

                var setIsEncryptionEnabledBackToFalse = writer.MaybeSetIsEncryptionEnabledToTrue(_encryptAttribute, options);

                foreach (var property in expando)
                {
                    if (property.Value == null)
                    {
                        continue;
                    }

                    IXmlSerializerInternal serializer;

                    if (property.Value is ExpandoObject)
                    {
                        serializer = DynamicSerializer.GetSerializer <ExpandoObject>(null, _options.WithRootElementName(property.Key));
                    }
                    else
                    {
                        serializer = CustomSerializer.GetSerializer(property.Value.GetType(), null, _options.WithRootElementName(property.Key));
                    }

                    serializer.SerializeObject(writer, property.Value, options);
                }

                if (setIsEncryptionEnabledBackToFalse)
                {
                    writer.IsEncryptionEnabled = false;
                }

                writer.WriteEndElement();
            }
        }
Пример #2
0
        public void SerializeObject(XSerializerXmlTextWriter writer, object value, ISerializeOptions options)
        {
            var wasEmptyWriter = writer.IsEmpty;

            writer.WriteStartDocument();

            if (value != null)
            {
                WriteElement(writer, w => w.WriteValue(_valueConverter.GetString(value, options)), options);
            }
            else if (_alwaysEmitNil || options.ShouldEmitNil || wasEmptyWriter)
            {
                WriteElement(writer, w => w.WriteNilAttribute(), options);
            }
        }
Пример #3
0
        public void SerializeObject(XSerializerXmlTextWriter writer, object instance, ISerializeOptions options)
        {
            if (instance == null && !options.ShouldEmitNil)
            {
                return;
            }

            writer.WriteStartDocument();
            writer.WriteStartElement(_options.RootElementName);
            writer.WriteDefaultDocumentNamespaces();

            using (writer.WriteDefaultNamespace(_options.DefaultNamespace))
            {
                if (instance == null)
                {
                    writer.WriteNilAttribute();
                    writer.WriteEndElement();
                    return;
                }

                var setIsEncryptionEnabledBackToFalse = writer.MaybeSetIsEncryptionEnabledToTrue(_encryptAttribute, options);

                foreach (var item in GetDictionaryEntries(instance))
                {
                    writer.WriteStartElement("Item");

                    if (item.Key != null)
                    {
                        _keySerializer.SerializeObject(writer, item.Key, options);
                    }

                    if (item.Value != null)
                    {
                        _valueSerializer.SerializeObject(writer, item.Value, options);
                    }

                    writer.WriteEndElement();
                }

                if (setIsEncryptionEnabledBackToFalse)
                {
                    writer.IsEncryptionEnabled = false;
                }

                writer.WriteEndElement();
            }
        }
Пример #4
0
        public void SerializeObject(XSerializerXmlTextWriter writer, object instance, ISerializeOptions options)
        {
            if (instance == null && !options.ShouldEmitNil)
            {
                return;
            }

            if (_options.RootElementName != null)
            {
                writer.WriteStartDocument();
                writer.WriteStartElement(_options.RootElementName);
                writer.WriteDefaultDocumentNamespaces();
                writer.WriteDefaultNamespace(_options.DefaultNamespace).Dispose();
            }

            if (instance == null)
            {
                writer.WriteNilAttribute();
            }
            else
            {
                var setIsEncryptionEnabledBackToFalse = writer.MaybeSetIsEncryptionEnabledToTrue(_encryptAttribute, options);

                foreach (var item in (IEnumerable)instance)
                {
                    _itemSerializer.SerializeObject(writer, item, options);
                }

                if (setIsEncryptionEnabledBackToFalse)
                {
                    writer.IsEncryptionEnabled = false;
                }
            }

            if (_options.RootElementName != null)
            {
                writer.WriteEndElement();
            }
        }
Пример #5
0
        public void SerializeObject(XSerializerXmlTextWriter writer, object instance, ISerializeOptions options)
        {
            if (instance == null && !options.ShouldEmitNil)
            {
                return;
            }

            if (_options.RootElementName != null)
            {
                writer.WriteStartDocument();
                writer.WriteStartElement(_options.RootElementName);
                writer.WriteDefaultDocumentNamespaces();
                writer.WriteDefaultNamespace(_options.DefaultNamespace).Dispose();
            }

            if (instance == null)
            {
                writer.WriteNilAttribute();
            }
            else
            {
                var setIsEncryptionEnabledBackToFalse = writer.MaybeSetIsEncryptionEnabledToTrue(_encryptAttribute, options);

                foreach (var item in (IEnumerable)instance)
                {
                    _itemSerializer.SerializeObject(writer, item, options);
                }

                if (setIsEncryptionEnabledBackToFalse)
                {
                    writer.IsEncryptionEnabled = false;
                }
            }

            if (_options.RootElementName != null)
            {
                writer.WriteEndElement();
            }
        }
Пример #6
0
        public void SerializeObject(XSerializerXmlTextWriter writer, object instance, ISerializeOptions options)
        {
            if (instance == null && !options.ShouldEmitNil)
            {
                return;
            }

            writer.WriteStartDocument();
            writer.WriteStartElement(_options.RootElementName);
            writer.WriteDefaultDocumentNamespaces();

            using (writer.WriteDefaultNamespace(_options.DefaultNamespace))
            {
                if (instance == null)
                {
                    writer.WriteNilAttribute();
                    writer.WriteEndElement();
                    return;
                }

                var setIsEncryptionEnabledBackToFalse = writer.MaybeSetIsEncryptionEnabledToTrue(_encryptAttribute, options);

                foreach (var item in GetDictionaryEntries(instance))
                {
                    writer.WriteStartElement("Item");

                    if (item.Key != null)
                    {
                        _keySerializer.SerializeObject(writer, item.Key, options);
                    }

                    if (item.Value != null)
                    {
                        _valueSerializer.SerializeObject(writer, item.Value, options);
                    }

                    writer.WriteEndElement();
                }

                if (setIsEncryptionEnabledBackToFalse)
                {
                    writer.IsEncryptionEnabled = false;
                }

                writer.WriteEndElement();
            }
        }