Пример #1
0
        public void CanDeserializeClassWithPrivateConstructor()
        {
            var child = new ClassWithPrivateConstructor("child");

            var data = new ClassWithPrivateConstructor("MyName")
            {
                Complex = child
            };

            var settings   = new SharpSerializerXmlSettings();
            var serializer = new SharpSerializer(settings);

            using (var stream = new MemoryStream())
            {
                serializer.Serialize(data, stream);

                stream.Position = 0;

                var deserialized = serializer.Deserialize(stream) as ClassWithPrivateConstructor;

                Assert.IsNotNull(deserialized);
                Assert.AreEqual(data.Name, deserialized.Name);
                Assert.AreEqual(data.Complex.Name, deserialized.Complex.Name);
            }
        }
        private void initialize(SharpSerializerXmlSettings settings)
        {
            // PropertiesToIgnore
            PropertyProvider.PropertiesToIgnore = settings.AdvancedSettings.PropertiesToIgnore;
            PropertyProvider.AttributesToIgnore = settings.AdvancedSettings.AttributesToIgnore;
            //RootName
            RootName = settings.AdvancedSettings.RootName;
            // TypeNameConverter)
            ITypeNameConverter typeNameConverter = settings.AdvancedSettings.TypeNameConverter ??
                                                   DefaultInitializer.GetTypeNameConverter(
                settings.IncludeAssemblyVersionInTypeName,
                settings.IncludeCultureInTypeName,
                settings.IncludePublicKeyTokenInTypeName);
            // SimpleValueConverter
            ISimpleValueConverter simpleValueConverter = settings.AdvancedSettings.SimpleValueConverter ??
                                                         DefaultInitializer.GetSimpleValueConverter(settings.Culture, typeNameConverter);
            // XmlWriterSettings
            XmlWriterSettings xmlWriterSettings = DefaultInitializer.GetXmlWriterSettings(settings.Encoding);
            // XmlReaderSettings
            XmlReaderSettings xmlReaderSettings = DefaultInitializer.GetXmlReaderSettings();

            // Create Serializer and Deserializer
            var reader = new DefaultXmlReader(typeNameConverter, simpleValueConverter, xmlReaderSettings);
            var writer = new DefaultXmlWriter(typeNameConverter, simpleValueConverter, xmlWriterSettings);

            _serializer   = new XmlPropertySerializer(writer);
            _deserializer = new XmlPropertyDeserializer(reader);
        }
Пример #3
0
        public void XmlSerial_ShouldSerializeDateTimeOffset()
        {
            var parent = new ClassWithDateTimeOffset
            {
                Date          = DateTimeOffset.Now,
                DateTimeLocal = new DateTime(2021, 12, 11, 10, 9, 8, DateTimeKind.Local),
                DateTimeUtc   = new DateTime(2021, 12, 11, 10, 9, 8, DateTimeKind.Utc)
            };

            var stream     = new MemoryStream();
            var settings   = new SharpSerializerXmlSettings();
            var serializer = new SharpSerializer(settings);

            serializer.Serialize(parent, stream);

            stream.Position = 0;
            XmlDocument doc = new XmlDocument();

            doc.Load(stream);
            System.Console.WriteLine(doc.InnerXml);

            serializer      = new SharpSerializer(settings);
            stream.Position = 0;
            ClassWithDateTimeOffset loaded = serializer.Deserialize(stream) as ClassWithDateTimeOffset;

            Assert.AreEqual(parent.Date, loaded.Date);

            // Additional tests concerning
            // https://github.com/polenter/SharpSerializer/issues/17#issuecomment-994484009
            Assert.AreEqual(parent.DateTimeLocal, loaded.DateTimeLocal);
            Assert.AreEqual(parent.DateTimeLocal.Kind, loaded.DateTimeLocal.Kind);
            Assert.AreEqual(parent.DateTimeUtc, loaded.DateTimeUtc);
            Assert.AreEqual(parent.DateTimeUtc.Kind, loaded.DateTimeUtc.Kind);
        }
