Exemplo n.º 1
0
        public void TestXmlSerializationStrategyWithCustomSettings()
        {
            var customSettings = new JsonSerializerSettings
            {
                Formatting        = Formatting.None,
                NullValueHandling = NullValueHandling.Ignore,
                ContractResolver  = new DefaultContractResolver
                {
                    NamingStrategy = new CamelCaseNamingStrategy()
                }
            };

            var strategy = new XmlSerializationStrategy(customSettings);

            var serializeRequest = new SecretAgent {
                Id = "007", Name = "Bond. James Bond"
            };

            var serializeResponse = strategy.Serialize(serializeRequest);
            var expectedResponse  = "<Root><name>Bond. James Bond</name><id>007</id></Root>";

            serializeResponse.Equals(expectedResponse).Should().BeTrue();

            var deserializeRequest  = serializeResponse;
            var deserializeResponse = strategy.Deserialize <SecretAgent>(deserializeRequest);

            deserializeResponse.Should().NotBeNull();
            deserializeResponse.Name.Should().Be("Bond. James Bond");
            deserializeResponse.Id.Should().Be("007");
        }
        protected override void CoreWriteObject(object obj, string objectName)
        {
            IXmlObject xmlObject;
            ITextSerializationStrategy serializationStrategy;

            if ((object)obj == null)
            {
                throw new ArgumentNullException(nameof(obj));
            }

            xmlObject = obj as IXmlObject;

            // this should support XPE, XML, JSON

            /*
             *      BACKLOG([email protected] / 2015 - 12 - 18):
             *      Refactor this logic that is common between this File and TextWriter Outputs.
             */
            if ((object)xmlObject != null)
            {
                serializationStrategy = new XpeSerializationStrategy(this.Xpe);
            }
            else if ((object)SolderFascadeAccessor.ReflectionFascade.GetOneAttribute <XmlRootAttribute>(obj.GetType()) != null)
            {
                serializationStrategy = new XmlSerializationStrategy();
            }
            else
            {
                serializationStrategy = new JsonSerializationStrategy();
            }

            serializationStrategy.SetObjectToWriter(this.CurrentTextWriter, obj);
        }
Exemplo n.º 3
0
        protected override void CoreWriteObject(object obj, string objectName)
        {
            string                 fullFilePath;
            IXmlObject             xmlObject;
            ISerializationStrategy serializationStrategy;

            if ((object)obj == null)
            {
                throw new ArgumentNullException(nameof(obj));
            }

            if ((object)objectName == null)
            {
                throw new ArgumentNullException(nameof(objectName));
            }

            if (SolderFascadeAccessor.DataTypeFascade.IsWhiteSpace(objectName))
            {
                throw new ArgumentOutOfRangeException(nameof(objectName));
            }

            fullFilePath = Path.GetFullPath(Path.Combine(this.BaseDirectoryPath, objectName));
            xmlObject    = obj as IXmlObject;

            // this should support XPE, XML, JSON

            /*
             *      BACKLOG([email protected] / 2015 - 12 - 18):
             *      Refactor this logic that is common between this File and TextWriter Outputs.
             */
            if ((object)xmlObject != null)
            {
                serializationStrategy = new XpeSerializationStrategy(this.Xpe);
            }
            else if ((object)SolderFascadeAccessor.ReflectionFascade.GetOneAttribute <XmlRootAttribute>(obj.GetType()) != null)
            {
                serializationStrategy = new XmlSerializationStrategy();
            }
            else
            {
                serializationStrategy = new JsonSerializationStrategy();
            }

            serializationStrategy.SetObjectToFile(fullFilePath, obj);
        }
Exemplo n.º 4
0
        public void TestXmlSerializationStrategyWithDefaultSettings()
        {
            var strategy = new XmlSerializationStrategy();

            var serializeRequest = new SecretAgent {
                Id = "007", Name = "Bond. James Bond"
            };

            var serializeResponse = strategy.Serialize(serializeRequest);
            var expectedResponse  = "<Root><Name>Bond. James Bond</Name><Id>007</Id><Drinks /></Root>";

            serializeResponse.Equals(expectedResponse).Should().BeTrue();

            var deserializeRequest  = serializeResponse;
            var deserializeResponse = strategy.Deserialize <SecretAgent>(deserializeRequest);

            deserializeResponse.Should().NotBeNull();
            deserializeResponse.Name.Should().Be("Bond. James Bond");
            deserializeResponse.Id.Should().Be("007");
        }
