|
|
|
@ -1,12 +1,99 @@
|
|
|
|
|
using System;
|
|
|
|
|
using Biblioteque_de_Class;
|
|
|
|
|
using System.Runtime.Serialization;
|
|
|
|
|
using System.Runtime.Serialization.Json;
|
|
|
|
|
using System.Xml;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Text.Json;
|
|
|
|
|
|
|
|
|
|
namespace Notus_Persistance
|
|
|
|
|
{
|
|
|
|
|
internal class ToJSON
|
|
|
|
|
public static class ToJSON
|
|
|
|
|
{
|
|
|
|
|
private const string DatabaseDataFilePath = "data.json";
|
|
|
|
|
private const string UserDataFilePath = "userdata.json";
|
|
|
|
|
private static DataContractJsonSerializer DatabasejsonSerializer = new DataContractJsonSerializer(typeof(Database));
|
|
|
|
|
private static DataContractJsonSerializer UserjsonSerializer = new DataContractJsonSerializer(typeof(User));
|
|
|
|
|
public static void SaveDatabaseData(Database database)
|
|
|
|
|
{
|
|
|
|
|
using (FileStream fileStream = File.Create(DatabaseDataFilePath))
|
|
|
|
|
{
|
|
|
|
|
using (var writer = JsonReaderWriterFactory.CreateJsonWriter(
|
|
|
|
|
fileStream,
|
|
|
|
|
System.Text.Encoding.UTF8,
|
|
|
|
|
false,
|
|
|
|
|
true))//<- this boolean says that we sant indentation
|
|
|
|
|
{
|
|
|
|
|
DatabasejsonSerializer.WriteObject(writer, database);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static Database LoadDatabaseData()
|
|
|
|
|
{
|
|
|
|
|
if (File.Exists(DatabaseDataFilePath))
|
|
|
|
|
{
|
|
|
|
|
Database database1;
|
|
|
|
|
using (FileStream fileStream = File.OpenRead(DatabaseDataFilePath))
|
|
|
|
|
{
|
|
|
|
|
Database? database = (Database?)DatabasejsonSerializer.ReadObject(fileStream);
|
|
|
|
|
if (database == null)
|
|
|
|
|
{
|
|
|
|
|
throw new Exception("Failed to load database. The loaded object is null.");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return database1 = database;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
throw new Exception("No data file found.");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void SaveUserData(User user)
|
|
|
|
|
{
|
|
|
|
|
using (FileStream fileStream = File.Create(UserDataFilePath))
|
|
|
|
|
{
|
|
|
|
|
using (var writer = JsonReaderWriterFactory.CreateJsonWriter(
|
|
|
|
|
fileStream,
|
|
|
|
|
System.Text.Encoding.UTF8,
|
|
|
|
|
false,
|
|
|
|
|
true))//<- this boolean says that we sant indentation
|
|
|
|
|
{
|
|
|
|
|
UserjsonSerializer.WriteObject(writer, user);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static User LoadUserData()
|
|
|
|
|
{
|
|
|
|
|
if (File.Exists(UserDataFilePath))
|
|
|
|
|
{
|
|
|
|
|
User user1;
|
|
|
|
|
using (FileStream fileStream = File.OpenRead(UserDataFilePath))
|
|
|
|
|
{
|
|
|
|
|
User? user = (User?)UserjsonSerializer.ReadObject(fileStream);
|
|
|
|
|
if (user == null)
|
|
|
|
|
{
|
|
|
|
|
throw new Exception("Failed to load database. The loaded object is null.");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return user1 = user;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
throw new Exception("No userfile find");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|