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); }
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); }
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"); }
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); }
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"); }
public Stream XmlSerialize(T t, Stream s) { ISerializationStrategy <T> strategy = new XmlSerializationStrategy <T>(); return(strategy.Serialize(s, t)); }
public T XmlDeserialize(Stream stream) { ISerializationStrategy <T> strategy = new XmlSerializationStrategy <T>(); return(strategy.Deserialize(stream)); }
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); }