20240101
This commit is contained in:
15
anbs_cp/Classes/NewtonsoftJsonSerializer.cs
Normal file
15
anbs_cp/Classes/NewtonsoftJsonSerializer.cs
Normal file
@@ -0,0 +1,15 @@
|
||||
using anbs_cp.Interfaces;
|
||||
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace anbs_cp.Classes;
|
||||
|
||||
/// <inheritdoc />
|
||||
public class NewtonsoftJsonSerializer: ISerializer
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public string Serialize<T> (T data) => JsonConvert.SerializeObject(data);
|
||||
|
||||
/// <inheritdoc />
|
||||
public T? Deserialize<T> (string json) => JsonConvert.DeserializeObject<T>(json);
|
||||
}
|
@@ -1,25 +0,0 @@
|
||||
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);
|
||||
}
|
15
anbs_cp/Classes/SysTextSerializer.cs
Normal file
15
anbs_cp/Classes/SysTextSerializer.cs
Normal file
@@ -0,0 +1,15 @@
|
||||
using System.Text.Json;
|
||||
|
||||
using anbs_cp.Interfaces;
|
||||
|
||||
namespace anbs_cp.Classes;
|
||||
|
||||
/// <inheritdoc />
|
||||
public class SysTextSerializer: ISerializer
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public string Serialize<T> (T data) => JsonSerializer.Serialize(data);
|
||||
|
||||
/// <inheritdoc />
|
||||
public T? Deserialize<T> (string json) => JsonSerializer.Deserialize<T>(json);
|
||||
}
|
@@ -71,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) => Serializer.Serialize(value);
|
||||
public static string TypeToStr<T> (T value) => new SysTextSerializer().Serialize(value);
|
||||
|
||||
#endregion
|
||||
|
||||
@@ -157,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) =>
|
||||
Serializer.Deserialize<T>(value) ?? defaultValue;
|
||||
new SysTextSerializer().Deserialize<T>(value) ?? defaultValue;
|
||||
|
||||
#endregion
|
||||
}
|
Reference in New Issue
Block a user