From 21d527a2cecd16bb3563b3c11867b404dce485ef Mon Sep 17 00:00:00 2001 From: Alexandre Agostinho Date: Sun, 14 May 2023 20:55:02 +0200 Subject: [PATCH] add export and import for JSON --- MCTG/ConsoleApp/Program.cs | 9 +- MCTG/DataPersistence/DataContractJSON.cs | 33 +++- MCTG/DataPersistence/data/data.json | 191 +++++++++++++++++++++++ 3 files changed, 225 insertions(+), 8 deletions(-) create mode 100644 MCTG/DataPersistence/data/data.json diff --git a/MCTG/ConsoleApp/Program.cs b/MCTG/ConsoleApp/Program.cs index b0eb3f4..190fe4b 100644 --- a/MCTG/ConsoleApp/Program.cs +++ b/MCTG/ConsoleApp/Program.cs @@ -15,11 +15,12 @@ DataManager dataMgr = new DataManager(new Stubs()); //DataManager dataMgr = new DataManager(new DataContractXML(xmlFolderPath: "../../../../DataPersistence/data")); //DataManager dataMgr = new DataManager(new DataContractJSON(jsonFolderPath: "../../../../DataPersistence/data")); -dataMgr.Serializer = new DataContractXML(xmlFolderPath: "../../../../DataPersistence/data"); -//dataMgr.Serializer = new DataContractJSON(jsonFolderPath: "../../../../DataPersistence/data"); +//dataMgr.Serializer = new DataContractXML(xmlFolderPath: "../../../../DataPersistence/data"); +dataMgr.Serializer = new DataContractJSON(jsonFolderPath: "../../../../DataPersistence/data"); -//dataMgr.Export(rc[2], "C:\\Users\\alex6\\Downloads\\recipe2.xml"); -dataMgr.Import("C:\\Users\\alex6\\Downloads\\recipe2.xml"); + +//dataMgr.Export(rc[2], "C:\\Users\\alex6\\Downloads\\recipe2.json"); +dataMgr.Import("C:\\Users\\alex6\\Downloads\\recipe2.json"); RecipeCollection rc = new RecipeCollection("All recipes", dataMgr.Data[nameof(Recipe)].Cast().ToArray()); diff --git a/MCTG/DataPersistence/DataContractJSON.cs b/MCTG/DataPersistence/DataContractJSON.cs index ef4eab8..c250a2e 100644 --- a/MCTG/DataPersistence/DataContractJSON.cs +++ b/MCTG/DataPersistence/DataContractJSON.cs @@ -7,6 +7,7 @@ using System.Runtime.Serialization.Json; using System.Text; using System.Threading.Tasks; using System.Xml; +using System.Xml.Linq; namespace DataPersistence { @@ -28,14 +29,38 @@ namespace DataPersistence _dataContractJsonSerializerSettings = dataContractJsonSerializerSettings; } - public void Export(T obj, string pathToExport) where T : class + public void Export(T obj, string pathToExport) + where T : class { - throw new NotImplementedException(); + 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 + public KeyValuePair Import(string pathToImport) + where T : class { - throw new NotImplementedException(); + 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() diff --git a/MCTG/DataPersistence/data/data.json b/MCTG/DataPersistence/data/data.json new file mode 100644 index 0000000..a3f4c7a --- /dev/null +++ b/MCTG/DataPersistence/data/data.json @@ -0,0 +1,191 @@ +[ + { + "Key": "Recipe", + "Value": [ + { + "__type": "recipe:#Model", + "id": 8877, + "preparation-steps": [ + { + "description": "Faire cuire.", + "order": 1 + }, + { + "description": "Manger.", + "order": 2 + } + ], + "title": "Cookies classiques" + }, + { + "__type": "recipe:#Model", + "id": 20950, + "preparation-steps": [ + { + "description": "Moulinez la pâte.", + "order": 1 + }, + { + "description": "Faire cuire pendant une bonne heure.", + "order": 2 + }, + { + "description": "Sortir du four et mettre dans un plat.", + "order": 3 + } + ], + "title": "Cookies au chocolat" + }, + { + "__type": "recipe:#Model", + "id": 23979, + "preparation-steps": [ + { + "description": "Achetez les ingrédients.", + "order": 1 + }, + { + "description": "Préparez le matériel. Ustensiles et tout.", + "order": 2 + }, + { + "description": "Pleurez.", + "order": 3 + } + ], + "title": "Gateau nature" + }, + { + "__type": "recipe:#Model", + "id": 17989, + "preparation-steps": [ + { + "description": "Achetez les légumes.", + "order": 1 + }, + { + "description": "Préparez le plat. Ustensiles et préchauffez le four.", + "order": 2 + }, + { + "description": "Coupez les pommes en morceaux et disposez-les sur le plat.", + "order": 3 + }, + { + "description": "Mettez enfin le plat au four, puis une fois cuit, dégustez !", + "order": 4 + } + ], + "title": "Gateau au pommes" + }, + { + "__type": "recipe:#Model", + "id": 28864, + "preparation-steps": [ + { + "description": "Ajouter les oeufs.", + "order": 1 + }, + { + "description": "Ajouter la farine.", + "order": 2 + }, + { + "description": "Ajouter 100g de chocolat fondu.", + "order": 3 + }, + { + "description": "Mélanger le tout.", + "order": 4 + }, + { + "description": "Faire cuire 45h au four traditionnel.", + "order": 5 + } + ], + "title": "Gateau au chocolat" + }, + { + "__type": "recipe:#Model", + "id": 30406, + "preparation-steps": [ + { + "description": "Faire une cuisson bien sec de la dinde à la poêle", + "order": 1 + }, + { + "description": "Mettre la dinde au frigo.", + "order": 2 + }, + { + "description": "Mettre le jambon dans le micro-onde.", + "order": 3 + }, + { + "description": "Faire chauffer 3min.", + "order": 4 + }, + { + "description": "Présentez sur un plat la dinde et le jambon : Miam !", + "order": 5 + } + ], + "title": "Dinde au jambon" + }, + { + "__type": "recipe:#Model", + "id": 17097, + "preparation-steps": [ + { + "description": "Trouvez des épices de curry.", + "order": 1 + }, + { + "description": "Trouvez maintenant du poulet.", + "order": 2 + }, + { + "description": "Coupez la tête du poulet et posez-la dans un plat.", + "order": 3 + }, + { + "description": "Parsemez d'épices curry la tête de la poule.", + "order": 4 + }, + { + "description": "Mettre le tout au four traditionnel 30min.", + "order": 5 + }, + { + "description": "Dégustez en famille !", + "order": 6 + } + ], + "title": "Poulet au curry" + }, + { + "__type": "recipe:#Model", + "id": 23542, + "preparation-steps": [ + { + "description": "Achetez les ingrédients.", + "order": 1 + }, + { + "description": "Préparez le matériel. Ustensiles et tout.", + "order": 2 + }, + { + "description": "Pleurez.", + "order": 3 + }, + { + "description": "Ce n'est pourtant pas une fatalité !", + "order": 4 + } + ], + "title": "Gateau nature v3" + } + ] + } +] \ No newline at end of file