public void ResolveUnknownContactTest()
        {
            var contracts = new XmlContract[] { };
            var resolver  = new XmlCustomContractResolver(contracts, null);

            Assert.Throws <XmlSerializationException>(() => resolver.ResolveContract(typeof(Foo)));
        }
        private void SetDefaultProperties(XmlContract contract, object target, XmlPropertyInfo[] propertyInfos)
        {
            for (var i = 0; i < propertyInfos.Length; i++)
            {
                var propertyState = propertyInfos[i];

                if (!propertyState.Assigned)
                {
                    var property = propertyState.Property;

                    if (property.DefaultValue != null && property.PropertyInfo.CanWrite)
                    {
                        SetValue(target, property, property.DefaultValue);
                    }
                    else if (property.IsRequired)
                    {
                        throw new XmlSerializationException(string.Format("Property \"{0}\" of type \"{1}\" is required.", property.PropertyName, contract.ValueType));
                    }
                }
                else if (propertyState.CollectionProxy != null)
                {
                    var collection = propertyState.CollectionProxy.GetResult();
                    SetValue(target, propertyState.Property, collection);
                }
            }
        }
        private void SetDefaultProperties(XmlContract contract, object target, XmlPropertyInfo[] propertyInfos)
        {
            foreach (var propertyState in propertyInfos)
            {
                if (!propertyState.Assigned)
                {
                    var property = propertyState.Property;

                    if (property.DefaultValue != null && property.PropertyInfo.CanWrite)
                    {
                        SetValue(target, property, property.DefaultValue);
                    }
                    else if (property.IsRequired)
                    {
                        throw new XmlSerializationException(
                                  $"Property \"{property.PropertyName}\" of type \"{contract.ValueType}\" is required.");
                    }
                }
                else if (propertyState.CollectionProxy != null)
                {
                    var collection = propertyState.CollectionProxy.GetResult();
                    SetValue(target, propertyState.Property, collection);
                }
            }
        }
Beispiel #4
0
 public XmlTypeContext(XmlContract contract, IXmlConverter readConverter, IXmlConverter writeConverter)
 {
     this.contract       = contract;
     this.readConverter  = readConverter;
     this.writeConverter = writeConverter;
     reader = readConverter != null ? readConverter.ReadXml : NotReadable(contract.ValueType);
     writer = writeConverter != null ? writeConverter.WriteXml : NotWritable(contract.ValueType);
 }
        public void ResolveUnknownContractByFallbackTest()
        {
            var fooContract = new XmlObjectContract(typeof(Foo), new XmlName("foo"));
            var contracts   = new XmlContract[] { fooContract };
            var resolver    = new XmlCustomContractResolver(contracts, new XmlContractResolver());

            var actual = resolver.ResolveContract(typeof(FooBar));

            Assert.AreSame(typeof(FooBar), actual.ValueType);
        }
        public void ResolveKnownContractTest()
        {
            var fooContract    = new XmlObjectContract(typeof(Foo), new XmlName("foo"));
            var fooBarContract = new XmlObjectContract(typeof(FooBar), new XmlName("fooBar"));

            var contracts = new XmlContract[] { fooBarContract, fooContract };

            var resolver = new XmlCustomContractResolver(contracts, null);

            Assert.AreSame(fooContract, resolver.ResolveContract(typeof(Foo)));
            Assert.AreSame(fooBarContract, resolver.ResolveContract(typeof(FooBar)));
        }
        internal void WriteXml(XmlWriter writer, object value, XmlMember member, XmlTypeContext typeContext)
        {
            XmlMember   lastMember   = currentMember;
            XmlContract lastContract = currentContract;

            currentMember   = member;
            currentContract = typeContext.Contract;

            typeContext.WriteXml(writer, value, this);

            currentMember   = lastMember;
            currentContract = lastContract;
        }
        internal object ReadXml(XmlReader reader, XmlMember member, XmlTypeContext typeContext)
        {
            XmlMember   lastMember   = currentMember;
            XmlContract lastContract = currentContract;

            currentMember   = member;
            currentContract = typeContext.Contract;

            object value = typeContext.ReadXml(reader, this);

            currentMember   = lastMember;
            currentContract = lastContract;

            return(value);
        }
Beispiel #9
0
        private static IXmlConverter GetConverter(XmlContract contract, IXmlConverter converter)
        {
            if (converter == null)
            {
                return(null);
            }

            var factory = converter as IXmlConverterFactory;

            if (factory != null)
            {
                converter = factory.CreateConverter(contract);
            }

            return(converter);
        }
        internal XmlSerializationContext(XmlSerializerSettings settings, XmlMember member, XmlContract contract)
            : this(settings)
        {
            if (contract == null)
            {
                throw new ArgumentNullException("contract");
            }

            if (member == null)
            {
                throw new ArgumentNullException("member");
            }

            currentContract = contract;
            currentMember   = member;
            initialState    = false;
        }
