public void Build_ShouldBuildFullDescription()
        {
            var builder = new FluentSchemaDescription();

            builder.Element<Person>(Ns + "Person")
                    .Attribute(Ns + "Id", x => x.Id)
                    .Attribute(Ns + "FirstName", x => x.FirstName)
                    .Attribute(Ns + "LastName", x => x.LastName)
                    .Attribute(Ns + "IsEnabled", x => x.IsEnabled)
                    .Element(Ns + "Address", x => x.Address)
                        .Attribute(Ns + "StreetName", x => x.StreetName)
                        .Attribute(Ns + "City", x => x.City)
                    .EndElement()
                    .Element(Ns + "ContactMethods", x => x.ContactMethods)
                        .CollectionElement<ContactMethod>(Ns + "ContactMethod")
                            .Attribute(Ns + "Type", x => x.Type)
                            .Attribute(Ns + "Value", x => x.Value)
                        .EndElement()
                        .CollectionElement<AddressContactMethod>(Ns + "AddressContactMethod")
                        .EndElement()
                    .EndElement();

            var schema = builder.Build();

            schema.Mappings.Count().ShouldBe(5);
            schema.TryFindMappingForType<Person>().ShouldBeTypeOf(typeof(ElementMapping<Person>));
            schema.TryFindMappingForType<Address>().ShouldBeTypeOf(typeof(ChildElementMapping<Person, Address>));
            schema.TryFindMappingForType<List<ContactMethod>>().ShouldBeTypeOf(typeof(ChildElementMapping<Person, List<ContactMethod>>));
            schema.TryFindMappingForType<ContactMethod>().ShouldBeTypeOf(typeof(ElementMapping<ContactMethod>));
            schema.TryFindMappingForType<AddressContactMethod>().ShouldBeTypeOf(typeof(ElementMapping<AddressContactMethod>));
        }
Пример #2
0
        public void CustomAnyAttributeSerializer_ShouldDeserializeToCustomValue()
        {
            var description = new FluentSchemaDescription();
            description.Element<Person>("Person")
                       .AnyAttribute<string>(x => x.CustomStringAttributes,
                                             reader => "Attribute:" + reader.LocalName + "=" + reader.Value,
                                             (writer, attr) => writer.WriteAttributeString("Oops", "Lost Information: " + attr));

            var schema = description.Build();

            var serializer = new Serializer(schema);

            var person = serializer.Deserialize<Person>("<Person Custom1='Value1' Custom2='Value2' />".ToStream());

            person.CustomStringAttributes.Count.ShouldBe(2);
            person.CustomStringAttributes[0].ShouldBe("Attribute:Custom1=Value1");
            person.CustomStringAttributes[1].ShouldBe("Attribute:Custom2=Value2");
        }
Пример #3
0
        public void CustomAnyAttributeSerializer_ShouldSerializeToCustomValue()
        {
            int i = 0;
            var description = new FluentSchemaDescription();
            description.Element<Person>("Person")
                       .AnyAttribute<string>(x => x.CustomStringAttributes,
                                             reader => "Attribute:" + reader.LocalName + "=" + reader.Value,
                                             (writer, attr) => writer.WriteAttributeString("CustomAttr" + (++i), "MissingValue"));

            var schema = description.Build();

            var serializer = new Serializer(schema);

            var person = serializer.Deserialize<Person>("<Person Custom1='Value1' Custom2='Value2' />".ToStream());
            var stream = new MemoryStream();
            serializer.Serialize(stream, person);

            XAssert.AreEqual("<Person CustomAttr1='MissingValue' CustomAttr2='MissingValue' />",
                             stream.ToXDocument().ToString());
        }
Пример #4
0
        internal static SchemaDescription FullSchema()
        {
            var description = new FluentSchemaDescription();

            description.Element<Document>(Ns + "Persons")
                           .CollectionElement(Ns + "Person", x => x.Persons)
                               .Attribute("Id", x => x.Id)
                               .Attribute("FirstName", x => x.FirstName)
                               .Attribute("LastName", x => x.LastName)
                               .Attribute("DateOfBirth", x => x.DateOfBirth)
                               .Attribute("TimeSinceLastLogin", x => x.TimeSinceLastLogin,
                                                                x => x != null ? TimeSpan.Parse(x) : (TimeSpan?)null,
                                                                x => x != null ? x.ToString() : (string)null)
                               .TextElement(Ns + "IsEnabled", x => x.IsEnabled)
                               .Element(Ns + "Address", x => x.Address)
                                   .Attribute("StreetName", x => x.StreetName)
                                   .Attribute("City", x => x.City)
                                   .TextContent(x => x.Comments)
                               .EndElement()
                               .Element(Ns + "ContactMethods", x => x.ContactMethods)
                                   .CollectionElement<ContactMethod>(Ns + "ContactMethod")
                                       .Attribute("Type", x => x.Type)
                                       .Attribute("Value", x => x.Value)
                                   .EndElement()
                                   .CollectionElement<AddressContactMethod>(Ns + "AddressContactMethod")
                                       .Attribute("Type", x => x.Type)
                                       .Attribute("Value", x => x.Value)
                                       .Attribute("StreetName", x => x.StreetName)
                                   .EndElement()
                               .EndElement()
                           .EndElement();

            return description.Build();
        }
Пример #5
0
        public void CustomAnyElementSerializer_ShouldDeserializeToCustomValue()
        {
            var description = new FluentSchemaDescription();
            description.Element<Person>("Person")
                       .AnyElement<int>(x => x.CustomIntegerElements,
                                             reader => reader.ReadElementContentAsInt(),
                                             (writer, value) => writer.WriteElementString("NewName", value.ToString()));

            var schema = description.Build();

            var serializer = new Serializer(schema);

            var person = serializer.Deserialize<Person>(
                @"<Person>
                    <Custom1>34</Custom1>
                    <Custom2>35</Custom2>
                  </Person>".ToStream());

            person.CustomIntegerElements.Count.ShouldBe(2);
            person.CustomIntegerElements[0].ShouldBe(34);
            person.CustomIntegerElements[1].ShouldBe(35);
        }
Пример #6
0
        public void CustomAnyElementSerializer_ShouldSerializeToCustomValue()
        {
            var description = new FluentSchemaDescription();
            description.Element<Person>("Person")
                       .AnyElement<int>(x => x.CustomIntegerElements,
                                             reader => reader.ReadElementContentAsInt(),
                                             (writer, value) => writer.WriteElementString("NewName", value.ToString()));

            var schema = description.Build();

            var serializer = new Serializer(schema);

            var person = new Person {CustomIntegerElements = new List<int> {1, 2, 3}};
            var stream = new MemoryStream();
            serializer.Serialize(stream, person);

            XAssert.AreEqual(@"<Person>
                                 <NewName>1</NewName>
                                 <NewName>2</NewName>
                                 <NewName>3</NewName>
                               </Person>",
                             stream.ToXDocument().ToString());
        }