public DataContractSerializer(Type type, DataContractSerializerSettings settings)
 {
     if (settings == null)
     {
         settings = new DataContractSerializerSettings();
     }
     Initialize(type, settings.RootName, settings.RootNamespace, settings.KnownTypes, settings.MaxItemsInObjectGraph, false,
         settings.PreserveObjectReferences, settings.DataContractResolver, settings.SerializeReadOnlyTypes);
 }
        public DataContractSerializer(ITypeProvider typeProvider)
        {
            var dataContractResolver = new NimbusDataContractResolver(typeProvider);

            _settings = new DataContractSerializerSettings
                        {
                            DataContractResolver = dataContractResolver,
                            SerializeReadOnlyTypes = true,
                            MaxItemsInObjectGraph = int.MaxValue,
                            KnownTypes = typeProvider.AllSerializableTypes()
                        };
        }
        /// <summary>
        /// Initializes a new instance of DataContractSerializerInputFormatter
        /// </summary>
        public XmlDataContractSerializerInputFormatter()
        {
            SupportedEncodings.Add(UTF8EncodingWithoutBOM);
            SupportedEncodings.Add(UTF16EncodingLittleEndian);

            SupportedMediaTypes.Add(MediaTypeHeaderValues.ApplicationXml);
            SupportedMediaTypes.Add(MediaTypeHeaderValues.TextXml);

            _serializerSettings = new DataContractSerializerSettings();

            WrapperProviderFactories = new List<IWrapperProviderFactory>();
            WrapperProviderFactories.Add(new SerializableErrorWrapperProviderFactory());
        }
        /// <summary>
        /// Initializes a new instance of DataContractSerializerInputFormatter
        /// </summary>
        public XmlDataContractSerializerInputFormatter()
        {
            SupportedEncodings.Add(Encodings.UTF8EncodingWithoutBOM);
            SupportedEncodings.Add(Encodings.UTF16EncodingLittleEndian);

            SupportedMediaTypes.Add(MediaTypeHeaderValue.Parse("application/xml"));
            SupportedMediaTypes.Add(MediaTypeHeaderValue.Parse("text/xml"));

            _serializerSettings = new DataContractSerializerSettings();

            WrapperProviderFactories = new List<IWrapperProviderFactory>();
            WrapperProviderFactories.Add(new SerializableErrorWrapperProviderFactory());

            _dataAnnotationRequiredAttributeValidation = new DataAnnotationRequiredAttributeValidation();
        }
        public void SerializationEvents()
        {
            var settings = new DataContractSerializerSettings
            {
                DataContractResolver = new ProxyDataContractResolver()
            };

            var tester = new XmlDataContractSerializerTester<IEnumerable<Category>>(
                _testOutputHelper,
                new DataContractSerializer(typeof (IEnumerable<Category>), settings),
                true);

            List<Category> categories = _dbContext.Categories.ToList();

            var deserializedCategories = tester.SerializeAndDeserialize(categories).ToList();
        }
        /// <summary>
        /// Initializes a new instance of <see cref="XmlDataContractSerializerOutputFormatter"/>
        /// </summary>
        /// <param name="writerSettings">The settings to be used by the <see cref="DataContractSerializer"/>.</param>
        public XmlDataContractSerializerOutputFormatter([NotNull] XmlWriterSettings writerSettings)
        {
            SupportedEncodings.Add(Encodings.UTF8EncodingWithoutBOM);
            SupportedEncodings.Add(Encodings.UTF16EncodingLittleEndian);

            SupportedMediaTypes.Add(MediaTypeHeaderValue.Parse("application/xml"));
            SupportedMediaTypes.Add(MediaTypeHeaderValue.Parse("text/xml"));

            WriterSettings = writerSettings;

            _serializerSettings = new DataContractSerializerSettings();

            WrapperProviderFactories = new List<IWrapperProviderFactory>();
            WrapperProviderFactories.Add(new EnumerableWrapperProviderFactory(WrapperProviderFactories));
            WrapperProviderFactories.Add(new SerializableErrorWrapperProviderFactory());
        }
        /// <summary>
        /// Initializes a new instance of <see cref="XmlDataContractSerializerOutputFormatter"/>
        /// </summary>
        /// <param name="writerSettings">The settings to be used by the <see cref="DataContractSerializer"/>.</param>
        public XmlDataContractSerializerOutputFormatter([NotNull] XmlWriterSettings writerSettings)
        {
            SupportedEncodings.Add(Encoding.UTF8);
            SupportedEncodings.Add(Encoding.Unicode);

            SupportedMediaTypes.Add(MediaTypeHeaderValues.ApplicationXml);
            SupportedMediaTypes.Add(MediaTypeHeaderValues.TextXml);

            WriterSettings = writerSettings;

            _serializerSettings = new DataContractSerializerSettings();

            WrapperProviderFactories = new List<IWrapperProviderFactory>();
            WrapperProviderFactories.Add(new EnumerableWrapperProviderFactory(WrapperProviderFactories));
            WrapperProviderFactories.Add(new SerializableErrorWrapperProviderFactory());
        }
        public void ISerializable()
        {
            var dataContractSerializerSettings = new DataContractSerializerSettings
            {
                DataContractResolver = this.resolver
            };

            var dataContractSerializer = new DataContractSerializer(
                typeof(IEnumerable<Product>),
                dataContractSerializerSettings);

            var products = this.context.Products.ToList();

            var tester = new XmlDataContractSerializerTester<IEnumerable<Product>>(dataContractSerializer, true);

            var productsBack = tester.SerializeAndDeserialize(products);
        }
        public void ISerializable()
        {
            var settings = new DataContractSerializerSettings
            {
                DataContractResolver = new ProxyDataContractResolver(),
                KnownTypes = new List<Type>() {typeof (Supplier)}
            };

            var tester = new XmlDataContractSerializerTester<IEnumerable<Product>>(
                _testOutputHelper,
                new DataContractSerializer(typeof (IEnumerable<Product>), settings),
                true);

            var products = _dbContext.Products.ToList();

            var deserializedOrders = tester.SerializeAndDeserialize(products);
        }
        public async Task ReadAsync_ReadsWhenConfiguredWithKnownTypes()
        {
            // Arrange
            var expectedInt = 10;
            var expectedString = "TestString";
            var KnownTypeName = "SomeDummyClass";
            var InstanceNamespace = "http://www.w3.org/2001/XMLSchema-instance";

            var input = string.Format(
                    "<DummyClass i:type=\"{0}\" xmlns:i=\"{1}\"><SampleInt>{2}</SampleInt>"
                    + "<SampleString>{3}</SampleString></DummyClass>",
                    KnownTypeName,
                    InstanceNamespace,
                    expectedInt,
                    expectedString);
            var settings = new DataContractSerializerSettings
            {
                KnownTypes = new[] { typeof(SomeDummyClass) }
            };
            var formatter = new XmlDataContractSerializerInputFormatter
            {
                SerializerSettings = settings
            };
            var contentBytes = Encoding.UTF8.GetBytes(input);
            var context = GetInputFormatterContext(contentBytes, typeof(DummyClass));

            // Act
            var result = await formatter.ReadAsync(context);

            // Assert
            Assert.NotNull(result);
            Assert.False(result.HasError);
            var model = Assert.IsType<SomeDummyClass>(result.Model);
            Assert.Equal(expectedInt, model.SampleInt);
            Assert.Equal(expectedString, model.SampleString);
        }
        public async Task ReadAsync_ReadsWhenConfiguredWithRootName()
        {
            // Arrange
            var expectedInt = 10;
            var SubstituteRootName = "SomeOtherClass";
            var SubstituteRootNamespace = "http://tempuri.org";

            var input = string.Format(
                "<{0} xmlns=\"{1}\"><SampleInt xmlns=\"\">{2}</SampleInt></{0}>",
                SubstituteRootName,
                SubstituteRootNamespace,
                expectedInt);

            var dictionary = new XmlDictionary();
            var settings = new DataContractSerializerSettings
            {
                RootName = dictionary.Add(SubstituteRootName),
                RootNamespace = dictionary.Add(SubstituteRootNamespace)
            };
            var formatter = new XmlDataContractSerializerInputFormatter
            {
                SerializerSettings = settings
            };
            var contentBytes = Encoding.UTF8.GetBytes(input);
            var context = GetInputFormatterContext(contentBytes, typeof(DummyClass));

            // Act
            var result = await formatter.ReadAsync(context);

            // Assert
            Assert.NotNull(result);
            Assert.False(result.HasError);
            var model = Assert.IsType<DummyClass>(result.Model);
            Assert.Equal(expectedInt, model.SampleInt);
        }
        public async Task WriteAsync_WritesWhenConfiguredWithPreserveReferences()
        {
            // Arrange
            var sampleId = 1;
            var sampleName = "Parent";
            var InstanceNamespace = "http://www.w3.org/2001/XMLSchema-instance";
            var SerializationNamespace = "http://schemas.microsoft.com/2003/10/Serialization/";

            var expectedOutput = string.Format(
                    "<Parent xmlns:i=\"{0}\" z:Id=\"{2}\" xmlns:z=\"{1}\">" +
                    "<Children z:Id=\"2\" z:Size=\"1\">" +
                    "<Child z:Id=\"3\"><Id>{2}</Id><Parent z:Ref=\"1\" i:nil=\"true\" />" +
                    "</Child></Children><Name z:Id=\"4\">{3}</Name></Parent>",
                    InstanceNamespace,
                    SerializationNamespace,
                    sampleId,
                    sampleName);

            var child = new Child { Id = sampleId };
            var parent = new Parent { Name = sampleName, Children = new List<Child> { child } };
            child.Parent = parent;

            var settings = new DataContractSerializerSettings
            {
                PreserveObjectReferences = true
            };
            var formatter = new XmlDataContractSerializerOutputFormatter
            {
                SerializerSettings = settings
            };
            var outputFormatterContext = GetOutputFormatterContext(parent, parent.GetType());

            // Act
            await formatter.WriteAsync(outputFormatterContext);

            // Assert
            var body = outputFormatterContext.HttpContext.Response.Body;
            body.Position = 0;

            var content = new StreamReader(body).ReadToEnd();
            XmlAssert.Equal(expectedOutput, content);
        }
        public async Task WriteAsync_WritesWhenConfiguredWithKnownTypes()
        {
            // Arrange
            var sampleInt = 10;
            var sampleString = "TestString";
            var KnownTypeName = "SomeDummyClass";
            var InstanceNamespace = "http://www.w3.org/2001/XMLSchema-instance";

            var expectedOutput = string.Format(
                    "<DummyClass xmlns:i=\"{1}\" xmlns=\"\" i:type=\"{0}\"><SampleInt>{2}</SampleInt>"
                    + "<SampleString>{3}</SampleString></DummyClass>",
                    KnownTypeName,
                    InstanceNamespace,
                    sampleInt,
                    sampleString);

            var sampleInput = new SomeDummyClass
            {
                SampleInt = sampleInt,
                SampleString = sampleString
            };

            var settings = new DataContractSerializerSettings
            {
                KnownTypes = new[] { typeof(SomeDummyClass) }
            };
            var formatter = new XmlDataContractSerializerOutputFormatter
            {
                SerializerSettings = settings
            };
            var outputFormatterContext = GetOutputFormatterContext(sampleInput, typeof(DummyClass));

            // Act
            await formatter.WriteAsync(outputFormatterContext);

            // Assert
            var body = outputFormatterContext.HttpContext.Response.Body;
            body.Position = 0;

            var content = new StreamReader(body).ReadToEnd();
            XmlAssert.Equal(expectedOutput, content);
        }
        public async Task WriteAsync_WritesWhenConfiguredWithRootName()
        {
            // Arrange
            var sampleInt = 10;
            var SubstituteRootName = "SomeOtherClass";
            var SubstituteRootNamespace = "http://tempuri.org";
            var InstanceNamespace = "http://www.w3.org/2001/XMLSchema-instance";

            var expectedOutput = string.Format(
                "<{0} xmlns:i=\"{2}\" xmlns=\"{1}\"><SampleInt xmlns=\"\">{3}</SampleInt></{0}>",
                SubstituteRootName,
                SubstituteRootNamespace,
                InstanceNamespace,
                sampleInt);

            var sampleInput = new DummyClass { SampleInt = sampleInt };

            var dictionary = new XmlDictionary();
            var settings = new DataContractSerializerSettings
            {
                RootName = dictionary.Add(SubstituteRootName),
                RootNamespace = dictionary.Add(SubstituteRootNamespace)
            };
            var formatter = new XmlDataContractSerializerOutputFormatter
            {
                SerializerSettings = settings
            };
            var outputFormatterContext = GetOutputFormatterContext(sampleInput, sampleInput.GetType());

            // Act
            await formatter.WriteAsync(outputFormatterContext);

            // Assert
            var body = outputFormatterContext.HttpContext.Response.Body;
            body.Position = 0;

            var content = new StreamReader(body).ReadToEnd();
            XmlAssert.Equal(expectedOutput, content);
        }
		public DataContractSerializer (Type type, DataContractSerializerSettings settings)
			: this (type, settings.RootName, settings.RootNamespace, settings.KnownTypes,
			        settings.MaxItemsInObjectGraph, settings.IgnoreExtensionDataObject,
			        settings.PreserveObjectReferences, settings.DataContractSurrogate,
			        settings.DataContractResolver)
		{
		}
