using System.IO; using Newtonsoft.Json; using Pilz.GeneralEventArgs; namespace Pilz.Configuration { public static class ConfigurationSerializer { public static event GettingJsonSerializerEventHandler GettingJsonSerializer; public delegate void GettingJsonSerializerEventHandler(object instance, GetValueEventArgs e); private static JsonSerializer GetJsonSerializer(SimpleConfiguration instance) { var args = new GetValueEventArgs(JsonSerializer.CreateDefault()); GettingJsonSerializer?.Invoke(instance, args); return args.Value; } /// /// Writes the given instance to a string and return it. /// /// The configuration instance that should be serialized. /// The content of the configuration instance as string. public static string WriteToString(SimpleConfiguration instance) { var tw = new StringWriter(); GetJsonSerializer(instance).Serialize(tw, instance); string txt = tw.ToString(); tw.Close(); return txt; } /// /// Write the given instance to a given stream. /// /// The configuration instance that should be serialized. /// The stream where the content should be written to. public static void WriteToStream(SimpleConfiguration instance, Stream stream) { var sr = new StreamWriter(stream); sr.Write(WriteToString(instance)); } /// /// Writes the given instance to the given filePath as text file. /// /// The configuration instance that should be serialized. /// The file path where the content should be written to. The file will be created or overwritten. public static void WriteToFile(SimpleConfiguration instance, string filePath) { var fs = new FileStream(filePath, FileMode.Create, FileAccess.ReadWrite); WriteToStream(instance, fs); fs.Close(); } /// /// Reads a configuratin from the given string and returns an instance of it. /// /// The type of the configuration class to instance. /// The content of the configuration as string. /// public static T ReadFromString(string content) where T : SimpleConfiguration { var sr = new StringReader(content); T instance = (T)GetJsonSerializer(null).Deserialize(sr, typeof(T)); sr.Close(); return instance; } /// /// Read a configuration from the given string and put them to the given instance. /// /// The instance to populate with the configuration. /// The content of the configuration as string. public static void ReadFromString(SimpleConfiguration instance, string content) { var sr = new StringReader(content); GetJsonSerializer(null).Populate(sr, content); sr.Close(); } /// /// Reads a configuratin from the given string and returns an instance of it. /// /// The type of the configuration class to instance. /// The stream with the content of the configuration. /// public static T ReadFromStream(Stream stream) where T : SimpleConfiguration { return ReadFromString(GetContentOfStream(stream)); } /// /// Read a configuration from the given string and put them to the given instance. /// /// The instance to populate with the configuration. /// The stream with the content of the configuration. public static void ReadFromStream(SimpleConfiguration instance, Stream stream) { ReadFromString(instance, GetContentOfStream(stream)); } /// /// Reads a configuratin from the given string and returns an instance of it. /// /// The type of the configuration class to instance. /// The path to the file with the content of the configuration. /// public static T ReadFromFile(string filePath) where T : SimpleConfiguration { return ReadFromString(GetContentOfFile(filePath)); } /// /// Read a configuration from the given string and put them to the given instance. /// /// The instance to populate with the configuration. /// The path to the file with the content of the configuration. public static void ReadFromFile(SimpleConfiguration instance, string filePath) { ReadFromString(instance, GetContentOfFile(filePath)); } private static string GetContentOfStream(Stream stream) { var sr = new StreamReader(stream); return sr.ReadToEnd(); } private static string GetContentOfFile(string filePath) { var fs = new FileStream(filePath, FileMode.Open, FileAccess.Read); string content = GetContentOfStream(fs); fs.Close(); return content; } } }