diff --git a/NUGET_README.md b/NUGET_README.md index 5dc62303..c01e0a21 100644 --- a/NUGET_README.md +++ b/NUGET_README.md @@ -35,13 +35,16 @@ PeyrSharp is divided in multiple packages: - Converters - Internet - Crypt +- XmlHelper +- JsonHelper **PeyrSharp.Env**, methods related to the file system and to the current execution environment. - FileSys - Logger -- Update - System +- Update +- UwpApp **PeyrSharp.Enums**, all enumerations used by PeyrSharp diff --git a/PeyrSharp.Core/Converters/Speeds.cs b/PeyrSharp.Core/Converters/Speeds.cs index 0091d758..6b065c3f 100644 --- a/PeyrSharp.Core/Converters/Speeds.cs +++ b/PeyrSharp.Core/Converters/Speeds.cs @@ -93,7 +93,7 @@ public static class Speeds public static double MachToKilometersPerHour(double n) => // Use the conversion formula: km/h = mach * 1234.8 n * 1234.8; - + /// /// Converts a speed in mach to miles per hour. /// diff --git a/PeyrSharp.Core/JsonHelper.cs b/PeyrSharp.Core/JsonHelper.cs new file mode 100644 index 00000000..10df6091 --- /dev/null +++ b/PeyrSharp.Core/JsonHelper.cs @@ -0,0 +1,68 @@ +/* +MIT License + +Copyright (c) Devyus + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ + +using System.IO; +using System.Text.Json; + +namespace PeyrSharp.Core +{ + /// + /// A helper class for working with JSON files. + /// + public static class JsonHelper + { + /// + /// Saves an object as a JSON file. + /// + /// The type of the object to save. + /// The object to save. + /// The name of the file to save to. + public static void SaveAsJson(T obj, string fileName) + { + // Convert the object to a JSON string + string json = JsonSerializer.Serialize(obj); + + // Write the JSON string to the file + File.WriteAllText(fileName, json); + } + + /// + /// Loads an object from a JSON file. + /// + /// The type of the object to load. + /// The name of the file to load from. + /// The object loaded from the file. + public static T LoadFromJson(string fileName) + { + // Read the JSON string from the file + string json = File.ReadAllText(fileName); + + // Convert the JSON string to an object of type T + T obj = JsonSerializer.Deserialize(json); + + // Return the object + return obj; + } + } +} diff --git a/PeyrSharp.Core/PeyrSharp.Core.csproj b/PeyrSharp.Core/PeyrSharp.Core.csproj index ec3f160d..5950f56a 100644 --- a/PeyrSharp.Core/PeyrSharp.Core.csproj +++ b/PeyrSharp.Core/PeyrSharp.Core.csproj @@ -4,7 +4,7 @@ net5.0;net6.0;net7.0 True PeyrSharp.Core - 1.7.0.2307 + 1.8.0.2308 Devyus Core methods and features of PeyrSharp. © 2023 @@ -16,11 +16,10 @@ NUGET_README.md MIT True - - Added Range() method in Stats (#117) -- Added Variance() method in Stats (#118) -- Added StandardDeviation() method in Stats (#119) -- Improved exceptions - + - Added Save() method in XmlHelper (#126) +- Added Load() method in XmlHelper (#127) +- Added Save() method in JsonHelper (#128) +- Added Load() method in JsonHelper (#129) @@ -35,8 +34,8 @@ - - + + \ No newline at end of file diff --git a/PeyrSharp.Core/XmlHelper.cs b/PeyrSharp.Core/XmlHelper.cs new file mode 100644 index 00000000..1bee1bf2 --- /dev/null +++ b/PeyrSharp.Core/XmlHelper.cs @@ -0,0 +1,113 @@ +/* +MIT License + +Copyright (c) Devyus + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ + +using System; +using System.IO; +using System.Xml.Serialization; + +namespace PeyrSharp.Core +{ + /// + /// Provides helper methods for loading and saving objects to/from XML files. + /// + public static class XmlHelper + { + /// + /// Loads an object of type T from an XML file at the specified path. + /// If the file does not exist, a new instance of type T will be created + /// and saved to the file using the SaveToXml method before returning it. + /// + /// The type of object to be loaded. + /// The path of the XML file to load or create. + /// + /// The loaded object of type T if the file exists and can be deserialized successfully, + /// or a new instance of type T if the file does not exist and can be created and saved successfully. + /// If an exception occurs during loading or saving, the method returns . + /// + public static T? LoadFromXml(string path) where T : new() + { + try + { + // Check if the file exists + if (File.Exists(path)) + { + // Create an XmlSerializer for type T + XmlSerializer serializer = new(typeof(T)); + + // Create a StreamReader to read from the file + using StreamReader reader = new(path); + + // Deserialize the object from the file + T obj = (T)serializer.Deserialize(reader); + + // Return the object + return obj; + } + else + { + Directory.CreateDirectory(Path.GetDirectoryName(path)); + // Create a new instance of type T + T obj = new(); + + // Save the object to the file using the SaveToXml method + SaveToXml(obj, path); + + // Return the object + return obj; + } + } + catch (Exception ex) + { + // Handle the exception + Console.WriteLine("An error occurred: " + ex.Message); + + // Return null if an exception is thrown + return default; + } + } + + /// + /// Saves the specified object of type T to an XML file at the specified path. + /// + /// The type of object to be saved. + /// The object to be saved. + /// The path of the XML file to save the object. + /// if the object is successfully serialized and saved to the file; otherwise, . + public static bool SaveToXml(T obj, string path) + { + // Create an XmlSerializer for type T + XmlSerializer serializer = new(typeof(T)); + + // Create a StreamWriter to write to the file + StreamWriter writer = new(path); + + // Serialize the object to the file + serializer.Serialize(writer, obj); + + writer.Dispose(); + // Return true if no exception is thrown + return true; + } + } +} diff --git a/PeyrSharp.Enums/PeyrSharp.Enums.csproj b/PeyrSharp.Enums/PeyrSharp.Enums.csproj index fb98be1b..f338582c 100644 --- a/PeyrSharp.Enums/PeyrSharp.Enums.csproj +++ b/PeyrSharp.Enums/PeyrSharp.Enums.csproj @@ -11,7 +11,7 @@ https://peyrsharp.leocorporation.dev/ https://github.com/DevyusCode/PeyrSharp enums;c-sharp;dotnet;vb;peyrsharp;leo corp - 1.7.0.2307 + 1.8.0.2308 True logo.png NUGET_README.md diff --git a/PeyrSharp.Env/PeyrSharp.Env.csproj b/PeyrSharp.Env/PeyrSharp.Env.csproj index d80c86ea..5d05c66c 100644 --- a/PeyrSharp.Env/PeyrSharp.Env.csproj +++ b/PeyrSharp.Env/PeyrSharp.Env.csproj @@ -4,7 +4,7 @@ net5.0;net6.0;net7.0 True PeyrSharp.Env - 1.7.0.2307 + 1.8.0.2308 Devyus Environment-related methods of PeyrSharp. © 2023 @@ -16,8 +16,7 @@ NUGET_README.md MIT True - - Added UwpApp record (#120) -- Added the possibility to get UWP apps asynchronously (#120) + @@ -32,6 +31,6 @@ - + \ No newline at end of file diff --git a/PeyrSharp.Exceptions/PeyrSharp.Exceptions.csproj b/PeyrSharp.Exceptions/PeyrSharp.Exceptions.csproj index ca6bf084..5885b1ee 100644 --- a/PeyrSharp.Exceptions/PeyrSharp.Exceptions.csproj +++ b/PeyrSharp.Exceptions/PeyrSharp.Exceptions.csproj @@ -6,7 +6,7 @@ enable True PeyrSharp.Exceptions - 1.7.0.2307 + 1.8.0.2308 Devyus Exceptions of PeyrSharp. © 2023 diff --git a/PeyrSharp.Extensions/PeyrSharp.Extensions.csproj b/PeyrSharp.Extensions/PeyrSharp.Extensions.csproj index 31bca901..e432aee4 100644 --- a/PeyrSharp.Extensions/PeyrSharp.Extensions.csproj +++ b/PeyrSharp.Extensions/PeyrSharp.Extensions.csproj @@ -4,7 +4,7 @@ net5.0;net6.0;net7.0 True PeyrSharp.Extensions - 1.7.0.2307 + 1.8.0.2308 Devyus Extensions methods of PeyrSharp. © 2023 @@ -31,7 +31,7 @@ - + \ No newline at end of file diff --git a/PeyrSharp.UiHelpers/PeyrSharp.UiHelpers.csproj b/PeyrSharp.UiHelpers/PeyrSharp.UiHelpers.csproj index 1dc4315d..51a5d486 100644 --- a/PeyrSharp.UiHelpers/PeyrSharp.UiHelpers.csproj +++ b/PeyrSharp.UiHelpers/PeyrSharp.UiHelpers.csproj @@ -8,7 +8,7 @@ true True PeyrSharp.UiHelpers - 1.7.0.2307 + 1.8.0.2308 Devyus Useful helpers for Windows Forms and Windows Presentation Framework. © 2023 @@ -35,7 +35,7 @@ - + \ No newline at end of file diff --git a/PeyrSharp/PeyrSharp.cs b/PeyrSharp/PeyrSharp.cs index 669bca9d..77fef707 100644 --- a/PeyrSharp/PeyrSharp.cs +++ b/PeyrSharp/PeyrSharp.cs @@ -32,6 +32,6 @@ public static class PeyrSharp /// /// The current version of PeyrSharp. /// - public static string Version => "1.7.0.2307"; + public static string Version => "1.8.0.2308"; } } diff --git a/PeyrSharp/PeyrSharp.csproj b/PeyrSharp/PeyrSharp.csproj index 05ab5dc1..2f65e1fd 100644 --- a/PeyrSharp/PeyrSharp.csproj +++ b/PeyrSharp/PeyrSharp.csproj @@ -4,7 +4,7 @@ net5.0;net6.0;net5.0-windows;net6.0-windows;net7.0;net7.0-windows True PeyrSharp - 1.7.0.2307 + 1.8.0.2308 Devyus © 2023 A C# library designed to make developers' job easier. @@ -17,7 +17,10 @@ NUGET_README.md MIT True - - Fixed IsAvailableAsync() crashes when no internet connection is available (#112) + - Added Save() method in XmlHelper (#126) +- Added Load() method in XmlHelper (#127) +- Added Save() method in JsonHelper (#128) +- Added Load() method in JsonHelper (#129) @@ -32,12 +35,12 @@ - - - - - - + + + + + + \ No newline at end of file diff --git a/README.md b/README.md index 8ee488ee..5e56fcc7 100644 --- a/README.md +++ b/README.md @@ -54,6 +54,8 @@ PeyrSharp is divided in multiple packages: - Converters - Internet - Crypt +- XmlHelper +- JsonHelper @@ -64,8 +66,9 @@ PeyrSharp is divided in multiple packages: - FileSys - Logger -- Update - System +- Update +- UwpApp