Exemple #16
0
        public static async Task Serialize(StorageFolder folder, string fileName, object instance)
        {
            StorageFile newFile = await folder.CreateFileAsync(fileName,
                CreationCollisionOption.ReplaceExisting);

            Stream stream = await newFile.OpenStreamForWriteAsync();

            DataContractSerializerSettings dataSettings = new DataContractSerializerSettings()
            {
                DataContractResolver = new MyCustomerResolver()
            };

            DataContractSerializer serializer = new DataContractSerializer(instance.GetType(), dataSettings);

            var settings = new XmlWriterSettings()
            {
                Indent = true,
                IndentChars = "\t"
            };

            using (var writer = XmlWriter.Create(stream, settings))
            {
                serializer.WriteObject(writer, instance);
            }

            stream.Dispose();
        }
 private static DataContractSerializer CreateSerializer()
 {
     var settings = new DataContractSerializerSettings();
     settings.IgnoreExtensionDataObject = true;
     settings.PreserveObjectReferences = true;
     return new DataContractSerializer( typeof( FileSystemImpl ), settings );
 }
 public static DbExportArgs FromDataContractFile(string filename)
 {
     var settings = new DataContractSerializerSettings();
     var serializer = new DataContractSerializer(typeof(DbExportArgs), settings);
     using (var stream = new FileStream(filename, FileMode.Open, FileAccess.Read))
     {
         return (DbExportArgs)serializer.ReadObject(stream);
     }
 }
        public void SerializationCallbacks()
        {
            var dataContractSerializerSettings = new DataContractSerializerSettings
            {
                DataContractResolver = this.resolver
            };
            var dataContractSerializer = new DataContractSerializer(typeof(Category), dataContractSerializerSettings);

            var tester = new XmlDataContractSerializerTester<Category>(dataContractSerializer, true);

            var category = this.context.Categories.First();

            var categoryBack = tester.SerializeAndDeserialize(category);
        }
