This commit is contained in:
Alexander
2023-11-25 17:11:04 +03:00
parent 77c602cd9c
commit 73e6e4d3b1
13 changed files with 328 additions and 23 deletions

View File

@@ -0,0 +1,25 @@
using System.Text.Json;
namespace anbs_cp.Classes;
/// <summary>
/// Класс для сериализации моделей
/// </summary>
public static class Serializer
{
/// <summary>
/// Сериализация данных <paramref name="data"/> в строку.
/// </summary>
/// <typeparam name="T">Тип данных</typeparam>
/// <param name="data">Данные</param>
/// <returns>Сериализованные данные</returns>
public static string Serialize<T>(T data) => JsonSerializer.Serialize(data);
/// <summary>
/// Десериализация данных из json-строки <paramref name="json"/>
/// </summary>
/// <typeparam name="T">Ожидаемый тип данных</typeparam>
/// <param name="json">Сериализованные данные</param>
/// <returns>Данные</returns>
public static T? Deserialize<T>(string json) => JsonSerializer.Deserialize<T>(json);
}

View File

@@ -1,7 +1,5 @@
using System.Globalization;
using Newtonsoft.Json;
namespace anbs_cp.Classes;
/// <summary>
@@ -73,7 +71,7 @@ public static class TypeConverter
/// <typeparam name="T">Тип</typeparam>
/// <param name="value">Значение типа</param>
/// <returns>Значение в <see cref="string"/></returns>
public static string TypeToStr<T> (T value) => JsonConvert.SerializeObject(value);
public static string TypeToStr<T> (T value) => Serializer.Serialize(value);
#endregion
@@ -159,7 +157,7 @@ public static class TypeConverter
/// <param name="defaultValue">Значение по умолчанию</param>
/// <returns>Значение в <see cref="T"/></returns>
public static T StrToType<T>(string value, T defaultValue) =>
JsonConvert.DeserializeObject<T>(value) ?? defaultValue;
Serializer.Deserialize<T>(value) ?? defaultValue;
#endregion
}