add export and import for XML

pull/39/head
Alexandre AGOSTINHO 2 years ago
parent 81c85dfb60
commit fc6165eb4b

@ -11,18 +11,21 @@ Console.WriteLine("Hello, World!\n\n");
// TESTS:
//DataManager dataMgr = new DataManager(new Stubs());
DataManager dataMgr = new DataManager(new Stubs());
//DataManager dataMgr = new DataManager(new DataContractXML(xmlFolderPath: "../../../../DataPersistence/data"));
DataManager dataMgr = new DataManager(new DataContractJSON(jsonFolderPath: "../../../../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");
RecipeCollection rc = new RecipeCollection("All recipes", dataMgr.Data["recipes"].Cast<Recipe>().ToArray());
//dataMgr.Export(rc[2], "C:\\Users\\alex6\\Downloads\\recipe2.xml");
dataMgr.Import<Recipe>("C:\\Users\\alex6\\Downloads\\recipe2.xml");
RecipeCollection rc = new RecipeCollection("All recipes", dataMgr.Data[nameof(Recipe)].Cast<Recipe>().ToArray());
Recipe? ret = SearcherRecipe.ResearchOn(rc);
Console.WriteLine(ret);
//dataMgr.Serializer = new DataContractXML(xmlFolderPath: "../../../../DataPersistence/data");
//dataMgr.Serializer = new DataContractJSON(jsonFolderPath: "../../../../DataPersistence/data");
dataMgr.Save();
// press any key to quit

@ -28,6 +28,16 @@ namespace DataPersistence
_dataContractJsonSerializerSettings = dataContractJsonSerializerSettings;
}
public void Export<T>(T obj, string pathToExport) where T : class
{
throw new NotImplementedException();
}
public KeyValuePair<string, T> Import<T>(string pathToImport) where T : class
{
throw new NotImplementedException();
}
public Dictionary<string, List<object>> Load()
{
Dictionary<string, List<object>>? elements = new Dictionary<string, List<object>>();

@ -6,6 +6,7 @@ using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Linq;
namespace DataPersistence
{
@ -36,9 +37,42 @@ namespace DataPersistence
_dataContractSerializerSettings = dataContractSerializerSettings;
}
public void Export<T>(T obj, string pathToExport)
where T : class
{
bool restore = _dataContractSerializerSettings.PreserveObjectReferences;
_dataContractSerializerSettings.PreserveObjectReferences = false;
var serializer = new DataContractSerializer(typeof(T), _dataContractSerializerSettings);
using (TextWriter textWriter = File.CreateText(Path.Combine(_xmlFolderPath, pathToExport)))
{
using (XmlWriter xmlWriter = XmlWriter.Create(textWriter, _xmlWriterSettings))
{
serializer.WriteObject(xmlWriter, obj);
}
}
_dataContractSerializerSettings.PreserveObjectReferences = restore;
}
public KeyValuePair<string, T> Import<T>(string pathToImport)
where T : class
{
T? obj;
var serializer = new DataContractSerializer(typeof(T), _dataContractSerializerSettings);
using (Stream s = File.OpenRead(Path.Combine(_xmlFolderPath, pathToImport)))
{
obj = serializer.ReadObject(s) as T;
}
if (obj is null)
throw new ArgumentNullException("obj");
string typeName = typeof(T).Name;
return new KeyValuePair<string, T>(typeName, obj);
}
public Dictionary<string, List<object>> Load()
{
Dictionary<string, List<object>>? elements = new Dictionary<string, List<object>>();
Dictionary<string, List<object>>? elements;
var serializer = new DataContractSerializer(typeof(Dictionary<string, List<object>>), _dataContractSerializerSettings);
using (Stream s = File.OpenRead(Path.Combine(_xmlFolderPath, "data.xml")))
{

@ -23,5 +23,16 @@ namespace DataPersistence
public void Save()
=> Serializer.Save(Data);
public void Import<T>(string pathOfTheFile)
where T : class
{
KeyValuePair<string, T> import = Serializer.Import<T>(pathOfTheFile);
Data[import.Key].Add(import.Value);
}
public void Export<T>(T obj, string pathToExport)
where T : class
=> Serializer.Export<T>(obj, pathToExport);
}
}

@ -8,10 +8,13 @@ namespace DataPersistence
{
public interface IDataManager
{
//void Save<T>(List<T> values);
//List<T> Load<T>();
void Save(Dictionary<string, List<object>> elements);
Dictionary<string, List<object>> Load();
public KeyValuePair<string, T> Import<T>(string pathToImport)
where T : class;
public void Export<T>(T obj, string pathToExport)
where T : class;
}
}

@ -14,7 +14,7 @@ namespace DataPersistence
Dictionary<string, List<object>> data = new Dictionary<string, List<object>>
{
{
"recipes",
nameof(Recipe),
new List<object>(new[]
{
new Recipe(
@ -89,7 +89,17 @@ namespace DataPersistence
public void Save(Dictionary<string, List<object>> elements)
{
// Stubs dont' have to save.
throw new NotSupportedException();
}
public void Export<T>(T obj, string pathToExport) where T : class
{
throw new NotSupportedException();
}
public KeyValuePair<string, T> Import<T>(string pathToImport) where T : class
{
throw new NotSupportedException();
}
}
}

@ -1,168 +0,0 @@
[
{
"Key": "recipes",
"Value": [
{
"__type": "recipe:#Model",
"id": 3005,
"preparation-steps": [
{
"description": "Faire cuire.",
"order": 1
},
{
"description": "Manger.",
"order": 2
}
],
"title": "Cookies classiques"
},
{
"__type": "recipe:#Model",
"id": 28377,
"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": 10495,
"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": 32282,
"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": 26518,
"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": 21970,
"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": 12830,
"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"
}
]
}
]

@ -1,10 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<ArrayOfKeyValueOfstringArrayOfanyTypety7Ep6D1 xmlns:i="http://www.w3.org/2001/XMLSchema-instance" z:Id="1" z:Size="1" xmlns:z="http://schemas.microsoft.com/2003/10/Serialization/" xmlns="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
<KeyValueOfstringArrayOfanyTypety7Ep6D1>
<Key z:Id="2">recipes</Key>
<Value z:Id="3" z:Size="7">
<Key z:Id="2">Recipe</Key>
<Value z:Id="3" z:Size="8">
<anyType z:Id="4" xmlns:d4p1="http://schemas.datacontract.org/2004/07/Model" i:type="d4p1:recipe">
<d4p1:id>3005</d4p1:id>
<d4p1:id>9062</d4p1:id>
<d4p1:preparation-steps z:Id="5" z:Size="2">
<d4p1:preparation-step z:Id="6">
<d4p1:description z:Id="7">Faire cuire.</d4p1:description>
@ -18,7 +18,7 @@
<d4p1:title z:Id="10">Cookies classiques</d4p1:title>
</anyType>
<anyType z:Id="11" xmlns:d4p1="http://schemas.datacontract.org/2004/07/Model" i:type="d4p1:recipe">
<d4p1:id>28377</d4p1:id>
<d4p1:id>27627</d4p1:id>
<d4p1:preparation-steps z:Id="12" z:Size="3">
<d4p1:preparation-step z:Id="13">
<d4p1:description z:Id="14">Moulinez la pâte.</d4p1:description>
@ -36,7 +36,7 @@
<d4p1:title z:Id="19">Cookies au chocolat</d4p1:title>
</anyType>
<anyType z:Id="20" xmlns:d4p1="http://schemas.datacontract.org/2004/07/Model" i:type="d4p1:recipe">
<d4p1:id>10495</d4p1:id>
<d4p1:id>8517</d4p1:id>
<d4p1:preparation-steps z:Id="21" z:Size="3">
<d4p1:preparation-step z:Id="22">
<d4p1:description z:Id="23">Achetez les ingrédients.</d4p1:description>
@ -54,7 +54,7 @@
<d4p1:title z:Id="28">Gateau nature</d4p1:title>
</anyType>
<anyType z:Id="29" xmlns:d4p1="http://schemas.datacontract.org/2004/07/Model" i:type="d4p1:recipe">
<d4p1:id>32282</d4p1:id>
<d4p1:id>6161</d4p1:id>
<d4p1:preparation-steps z:Id="30" z:Size="4">
<d4p1:preparation-step z:Id="31">
<d4p1:description z:Id="32">Achetez les légumes.</d4p1:description>
@ -76,7 +76,7 @@
<d4p1:title z:Id="39">Gateau au pommes</d4p1:title>
</anyType>
<anyType z:Id="40" xmlns:d4p1="http://schemas.datacontract.org/2004/07/Model" i:type="d4p1:recipe">
<d4p1:id>26518</d4p1:id>
<d4p1:id>17869</d4p1:id>
<d4p1:preparation-steps z:Id="41" z:Size="5">
<d4p1:preparation-step z:Id="42">
<d4p1:description z:Id="43">Ajouter les oeufs.</d4p1:description>
@ -102,7 +102,7 @@
<d4p1:title z:Id="52">Gateau au chocolat</d4p1:title>
</anyType>
<anyType z:Id="53" xmlns:d4p1="http://schemas.datacontract.org/2004/07/Model" i:type="d4p1:recipe">
<d4p1:id>21970</d4p1:id>
<d4p1:id>4810</d4p1:id>
<d4p1:preparation-steps z:Id="54" z:Size="5">
<d4p1:preparation-step z:Id="55">
<d4p1:description z:Id="56">Faire une cuisson bien sec de la dinde à la poêle</d4p1:description>
@ -128,7 +128,7 @@
<d4p1:title z:Id="65">Dinde au jambon</d4p1:title>
</anyType>
<anyType z:Id="66" xmlns:d4p1="http://schemas.datacontract.org/2004/07/Model" i:type="d4p1:recipe">
<d4p1:id>12830</d4p1:id>
<d4p1:id>13570</d4p1:id>
<d4p1:preparation-steps z:Id="67" z:Size="6">
<d4p1:preparation-step z:Id="68">
<d4p1:description z:Id="69">Trouvez des épices de curry.</d4p1:description>
@ -157,6 +157,28 @@
</d4p1:preparation-steps>
<d4p1:title z:Id="80">Poulet au curry</d4p1:title>
</anyType>
<anyType z:Id="81" xmlns:d4p1="http://schemas.datacontract.org/2004/07/Model" i:type="d4p1:recipe">
<d4p1:id>26918</d4p1:id>
<d4p1:preparation-steps z:Id="82" z:Size="4">
<d4p1:preparation-step z:Id="83">
<d4p1:description z:Id="84">Achetez les ingrédients.</d4p1:description>
<d4p1:order>1</d4p1:order>
</d4p1:preparation-step>
<d4p1:preparation-step z:Id="85">
<d4p1:description z:Id="86">Préparez le matériel. Ustensiles et tout.</d4p1:description>
<d4p1:order>2</d4p1:order>
</d4p1:preparation-step>
<d4p1:preparation-step z:Id="87">
<d4p1:description z:Id="88">Pleurez.</d4p1:description>
<d4p1:order>3</d4p1:order>
</d4p1:preparation-step>
<d4p1:preparation-step z:Id="89">
<d4p1:description z:Id="90">Aprenez de vos echecs.</d4p1:description>
<d4p1:order>4</d4p1:order>
</d4p1:preparation-step>
</d4p1:preparation-steps>
<d4p1:title z:Id="91">Gateau nature v2</d4p1:title>
</anyType>
</Value>
</KeyValueOfstringArrayOfanyTypety7Ep6D1>
</ArrayOfKeyValueOfstringArrayOfanyTypety7Ep6D1>
Loading…
Cancel
Save