< Summary - ReFlex - Library

Information
Class: ReFlex.Core.Common.Components.SerializationUtils
Assembly: ReFlex.Core.Common
File(s): D:\a\reflex\reflex\library\src\Core\Common\Components\SerializationUtils.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 40
Coverable lines: 40
Total lines: 117
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 6
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
SerializeToJson(...)100%210%
DeserializeFromJson(...)100%210%
SerializeObject(...)0%620%
DeserializeObject(...)0%620%
SaveTo(...)100%210%
LoadFrom(...)0%620%

File(s)

D:\a\reflex\reflex\library\src\Core\Common\Components\SerializationUtils.cs

#LineLine coverage
 1using System.IO;
 2using System.Xml;
 3using System.Xml.Serialization;
 4using Newtonsoft.Json;
 5
 6namespace 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>
 020        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)
 029        {
 30            //try
 31            //{
 032                return JsonConvert.DeserializeObject<T>(json);
 33            //}
 34            // catch (Exception exc)
 35            // {
 36            //
 37            //     return default(T);
 38            // }
 039        }
 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)
 048        {
 049            if (serializableObject == null) return;
 50
 051            var xmlDocument = new XmlDocument();
 052            var serializer = new XmlSerializer(serializableObject.GetType());
 053            using (var memoryStream = new MemoryStream())
 054            {
 055                serializer.Serialize(memoryStream, serializableObject);
 056                memoryStream.Position = 0;
 057                xmlDocument.Load(memoryStream);
 058                xmlDocument.Save(fileStream);
 059                memoryStream.Close();
 060            }
 061        }
 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)
 070        {
 071            if (fileStream == null) return default(T);
 72
 73            T objectOut;
 74
 075            var xmlDocument = new XmlDocument();
 076            xmlDocument.Load(fileStream);
 077            var xmlString = xmlDocument.OuterXml;
 78
 079            using (var read = new StringReader(xmlString))
 080            {
 081                var outType = typeof(T);
 82
 083                var serializer = new XmlSerializer(outType);
 084                using (XmlReader reader = new XmlTextReader(read))
 085                {
 086                    objectOut = (T)serializer.Deserialize(reader);
 087                    reader.Close();
 088                }
 89
 090                read.Close();
 091            }
 92
 093            return objectOut;
 094        }
 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")
 0102        {
 0103            File.SetAttributes(path, FileAttributes.Normal);
 0104            File.WriteAllText(path, data);
 0105        }
 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>
 0114        public static string LoadFrom(string path = @"c:\temp") => File.Exists(path) ? File.ReadAllText(path) : "";
 115
 116    }
 117}