Exemple #20
0
        public static async Task<object> Deserialize(Type type, StorageFolder folder, string fileName)
        {
            try
            {
                StorageFile newFile = await folder.GetFileAsync(fileName);
                Stream stream = await newFile.OpenStreamForReadAsync();

                DataContractSerializerSettings settings = new DataContractSerializerSettings()
                {
                    DataContractResolver = new MyCustomerResolver()
                };

                DataContractSerializer serializer = new DataContractSerializer(type, settings);

                object item = (object)serializer.ReadObject(stream);
         
                stream.Dispose();

                return item;
            }
            catch (Exception ex)
            {
                Debug.WriteLine("ERROR B10: " + ex.Message);
            }

            return null;
        }
        public void IDataContractSurrogate()
        {
            var settings = new DataContractSerializerSettings
            {
                DataContractResolver = new ProxyDataContractResolver(),
                DataContractSurrogate = new OrderDataContractSurrogate(),
            };

            var tester = new XmlDataContractSerializerTester<IEnumerable<Order>>(
                _testOutputHelper,
                new DataContractSerializer(typeof(IEnumerable<Order>), settings),
                true);

            var orders = _dbContext.Orders.Take(10).ToList();

            var deserializedOrders = tester.SerializeAndDeserialize(orders);
        }
Exemple #22
0
 public DataContractSerializer(System.Type type, System.Runtime.Serialization.DataContractSerializerSettings settings)
 {
 }