Beispiel #11
0
        public static void AreEqual(XmlContract expected, XmlContract actual)
        {
            if (expected == null)
            {
                Assert.IsNull(actual);
                return;
            }

            Assert.IsNotNull(actual);
            Assert.IsInstanceOf(expected.GetType(), actual);
            Assert.AreEqual(expected.Name, actual.Name);
            Assert.AreEqual(expected.ValueType, actual.ValueType);
            AreEqual(expected.Root, actual.Root);

            if (expected is XmlObjectContract)
            {
                var eo = (XmlObjectContract)expected;
                var ao = (XmlObjectContract)actual;

                AreEqual(eo.Item, ao.Item);
                Assert.AreEqual(eo.TypeHandling.HasValue, ao.TypeHandling.HasValue);
                Assert.AreEqual(eo.TypeHandling, ao.TypeHandling);

                Assert.AreEqual(eo.Properties.Count, ao.Properties.Count);

                for (var i = 0; i < eo.Properties.Count; i++)
                {
                    AreEqual(eo.Properties[i], ao.Properties[i]);
                }
            }
            else if (expected is XmlEnumContract)
            {
                var ee = (XmlEnumContract)expected;
                var ae = (XmlEnumContract)actual;

                var eitems = new List <XmlEnumItem>(ee.Items);
                var aitems = new List <XmlEnumItem>(ae.Items);

                for (var i = 0; i < eitems.Count; i++)
                {
                    Assert.AreEqual(eitems[i].Name, aitems[i].Name);
                    Assert.AreEqual(eitems[i].Value, aitems[i].Value);
                }
            }
        }
        public virtual IXmlConverter CreateConverter(XmlContract contract)
        {
            if (contract == null)
            {
                throw new ArgumentNullException("contract");
            }

            var valueType = contract.ValueType;

            if (!AcceptType(valueType))
            {
                throw new ArgumentException($"Type \"{valueType}\" is not acceptable.", "contract");
            }

            var converterType = GetConverterType(valueType);

            return((IXmlConverter)Activator.CreateInstance(converterType));
        }
Beispiel #13
0
 internal XmlSerializationContext(XmlSerializerSettings settings, XmlMember member, XmlContract contract)
     : this(settings)
 {
     Contract     = contract ?? throw new ArgumentNullException("contract");
     Member       = member ?? throw new ArgumentNullException("member");
     initialState = false;
 }
 protected override object CreateTarget(XmlContract contract)
 {
     return(new ValueProxy());
 }
 public static string ToXml <TValue>(this IXmlConverter converter, TValue value, XmlMember member = null, XmlContract contract = null, XmlSerializerSettings settings = null)
 {
     return(ToXml(converter, typeof(TValue), value, member, contract, settings));
 }
 protected virtual object CreateTarget(XmlContract contract)
 {
     return(contract.CreateDefault());
 }
        public static object ParseXml(this IXmlConverter converter, Type valueType, string xmlString, XmlMember member = null, XmlContract contract = null, XmlSerializerSettings settings = null)
        {
            var context = CreateContext(valueType, member, contract, settings);

            using (var input = new StringReader(xmlString))
                using (var reader = XmlReader.Create(input, context.Settings.GetReaderSettings()))
                {
                    while (reader.NodeType != XmlNodeType.Element && reader.Read())
                    {
                    }

                    if (reader.Name != "xml")
                    {
                        Assert.Fail("Expected start element \"xml\".");
                    }

                    var isAttribute = context.Member.MappingType == XmlMappingType.Attribute;

                    if (isAttribute)
                    {
                        reader.MoveToAttribute(context.Member.Name.LocalName, context.Member.Name.NamespaceUri);
                    }

                    var value = converter.ReadXml(reader, context);

                    if (isAttribute)
                    {
                        reader.MoveToElement();

                        if (reader.NodeType != XmlNodeType.Element || reader.Name != "xml")
                        {
                            Assert.Fail("Expected element \"xml\".");
                        }
                    }
                    else if (reader.NodeType != XmlNodeType.None)
                    {
                        Assert.Fail("Expected end of xml.");
                    }

                    return(value);
                }
        }
        public static string ToXml(this IXmlConverter converter, Type valueType, object value, XmlMember member = null, XmlContract contract = null, XmlSerializerSettings settings = null)
        {
            var builder = new StringBuilder();
            var context = CreateContext(valueType, member, contract, settings);

            using (var output = new StringWriter(builder))
                using (var writer = XmlWriter.Create(output, context.Settings.GetWriterSettings()))
                {
                    writer.WriteStartElement("xml");

                    var isAttribute = context.Member.MappingType == XmlMappingType.Attribute;

                    if (isAttribute)
                    {
                        writer.WriteStartAttribute(context.Member.Name);
                    }

                    converter.WriteXml(writer, value, context);

                    if (isAttribute)
                    {
                        writer.WriteEndAttribute();
                    }

                    writer.WriteEndElement();
                }

            return(builder.ToString());
        }
        private static XmlSerializationContext CreateContext(Type valueType, XmlMember member, XmlContract contract, XmlSerializerSettings settings)
        {
            if (settings == null)
            {
                settings = new XmlSerializerSettings
                {
                    OmitXmlDeclaration = true,
                    ContractResolver   = new XmlContractResolver(NamingConventions.CamelCase)
                };
            }

            if (contract == null)
            {
                contract = settings.ContractResolver.ResolveContract(valueType);
            }

            if (member == null)
            {
                member = contract.Root;
            }

            return(new XmlSerializationContext(settings, member, contract));
        }
 public static TValue ParseXml <TValue>(this IXmlConverter converter, string xmlString, XmlMember member = null, XmlContract contract = null, XmlSerializerSettings settings = null)
 {
     return((TValue)ParseXml(converter, typeof(TValue), xmlString, member, contract, settings));
 }