67 lines
1.6 KiB
C#
67 lines
1.6 KiB
C#
using System;
|
|
using System.IO;
|
|
using System.Text.Json;
|
|
using GazpromApp.Models;
|
|
|
|
namespace GazpromApp.Utils;
|
|
|
|
public static class DataSerialization
|
|
{
|
|
private static void SaveData(string path, string data)
|
|
{
|
|
if (!File.Exists(path))
|
|
{
|
|
File.WriteAllText(path, data);
|
|
return;
|
|
}
|
|
|
|
var dir = Path.GetDirectoryName(path) ?? "";
|
|
var fileName = Path.GetFileNameWithoutExtension(path);
|
|
var ext = Path.GetExtension(path);
|
|
var index = 1;
|
|
|
|
while (File.Exists(Path.Combine(dir, $"{fileName} ({index}){ext}")))
|
|
index++;
|
|
|
|
File.WriteAllText(Path.Combine(dir, $"{fileName} ({index}){ext}"), data);
|
|
}
|
|
|
|
private static string LoadData(string path)
|
|
{
|
|
var text = File.ReadAllText(path);
|
|
return text;
|
|
}
|
|
|
|
public static void SavePoint(Point point, string path)
|
|
{
|
|
SaveData(path, point.Serialize());
|
|
}
|
|
|
|
public static Point? LoadPoint(string path)
|
|
{
|
|
return Deserialization.Deserialize(LoadData(path));
|
|
}
|
|
}
|
|
|
|
public static class Serialization
|
|
{
|
|
public static string Serialize(this Point point)
|
|
{
|
|
return JsonSerializer.Serialize(point);
|
|
}
|
|
}
|
|
|
|
public static class Deserialization
|
|
{
|
|
public static Point? Deserialize(string point)
|
|
{
|
|
try
|
|
{
|
|
return !string.IsNullOrEmpty(point) ? JsonSerializer.Deserialize<Point>(point) : null;
|
|
}
|
|
catch
|
|
{
|
|
throw new ArgumentException("Ошибка расшифровки json файла");
|
|
}
|
|
}
|
|
} |