Пример #4
0
        public void XmlSerial_ShouldSerializeGuid()
        {
            var parent = new ClassWithGuid()
            {
                Guid = Guid.NewGuid(),
            };

            var stream     = new MemoryStream();
            var settings   = new SharpSerializerXmlSettings();
            var serializer = new SharpSerializer(settings);

            serializer.Serialize(parent, stream);

            stream.Position = 0;
            XmlDocument doc = new XmlDocument();

            doc.Load(stream);
            System.Console.WriteLine(doc.InnerXml);

            serializer      = new SharpSerializer(settings);
            stream.Position = 0;
            ClassWithGuid loaded = serializer.Deserialize(stream) as ClassWithGuid;

            Assert.AreEqual(parent.Guid, loaded.Guid, "same guid");
        }
Пример #5
0
        private static XmlDocument Save(object data)
        {
            var stream   = new MemoryStream();
            var settings = new SharpSerializerXmlSettings();

            settings.AdvancedSettings.PropertiesToIgnore.Add(typeof(Class2BeSerialized), "NameRule");
            settings.AdvancedSettings.PropertiesToIgnore.Add(typeof(Class2BeSerialized), "ComplexRule");

            settings.AdvancedSettings.PropertyTypesToIgnore.Add(typeof(Class2BeIgnored));

            settings.AdvancedSettings.AttributesToIgnore.Add(typeof(MyExcludeAttribute));
            // this does not work
            //settings.AdvancedSettings.PropertiesToIgnore.Add(null, "NameRule");
            //settings.AdvancedSettings.PropertiesToIgnore.Add(null, "ComplexRule");
            var serializer = new SharpSerializer(settings);

            serializer.Serialize(data, stream);

            stream.Position = 0;

            XmlDocument doc = new XmlDocument();

            doc.Load(stream);

            return(doc);
        }
 /// <summary>
 ///   Xml serialization with custom settings
 /// </summary>
 /// <param name = "settings"></param>
 public SharpSerializer(SharpSerializerXmlSettings settings)
 {
     if (settings == null)
     {
         throw new ArgumentNullException("settings");
     }
     initialize(settings);
 }
Пример #7
0
        public void XmlSerial_TwoIdenticalChildsShouldBeSameInstance()
        {
            var parent = new ParentChildTestClass()
            {
                Name = "parent",
            };

            var child = new ParentChildTestClass()
            {
                Name   = "child",
                Father = parent,
                Mother = parent,
            };

            Assert.AreSame(child.Father, child.Mother, "Precondition: Saved Father and Mother are same instance");

            var stream     = new MemoryStream();
            var settings   = new SharpSerializerXmlSettings();
            var serializer = new SharpSerializer(settings);

            serializer.Serialize(child, stream);

            /*
             *  <Complex name="Root" type="Polenter.Serialization.XmlSerialisationTests+ParentChildTestClass, SharpSerializer.Tests">
             *          <Properties>
             *                  <Simple name="Name" value="child" />
             *                  <Complex name="Mother" id="1">
             *                          <Properties>
             *                                  <Simple name="Name" value="parent" />
             *                                  <Null name="Mother" />
             *                                  <Null name="Father" />
             *                          </Properties>
             *                  </Complex>
             *                  <ComplexReference name="Father" id="1" />
             *          </Properties>
             *  </Complex>
             */
            stream.Position = 0;
            XmlDocument doc = new XmlDocument();

            doc.Load(stream);
            System.Console.WriteLine(doc.InnerXml);

            serializer      = new SharpSerializer(settings);
            stream.Position = 0;
            ParentChildTestClass loaded = serializer.Deserialize(stream) as ParentChildTestClass;

            Assert.AreSame(loaded.Father, loaded.Mother, "Loaded Father and Mother are same instance");
        }
Пример #8
0
        public void CannotDeserializeClassWithoutParameterlessConstructor()
        {
            var child = new ClassWithoutParameterlessConstructor("child");

            var data = new ClassWithoutParameterlessConstructor("MyName")
            {
                Complex = child
            };

            var settings   = new SharpSerializerXmlSettings();
            var serializer = new SharpSerializer(settings);

            using (var stream = new MemoryStream())
            {
                serializer.Serialize(data, stream);

                stream.Position = 0;

                serializer.Deserialize(stream);
            }
        }