Exemplo n.º 5
0
		protected override void CoreWriteObject(object obj, string objectName)
		{
			IXmlObject xmlObject;
			ITextSerializationStrategy serializationStrategy;

			if ((object)obj == null)
				throw new ArgumentNullException(nameof(obj));

			xmlObject = obj as IXmlObject;

			// this should support XPE, XML, JSON
			/*
				BACKLOG([email protected] / 2015 - 12 - 18):
				Refactor this logic that is common between this File and TextWriter Outputs.
			*/
			if ((object)xmlObject != null)
				serializationStrategy = new XpeSerializationStrategy(this.Xpe);
			else if ((object)SolderLegacyInstanceAccessor.ReflectionFascadeLegacyInstance.GetOneAttribute<XmlRootAttribute>(obj.GetType()) != null)
				serializationStrategy = new XmlSerializationStrategy();
			else
				serializationStrategy = new JsonSerializationStrategy();

			serializationStrategy.SetObjectToWriter(this.CurrentTextWriter, obj);
		}
Exemplo n.º 6
0
        public void TestXmlSerializationStrategyWithArrayOfOneElement()
        {
            var strategy = new XmlSerializationStrategy();

            var serializeRequest = new SecretAgent {
                Id = "007", Name = "Bond. James Bond", Drinks = new string[] { "Martini" }
            };

            var serializeResponse = strategy.Serialize(serializeRequest);

            var arrayAttribute   = "json:Array=\"true\" xmlns:json=\"http://james.newtonking.com/projects/json\"";
            var expectedResponse = $"<Root><Name>Bond. James Bond</Name><Id>007</Id><Drinks {arrayAttribute}>Martini</Drinks></Root>";

            serializeResponse.Equals(expectedResponse).Should().BeTrue();

            var deserializeRequest  = serializeResponse;
            var deserializeResponse = strategy.Deserialize <SecretAgent>(deserializeRequest);

            deserializeResponse.Should().NotBeNull();
            deserializeResponse.Name.Should().Be("Bond. James Bond");
            deserializeResponse.Id.Should().Be("007");
            deserializeResponse.Drinks.Should().HaveCount(1);
            deserializeResponse.Drinks[0].Should().Be("Martini");
        }
Exemplo n.º 7
0
        public Stream XmlSerialize(T t, Stream s)
        {
            ISerializationStrategy <T> strategy = new XmlSerializationStrategy <T>();

            return(strategy.Serialize(s, t));
        }
Exemplo n.º 8
0
        public T XmlDeserialize(Stream stream)
        {
            ISerializationStrategy <T> strategy = new XmlSerializationStrategy <T>();

            return(strategy.Deserialize(stream));
        }
Exemplo n.º 9
0
		protected override void CoreWriteObject(object obj, string objectName)
		{
			string fullFilePath;
			IXmlObject xmlObject;
			ISerializationStrategy serializationStrategy;

			if ((object)obj == null)
				throw new ArgumentNullException(nameof(obj));

			if ((object)objectName == null)
				throw new ArgumentNullException(nameof(objectName));

			if (SolderLegacyInstanceAccessor.DataTypeFascadeLegacyInstance.IsWhiteSpace(objectName))
				throw new ArgumentOutOfRangeException(nameof(objectName));

			fullFilePath = Path.GetFullPath(Path.Combine(this.BaseDirectoryPath, objectName));
			xmlObject = obj as IXmlObject;

			// this should support XPE, XML, JSON
			/*
				BACKLOG([email protected] / 2015 - 12 - 18):
				Refactor this logic that is common between this File and TextWriter Outputs.
			*/
			if ((object)xmlObject != null)
				serializationStrategy = new XpeSerializationStrategy(this.Xpe);
			else if ((object)SolderLegacyInstanceAccessor.ReflectionFascadeLegacyInstance.GetOneAttribute<XmlRootAttribute>(obj.GetType()) != null)
				serializationStrategy = new XmlSerializationStrategy();
			else
				serializationStrategy = new JsonSerializationStrategy();

			serializationStrategy.SetObjectToFile(fullFilePath, obj);
		}