| | 1 | | using System.IO; |
| | 2 | | using System.Xml; |
| | 3 | | using System.Xml.Serialization; |
| | 4 | | using Newtonsoft.Json; |
| | 5 | |
|
| | 6 | | namespace ReFlex.Core.Common.Components |
| | 7 | | { |
| | 8 | |
|
| | 9 | |
|
| | 10 | | /// <summary> |
| | 11 | | /// Helps to serialize or deserialize objects and files. |
| | 12 | | /// </summary> |
| | 13 | | public static class SerializationUtils |
| | 14 | | { |
| | 15 | | /// <summary> |
| | 16 | | /// Serialize an object to json string |
| | 17 | | /// </summary> |
| | 18 | | /// <param name="data"> object to serialize </param> |
| | 19 | | /// <returns> a json-string </returns> |
| 0 | 20 | | public static string SerializeToJson(object data) => JsonConvert.SerializeObject(data); |
| | 21 | |
|
| | 22 | | /// <summary> |
| | 23 | | /// try to deserialize a string from json to object with type T |
| | 24 | | /// </summary> |
| | 25 | | /// <typeparam name="T"></typeparam> |
| | 26 | | /// <param name="json"></param> |
| | 27 | | /// <returns> the deserialized object if the algorithm finished succesfully </returns> |
| | 28 | | public static T DeserializeFromJson<T>(string json) |
| 0 | 29 | | { |
| | 30 | | //try |
| | 31 | | //{ |
| 0 | 32 | | return JsonConvert.DeserializeObject<T>(json); |
| | 33 | | //} |
| | 34 | | // catch (Exception exc) |
| | 35 | | // { |
| | 36 | | // |
| | 37 | | // return default(T); |
| | 38 | | // } |
| 0 | 39 | | } |
| | 40 | |
|
| | 41 | | /// <summary> |
| | 42 | | /// Serializes the object. |
| | 43 | | /// </summary> |
| | 44 | | /// <typeparam name="T"></typeparam> |
| | 45 | | /// <param name="serializableObject">The serializable object.</param> |
| | 46 | | /// <param name="fileStream">The file stream.</param> |
| | 47 | | public static void SerializeObject<T>(T serializableObject, FileStream fileStream) |
| 0 | 48 | | { |
| 0 | 49 | | if (serializableObject == null) return; |
| | 50 | |
|
| 0 | 51 | | var xmlDocument = new XmlDocument(); |
| 0 | 52 | | var serializer = new XmlSerializer(serializableObject.GetType()); |
| 0 | 53 | | using (var memoryStream = new MemoryStream()) |
| 0 | 54 | | { |
| 0 | 55 | | serializer.Serialize(memoryStream, serializableObject); |
| 0 | 56 | | memoryStream.Position = 0; |
| 0 | 57 | | xmlDocument.Load(memoryStream); |
| 0 | 58 | | xmlDocument.Save(fileStream); |
| 0 | 59 | | memoryStream.Close(); |
| 0 | 60 | | } |
| 0 | 61 | | } |
| | 62 | |
|
| | 63 | | /// <summary> |
| | 64 | | /// Deserializes an xml file into an object list |
| | 65 | | /// </summary> |
| | 66 | | /// <typeparam name="T"></typeparam> |
| | 67 | | /// <param name="fileStream"></param> |
| | 68 | | /// <returns></returns> |
| | 69 | | public static T DeserializeObject<T>(FileStream fileStream) |
| 0 | 70 | | { |
| 0 | 71 | | if (fileStream == null) return default(T); |
| | 72 | |
|
| | 73 | | T objectOut; |
| | 74 | |
|
| 0 | 75 | | var xmlDocument = new XmlDocument(); |
| 0 | 76 | | xmlDocument.Load(fileStream); |
| 0 | 77 | | var xmlString = xmlDocument.OuterXml; |
| | 78 | |
|
| 0 | 79 | | using (var read = new StringReader(xmlString)) |
| 0 | 80 | | { |
| 0 | 81 | | var outType = typeof(T); |
| | 82 | |
|
| 0 | 83 | | var serializer = new XmlSerializer(outType); |
| 0 | 84 | | using (XmlReader reader = new XmlTextReader(read)) |
| 0 | 85 | | { |
| 0 | 86 | | objectOut = (T)serializer.Deserialize(reader); |
| 0 | 87 | | reader.Close(); |
| 0 | 88 | | } |
| | 89 | |
|
| 0 | 90 | | read.Close(); |
| 0 | 91 | | } |
| | 92 | |
|
| 0 | 93 | | return objectOut; |
| 0 | 94 | | } |
| | 95 | |
|
| | 96 | | /// <summary> |
| | 97 | | /// Saves data to the file path. |
| | 98 | | /// </summary> |
| | 99 | | /// <param name="data">The data.</param> |
| | 100 | | /// <param name="path">The path.</param> |
| | 101 | | public static void SaveTo(string data, string path = @"c:\temp") |
| 0 | 102 | | { |
| 0 | 103 | | File.SetAttributes(path, FileAttributes.Normal); |
| 0 | 104 | | File.WriteAllText(path, data); |
| 0 | 105 | | } |
| | 106 | |
|
| | 107 | | /// <summary> |
| | 108 | | /// Loads data from the file path. |
| | 109 | | /// </summary> |
| | 110 | | /// <param name="path">The path.</param> |
| | 111 | | /// <returns> |
| | 112 | | /// The data as string. |
| | 113 | | /// </returns> |
| 0 | 114 | | public static string LoadFrom(string path = @"c:\temp") => File.Exists(path) ? File.ReadAllText(path) : ""; |
| | 115 | |
|
| | 116 | | } |
| | 117 | | } |