// Copyright (C) Stichting Deltares 2018. All rights reserved. // // This file is part of the application DAM - Clients Library. // // DAM - UI is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program. If not, see . // // All names, logos, and references to "Deltares" are registered trademarks of // Stichting Deltares and remain full property of Stichting Deltares at all times. // All rights reserved. // // using System; using System.IO; using System.Runtime.Serialization; using System.Xml.Serialization; namespace Deltares.Standard { /// /// Types that implement this interface has a seperate data object /// that needs to be serialized instead of the implementer itself /// /// /// The type implementing this interface act as an adapter without subclassing (Decorator pattern). /// It can have a complete different data contract interface and can delegate calls to the members /// of the object being serialized /// /// public interface ISerializableObject { /// /// Should return the data object to serialize /// /// T GetDataObject(); /// /// Sets the data object which needs to be serialized later /// Data properties in the implementer should implemenent setters and getters on this object /// /// void SetDataObject(T obj); } /// /// A common exception which should be trown when validating the schema and mandatory fields are not set. /// [Serializable] public class NotSchemaCompliantException : Exception { public NotSchemaCompliantException() { } public NotSchemaCompliantException(string message) : base(message) { } public NotSchemaCompliantException(string message, Exception inner) : base(message, inner) { } protected NotSchemaCompliantException( SerializationInfo info, StreamingContext context) : base(info, context) { } } /// /// Defines a contract for the Serializer. /// public interface INamespaceProvider { /// /// Defines a method which should return the target namespaces /// /// The qualified name XmlSerializerNamespaces GetNamespaces(); } public static class Serialization { /// /// Serializes the target to file /// /// The target type that implements ISchemaSerializer /// The target instance /// The name of the file to serialze to public static void SerializeToFile(T target, string fileName) { XmlSerializerNamespaces namespaces = null; var ns = target as INamespaceProvider; if (ns != null) namespaces = ns.GetNamespaces(); var xml = new System.Xml.Serialization.XmlSerializer(typeof (T)); using (var fs = new FileStream(fileName, FileMode.Create)) { if (namespaces != null) xml.Serialize(fs, target, namespaces); else xml.Serialize(fs, target); } } /// /// Serializes the target to string /// /// The target type /// The target instance /// the target serialized as a xml string public static string SerializeToString(T target) { string result = ""; XmlSerializerNamespaces namespaces = null; var ns = target as INamespaceProvider; if (ns != null) namespaces = ns.GetNamespaces(); var xml = new System.Xml.Serialization.XmlSerializer(typeof (T)); using (var sw = new StringWriter()) { if (namespaces != null) xml.Serialize(sw, target, namespaces); else xml.Serialize(sw, target); result = sw.ToString(); } return result; } /// /// Deserializes the target object class from file /// /// The target type that implements ISchemaSerializer /// The name of the file to deserialize from /// The target object graph public static T DeserializeFromFile(string fileName) where T : class { T result = default(T); var xml = new System.Xml.Serialization.XmlSerializer(typeof (T)); using (var fs = new FileStream(fileName, FileMode.Open)) { result = xml.Deserialize(fs) as T; } return result; } /// /// Deserializes the target object class from file /// /// The target type that implements ISchemaSerializer /// The name of the file to deserialize from /// The target object graph public static string DeserializeFromFileToString(string fileName) where T : class { T result = default(T); var xml = new System.Xml.Serialization.XmlSerializer(typeof (T)); using (var fs = new FileStream(fileName, FileMode.Open)) { result = xml.Deserialize(fs) as T; } return SerializeToString(result); } /// /// Deserializes the target object from string /// /// /// /// public static T DeserializeFromString(string xmlData) where T : class { T result = default(T); var xml = new System.Xml.Serialization.XmlSerializer(typeof (T)); using (var sr = new StringReader(xmlData)) { result = xml.Deserialize(sr) as T; } return result; } } }