/// Copyright (c) 2021 Iiro Iivanainen, Harri Linna, Jere Pakkanen, Riikka Vilavaara
///
/// Permission is hereby granted, free of charge, to any person obtaining
/// a copy of this software and associated documentation files (the
/// "Software"), to deal in the Software without restriction, including
/// without limitation the rights to use, copy, modify, merge, publish,
/// distribute, sublicense, and/or sell copies of the Software, and to
/// permit persons to whom the Software is furnished to do so, subject to
/// the following conditions:
///
/// The above copyright notice and this permission notice shall be included
/// in all copies or substantial portions of the Software.
///
/// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
/// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
/// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
/// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
/// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
/// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
/// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
///
/// @created 07.03.2021
/// @version 24.04.2021, JSON
/// @version 25.04.2021, Standard library Vector3
using System;
using System.IO;
using System.Text.Json;
using System.Threading.Tasks;
namespace CoreLibrary
{
///
/// Exception class for image reading
///
public class SaveException : Exception
{
public SaveException(string message) : base(message)
{
}
}
///
/// Exception class for stack writing
///
public class LoadException : Exception
{
public LoadException(string message) : base(message)
{
}
}
/// Operations for reading text files, especially CSV files.
public static class FileManager
{
///
/// Synchronous non generic deserializer. Deserializes sample from json
/// file.
///
/// Path to JSON file
/// Sample made from the JSON file
/// Thrown when failed to write stack
public static Sample Dezerialize(string filepath)
{
try
{
FileStream filestream = File.OpenRead(filepath);
if (!File.Exists(filepath)) return new Sample();
string contents;
var sr = new StreamReader(filestream);
contents = sr.ReadToEnd();
filestream.Close();
sr.Close();
return JsonSerializer.Deserialize(contents, options);
}
catch (IOException e)
{
throw new SaveException("Failed to save sample. Problems with IO operations: "
+ e.Message);
}
catch (JsonException e)
{
throw new SaveException("Failed to save sample. Problems with Json serialization: "
+ e.Message);
}
}
///
/// Synchronous non generic serialization of sample. Writes
/// JSON string representing the sample into a file.
///
/// Sample to serialize into JSON
/// Filepath to the written JSON file
/// Thrown when failed to write stack
public static void SerializeSync(Sample samp, string filepath)
{
try
{
string info = JsonSerializer.Serialize(samp, options);
StreamWriter writer = new(filepath);
writer.Write(info);
writer.Close();
}
catch(IOException e)
{
throw new SaveException("Failed to load sample. Problems with IO operations: "
+ e.Message);
}
}
///
/// JSON object contains public properties as well.
///
private static readonly JsonSerializerOptions options
= new() { IncludeFields = true,
Converters = {new Vector3JSONConverter()}
};
}
}