using Model; using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Serialization; using System.Runtime.Serialization.Json; using System.Text; using System.Threading.Tasks; using System.Xml; using System.Xml.Linq; namespace DataPersistence { /// /// Define a serializer to manage JSON files. /// public class DataContractJSON : IDataSerializer { #region Attributes private string _jsonFolderPath; private DataContractJsonSerializerSettings _dataContractJsonSerializerSettings; #endregion #region Constructors /// /// Constructor of the DataContractJSON serializer. /// /// Give the default path where to load and save the data (by default is the current execution dir). /// Give another set of DataContractJson serializer setting to write file public DataContractJSON(string jsonFolderPath = "", DataContractJsonSerializerSettings? dataContractJsonSerializerSettings = null) { _jsonFolderPath = jsonFolderPath; if (dataContractJsonSerializerSettings is null) _dataContractJsonSerializerSettings = new DataContractJsonSerializerSettings() { KnownTypes = new[] { typeof(Recipe), typeof(RecipeType), typeof(Review), typeof(User), typeof(Ingredient), typeof(Quantity) } }; else _dataContractJsonSerializerSettings = dataContractJsonSerializerSettings; } #endregion #region IDataManager implementation public void Export(T obj, string pathToExport) where T : class { var jsonSerializer = new DataContractJsonSerializer(typeof(T), _dataContractJsonSerializerSettings); using (FileStream stream = File.Create(Path.Combine(_jsonFolderPath, pathToExport))) { using (var jsonWriter = JsonReaderWriterFactory.CreateJsonWriter( stream: stream, encoding: System.Text.Encoding.UTF8, ownsStream: false, indent: true)) { jsonSerializer.WriteObject(jsonWriter, obj); } } } public KeyValuePair Import(string pathToImport) where T : class { T? obj; var jsonSerializer = new DataContractJsonSerializer(typeof(T), _dataContractJsonSerializerSettings); using (FileStream stream = File.OpenRead(Path.Combine(_jsonFolderPath, pathToImport))) { obj = jsonSerializer.ReadObject(stream) as T; } if (obj is null) throw new ArgumentNullException("obj"); string typeName = typeof(T).Name; return new KeyValuePair(typeName, obj); } public Dictionary> Load() { Dictionary>? elements = new Dictionary>(); var jsonSerializer = new DataContractJsonSerializer(typeof(Dictionary>), _dataContractJsonSerializerSettings); using (FileStream stream = File.OpenRead(Path.Combine(_jsonFolderPath, "data.json"))) { elements = jsonSerializer.ReadObject(stream) as Dictionary>; } if (elements is null) throw new ArgumentNullException("elements"); return elements; } public void Save(Dictionary> elements) { var jsonSerializer = new DataContractJsonSerializer(typeof(Dictionary>), _dataContractJsonSerializerSettings); using (FileStream stream = File.Create(Path.Combine(_jsonFolderPath, "data.json"))) { using (var jsonWriter = JsonReaderWriterFactory.CreateJsonWriter( stream: stream, encoding: System.Text.Encoding.UTF8, ownsStream: false, indent: true)) { jsonSerializer.WriteObject(jsonWriter, elements); } } } #endregion } }