public void SerializeCTest() { A a1 = new A("A", 1.1f, new DateTime(2019, 12, 1), null); B b1 = new B("B", 3.65f, new DateTime(2019, 10, 1), null); C c1 = new C("C", 5.37f, new DateTime(2020, 1, 2), null); a1.ObjectB = b1; b1.ObjectC = c1; c1.ObjectA = a1; CSVSerialization <C> csv = new CSVSerialization <C>("ObiektC.csv", c1); csv.serialize(); string result = File.ReadAllText("ObiektC.csv"); Assert.AreEqual( "SerializationLibrary, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null;SerializationLibrary.C;1;" + "\n" + "System.String|Name|C;System.Single|Number|5.37;System.DateTime|Date|01/02/2020;SerializationLibrary.A|ObjectA|ref2;" + "\n" + "SerializationLibrary, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null;SerializationLibrary.A;2;" + "\n" + "System.String|Name|A;System.Single|Number|1.1;System.DateTime|Date|12/01/2019;SerializationLibrary.B|ObjectB|ref3;" + "\n" + "SerializationLibrary, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null;SerializationLibrary.B;3;" + "\n" + "System.String|Name|B;System.Single|Number|3.65;System.DateTime|Date|10/01/2019;SerializationLibrary.C|ObjectC|ref1;" + "\n", result); }
static void Main(string[] args) { ConsoleKeyInfo key; do { Console.WriteLine("1. Serialize to JSON\n" + "2. Deserialize from JSON\n" + "3. Serialize to CSV\n" + "4. Deserialize from CSV\n" + "0. Exit\n\n"); key = Console.ReadKey(); switch (key.KeyChar) { case '1': { Console.Clear(); A a1 = new A("A", 1.1f, new DateTime(2019, 12, 1), null); B b1 = new B("B", 3.65f, new DateTime(2019, 10, 1), null); C c1 = new C("C", 5.37f, new DateTime(2020, 1, 2), null); a1.ObjectB = b1; b1.ObjectC = c1; c1.ObjectA = a1; JSONSerialization <A> json = new JSONSerialization <A>("ObiektA.json", a1); json.serialize(); Console.WriteLine("Serialization successful\n" + "Press any key to continue"); Console.ReadKey(); Console.Clear(); break; } case '2': { Console.Clear(); A a2 = null; JSONSerialization <A> json = null; try { json = new JSONSerialization <A>("ObiektA.json", a2); a2 = json.deserialize(); } catch (FileNotFoundException e) { Console.WriteLine("Serialize the object first\n" + "Press any key to continue"); Console.ReadKey(); Console.Clear(); break; } Console.WriteLine(a2); Console.WriteLine("\nPress any key to continue"); Console.ReadKey(); Console.Clear(); break; } case '3': { Console.Clear(); A a1 = new A("A", 1.1f, new DateTime(2019, 12, 1), null); B b1 = new B("B", 3.65f, new DateTime(2019, 10, 1), null); C c1 = new C("C", 5.37f, new DateTime(2020, 1, 2), null); a1.ObjectB = b1; b1.ObjectC = c1; c1.ObjectA = a1; CSVSerialization <B> csv = new CSVSerialization <B>("ObiektB.csv", b1); csv.serialize(); Console.WriteLine("Serialization successful\n" + "Press any key to continue"); Console.ReadKey(); Console.Clear(); break; } case '4': { Console.Clear(); B b2 = null; CSVSerialization <B> csv = null; try { csv = new CSVSerialization <B>("ObiektB.csv", b2); b2 = csv.deserialize(); } catch (FileNotFoundException e) { Console.WriteLine("Serialize the object first\n" + "Press any key to continue"); Console.ReadKey(); Console.Clear(); break; } Console.WriteLine(b2); Console.WriteLine("\nPress any key to continue"); Console.ReadKey(); Console.Clear(); break; } case '0': { break; } default: { Console.Clear(); Console.WriteLine("No such option\n" + "Press any key to continue"); Console.ReadKey(); Console.Clear(); break; } } }while (key.KeyChar != '0'); }