This commit is contained in:
2024-01-01 13:29:59 +03:00
parent ce063558c5
commit f4872e1d49
9 changed files with 53 additions and 20 deletions

View 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);
}

View 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);
}

View File

@@ -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
}

View File

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

View File

@@ -86,7 +86,7 @@ public struct TwoDimSize (int width = 0, int height = 0): ISerializable
/// Сериализовать элемент в формат json
/// </summary>
/// <returns>Строка в формате json</returns>
public readonly string Serialize () => Serializer.Serialize(ToString());
public readonly string Serialize () => new SysTextSerializer().Serialize(ToString());
/// <summary>
/// Восстановить элемент из формата json
@@ -95,7 +95,7 @@ public struct TwoDimSize (int width = 0, int height = 0): ISerializable
public void Deserialize (string json)
{
// Десериализую строку
string deserialized = Serializer.Deserialize<string>(json) ?? "0:0";
string deserialized = new SysTextSerializer().Deserialize<string>(json) ?? "0:0";
// Перевожу строку в двумерный размер
TwoDimSize result = Parse(deserialized);

View File

@@ -2,7 +2,7 @@
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Version>2023.1217.0</Version>
<Version>2024.1.1</Version>
<Authors>Александр Бабаев</Authors>
<Product>Набор компонентов ANB Software</Product>
<Description>Библиотека полезных методов языка C#</Description>
@@ -41,6 +41,7 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>
<ItemGroup>