public ClassB(string stringProperty, float floatProperty, int intProperty, bool boolProperty, ClassC cProperty) { StringProperty = stringProperty; FloatProperty = floatProperty; IntProperty = intProperty; BoolProperty = boolProperty; CProperty = cProperty; }
public override bool Equals(object obj) { ClassC inst = (ClassC)obj; return(this.BoolProperty.Equals(inst.BoolProperty) && this.IntProperty.Equals(inst.IntProperty) && this.FloatProperty.Equals(inst.FloatProperty) && this.StringProperty.Equals(inst.StringProperty)); }
private static void InitModel() { _classA = new ClassA("Klasa A", 2.1, null); _classB = new ClassB("Klasa B", 3.7f, null); _classC = new ClassC("Klasa C", new DateTime(2020, 12, 24), null); _classA.B = _classB; _classB.C = _classC; _classC.A = _classA; }
static void Main(string[] args) { Console.WriteLine("Choose option \nown - for own serializer \nxml - for xml serializer"); String option = Console.ReadLine(); switch (option) { case "own": { Console.WriteLine("Choose option \ns - for serialization \nd - for deserialization"); String localOption = Console.ReadLine(); switch (localOption) { case "s": { Console.WriteLine( "Choose option \nA - for class A serialization \nB - for class B serialization \nC - for class C serialization"); String localClassOption = Console.ReadLine(); ClassA classA = new ClassA("message from A class", 56.35345f, 65, false, null); ClassB classB = new ClassB("message from B class", 57.35345f, 66, true, null); ClassC classC = new ClassC("message from C class", 58.35345f, 67, false, null); classA.BProperty = classB; classB.CProperty = classC; classC.AProperty = classA; switch (localClassOption) { case "A": { using (FileStream fs = new FileStream("output.txt", FileMode.Create)) { IFormatter ownSerializer = new Serializer(); ownSerializer.Serialize(fs, classA); } Console.WriteLine("Success"); break; } case "B": { using (FileStream fs = new FileStream("output.txt", FileMode.Create)) { IFormatter ownSerializer = new Serializer(); ownSerializer.Serialize(fs, classB); } Console.WriteLine("Success"); break; } case "C": { using (FileStream fs = new FileStream("output.txt", FileMode.Create)) { IFormatter ownSerializer = new Serializer(); ownSerializer.Serialize(fs, classC); } Console.WriteLine("Success"); break; } } break; } case "d": { Console.WriteLine( "Choose option \nA - for class A deserialization \nB - for class B deserialization \nC - for class C deserialization"); String local_class_option = Console.ReadLine(); switch (local_class_option) { case "A": { ClassA test; using (FileStream fs = new FileStream("output.txt", FileMode.Open)) { IFormatter ownSerializer = new Serializer(); test = ownSerializer.Deserialize(fs) as ClassA; } Console.WriteLine(test.ToString()); Console.WriteLine(test.BProperty.ToString()); Console.WriteLine(test.BProperty.CProperty.ToString()); Console.WriteLine("Success"); break; } case "B": { ClassB test; using (FileStream fs = new FileStream("output.txt", FileMode.Open)) { IFormatter ownSerializer = new Serializer(); test = ownSerializer.Deserialize(fs) as ClassB; } Console.WriteLine(test.ToString()); Console.WriteLine(test.CProperty.ToString()); Console.WriteLine(test.CProperty.AProperty.ToString()); Console.WriteLine("Success"); break; } case "C": { ClassC test; using (FileStream fs = new FileStream("output.txt", FileMode.Open)) { IFormatter ownSerializer = new Serializer(); test = ownSerializer.Deserialize(fs) as ClassC; } Console.WriteLine(test.ToString()); Console.WriteLine(test.AProperty.ToString()); Console.WriteLine(test.AProperty.BProperty.ToString()); Console.WriteLine("Success"); break; } } break; } } break; } case "xml": { Console.WriteLine("Choose option \ns - for serialization \nd - for deserialization"); String local_option = Console.ReadLine(); switch (local_option) { case "s": { Console.WriteLine( "Choose option \nA - for class A serialization \nB - for class B serialization \nC - for class C serialization"); String local_class_option = Console.ReadLine(); ClassA classA = new ClassA("message from A class", 56.35345f, 65, false, null); ClassB classB = new ClassB("message from B class", 57.35345f, 66, true, null); ClassC classC = new ClassC("message from C class", 58.35345f, 67, false, null); classA.BProperty = classB; classB.CProperty = classC; classC.AProperty = classA; switch (local_class_option) { case "A": { XmlSerializer f = new XmlSerializer(); f.Serialize("output.xml", classA); Console.WriteLine("Success"); break; } case "B": { XmlSerializer f = new XmlSerializer(); f.Serialize("output.xml", classB); Console.WriteLine("Success"); break; } case "C": { XmlSerializer f = new XmlSerializer(); f.Serialize("output.xml", classC); Console.WriteLine("Success"); break; } } break; } case "d": { Console.WriteLine( "Choose option \nA - for class A deserialization \nB - for class B deserialization \nC - for class C deserialization"); String local_class_option = Console.ReadLine(); switch (local_class_option) { case "A": { XmlSerializer f = new XmlSerializer(); ClassA test = (ClassA)f.Deserialize("output.xml", typeof(ClassA)); Console.WriteLine(test.ToString()); Console.WriteLine(test.BProperty.ToString()); Console.WriteLine(test.BProperty.CProperty.ToString()); Console.WriteLine("Success"); break; } case "B": { XmlSerializer f = new XmlSerializer(); ClassB test = (ClassB)f.Deserialize("output.xml", typeof(ClassB)); Console.WriteLine(test.ToString()); Console.WriteLine(test.CProperty.ToString()); Console.WriteLine(test.CProperty.AProperty.ToString()); Console.WriteLine("Success"); break; } case "C": { XmlSerializer f = new XmlSerializer(); ClassC test = (ClassC)f.Deserialize("output.xml", typeof(ClassC)); Console.WriteLine(test.ToString()); Console.WriteLine(test.AProperty.ToString()); Console.WriteLine(test.AProperty.BProperty.ToString()); Console.WriteLine("Success"); break; } } break; } } break; } default: break; } }
private static void InitEmptyModel() { _classA = new ClassA(); _classB = new ClassB(); _classC = new ClassC(); }
private static void Main(string[] args) { Console.Clear(); Console.WriteLine("SERIALIZATION TASK 2"); Console.WriteLine("1. Serialize to JSON"); Console.WriteLine("2. Deserialize from JSON"); Console.WriteLine("3. Serialize to TXT"); Console.WriteLine("4. Deserialize from TXT"); Console.WriteLine("5. Exit"); Console.Write(Environment.NewLine + "Select an option: "); switch (Console.ReadLine()) { case "1": { InitModel(); Stream serializeStream = File.Open("example.json", FileMode.Create, FileAccess.ReadWrite); JsonFormatter.Serialize(_classC, serializeStream); serializeStream.Close(); break; } case "2": { InitEmptyModel(); Stream deserializeStream = File.Open("example.json", FileMode.Open, FileAccess.ReadWrite); _classC = JsonFormatter.Deserialize <ClassC>(deserializeStream); deserializeStream.Close(); BindReferences(); DisplayModel(); break; } case "3": { OurFormatter ourFormatter = new OurFormatter(); InitModel(); Stream serializeStream = File.Open("example.txt", FileMode.Create, FileAccess.ReadWrite); ourFormatter.Serialize(serializeStream, _classC); serializeStream.Close(); break; } case "4": { InitEmptyModel(); OurFormatter ourFormatter = new OurFormatter(); Stream deserializeStream = File.Open("example.txt", FileMode.Open, FileAccess.ReadWrite); _classC = (ClassC)ourFormatter.Deserialize(deserializeStream); deserializeStream.Close(); BindReferences(); DisplayModel(); break; } case "5": { break; } } }
static void Main(string[] args) { string choice; do { Console.Clear(); Console.WriteLine("Insert \"xml\" or \"own\"\n(press any key to exit)"); choice = Console.ReadLine(); if (choice == "xml") { #region "XmlSerialization" Console.WriteLine("Insert \"s\" - serialize or \"d\" - deserialize"); string choice2 = Console.ReadLine(); switch (choice2) { case "s": ClassAPrim classAPrim = new ClassAPrim(1.2f, new DateTime(1997, 1, 1, 0, 0, 0), "TestA", null); ClassBPrim classBPrim = new ClassBPrim(2.2f, new DateTime(1997, 2, 1, 0, 0, 0), "testB", null); ClassCPrim classCPrim = new ClassCPrim(3.2f, new DateTime(1997, 3, 1, 0, 0, 0), "testC", null); classAPrim.ClassBProperty = classBPrim; classBPrim.ClassCProperty = classCPrim; classCPrim.ClassAProperty = classAPrim; Console.WriteLine("Insert \"A\" - serialize ClassA, \"B\" - serialize ClassB or \"C\" - serialize ClassC"); choice2 = Console.ReadLine(); switch (choice2) { case "A": XmlSerialization <ClassAPrim> xmlClassA = new XmlSerialization <ClassAPrim>("xml-a.xml", classAPrim); xmlClassA.serialize(); break; case "B": XmlSerialization <ClassBPrim> xmlClassB = new XmlSerialization <ClassBPrim>("xml-b.xml", classBPrim); xmlClassB.serialize(); break; case "C": XmlSerialization <ClassCPrim> xmlClassC = new XmlSerialization <ClassCPrim>("xml-c.xml", classCPrim); xmlClassC.serialize(); break; default: Console.WriteLine("Invalid input!"); break; } break; case "d": Console.WriteLine("Insert \"A\" - deserialize ClassA, \"B\" - deserialize ClassB or \"C\" - deserialize ClassC"); choice2 = Console.ReadLine(); switch (choice2) { case "A": XmlSerialization <ClassAPrim> xmlClassA = new XmlSerialization <ClassAPrim>("xml-a.xml", new ClassAPrim()); ClassAPrim deserializedClassA = xmlClassA.deserialize(); if (deserializedClassA.ClassBProperty.ClassCProperty.ClassAProperty == deserializedClassA) { Console.WriteLine("Deserialization is completed!"); } else { Console.WriteLine("Deserialization is failed!"); } break; case "B": XmlSerialization <ClassBPrim> xmlClassB = new XmlSerialization <ClassBPrim>("xml-b.xml", new ClassBPrim()); ClassBPrim deserializedClassB = xmlClassB.deserialize(); if (deserializedClassB.ClassCProperty.ClassAProperty.ClassBProperty == deserializedClassB) { Console.WriteLine("Deserialization is completed!"); } else { Console.WriteLine("Deserialization is failed!"); } break; case "C": XmlSerialization <ClassCPrim> xmlClassC = new XmlSerialization <ClassCPrim>("xml-c.xml", new ClassCPrim()); ClassCPrim deserializedClassC = xmlClassC.deserialize(); if (deserializedClassC.ClassAProperty.ClassBProperty.ClassCProperty == deserializedClassC) { Console.WriteLine("Deserialization is completed!"); } else { Console.WriteLine("Deserialization is failed!"); } break; default: Console.WriteLine("Invalid input!"); break; } break; default: Console.WriteLine("Invalid input!"); break; } #endregion } else if (choice == "own") { #region "OwnSerialization" Console.WriteLine("Insert \"s\" - serialize or \"d\" - deserialize"); string choice2 = Console.ReadLine(); switch (choice2) { case "s": ClassA classA = new ClassA(1.2f, new DateTime(1997, 1, 1, 0, 0, 0), "TestA", null); ClassB classB = new ClassB(2.2f, new DateTime(1997, 2, 1, 0, 0, 0), "testB", null); ClassC classC = new ClassC(3.2f, new DateTime(1997, 3, 1, 0, 0, 0), "testC", null); classA.ClassBProperty = classB; classB.ClassCProperty = classC; classC.ClassAProperty = classA; Console.WriteLine("Insert \"A\" - serialize ClassA, \"B\" - serialize ClassB or \"C\" - serialize ClassC"); choice2 = Console.ReadLine(); switch (choice2) { case "A": using (FileStream s = new FileStream("own-a.txt", FileMode.Create)) { IFormatter f = new OwnFormatter(); f.Serialize(s, classA); } break; case "B": using (FileStream s = new FileStream("own-b.txt", FileMode.Create)) { IFormatter f = new OwnFormatter(); f.Serialize(s, classB); } break; case "C": using (FileStream s = new FileStream("own-c.txt", FileMode.Create)) { IFormatter f = new OwnFormatter(); f.Serialize(s, classC); } break; default: Console.WriteLine("Invalid input!"); break; } break; case "d": Console.WriteLine("Insert \"A\" - deserialize ClassA, \"B\" - deserialize ClassB or \"C\" - deserialize ClassC"); choice2 = Console.ReadLine(); switch (choice2) { case "A": using (FileStream s = new FileStream("own-a.txt", FileMode.Open)) { IFormatter f = new OwnFormatter(); ClassA testClass = (ClassA)f.Deserialize(s); if (testClass.ClassBProperty.ClassCProperty.ClassAProperty == testClass) { Console.WriteLine("Deserialization is completed!"); Console.WriteLine(testClass); Console.WriteLine(testClass.ClassBProperty); Console.WriteLine(testClass.ClassBProperty.ClassCProperty); Console.WriteLine(testClass.ClassBProperty.ClassCProperty.ClassAProperty); } else { Console.WriteLine("Deserialization is failed!"); } } break; case "B": using (FileStream s = new FileStream("own-b.txt", FileMode.Open)) { IFormatter f = new OwnFormatter(); ClassB testClass = (ClassB)f.Deserialize(s); if (testClass.ClassCProperty.ClassAProperty.ClassBProperty == testClass) { Console.WriteLine("Deserialization is completed!"); Console.WriteLine(testClass); Console.WriteLine(testClass.ClassCProperty); Console.WriteLine(testClass.ClassCProperty.ClassAProperty); Console.WriteLine(testClass.ClassCProperty.ClassAProperty.ClassBProperty); } else { Console.WriteLine("Deserialization is failed!"); } } break; case "C": using (FileStream s = new FileStream("own-c.txt", FileMode.Open)) { IFormatter f = new OwnFormatter(); ClassC testClass = (ClassC)f.Deserialize(s); if (testClass.ClassAProperty.ClassBProperty.ClassCProperty == testClass) { Console.WriteLine("Deserialization is completed!"); Console.WriteLine(testClass); Console.WriteLine(testClass.ClassAProperty); Console.WriteLine(testClass.ClassAProperty.ClassBProperty); Console.WriteLine(testClass.ClassAProperty.ClassBProperty.ClassCProperty); } else { Console.WriteLine("Deserialization is failed!"); } } break; default: Console.WriteLine("Invalid input!"); break; } break; default: Console.WriteLine("Invalid input!"); break; } #endregion } Console.WriteLine("Click any to continue"); Console.ReadLine(); } while (choice == "xml" || choice == "own"); }
static void Main(string[] args) { ClassA classA = new ClassA("classA", 789654, true); ClassB classB = new ClassB("classB", 123456897, 777); ClassC classC = new ClassC("classC", 98765188); classA.ClassB = classB; classA.ClassC = classC; classB.ClassA = classA; classB.ClassC = classC; classC.ClassA = classA; classC.ClassB = classB; Bookshelf bookshelf = new Bookshelf( new List <Book> { new Book("123-456", 9.9), new Book("987-654", 1.1) }, new BookGenres[] { new BookGenres("Drama"), new BookGenres("Action"), new BookGenres("Poetry") } ); CustomFormatter customFormatter; Console.WriteLine("Wciśnij:"); Console.WriteLine("1 - by zapisać stan programu w pliku JSON - ClassA"); Console.WriteLine("2 - by zapisać stan programu w pliku JSON - Bookshelf"); Console.WriteLine("3 - by zapisać stan programu własną metodą - ClassA"); Console.WriteLine("4 - by zapisać stan programu własną metodą - Bookshelf"); Console.WriteLine("5 - by wczytać stan programu z pliku JSON - ClassA"); Console.WriteLine("6 - by wczytać stan programu z pliku JSON - Bookshelf"); Console.WriteLine("7 - by wczytać stan programu własną metodą - ClassA"); Console.WriteLine("8 - by wczytać stan programu własną metodą - Bookshelf"); Console.WriteLine("9 - by wyłączyć program"); string option = "10"; string filePath; while (option != "9") { customFormatter = new CustomFormatter(); option = Console.ReadLine(); switch (option) { case "1": filePath = "..\\..\\..\\..\\TestResults\\jsonFileClassA.json"; JsonSerializer.Serialize(classA, filePath); break; case "2": filePath = "..\\..\\..\\..\\TestResults\\jsonFileBookshelf.json"; JsonSerializer.Serialize(bookshelf, filePath); break; case "3": filePath = "..\\..\\..\\..\\TestResults\\CustomSerializationClassA.txt"; using (Stream stream = new FileStream(filePath, FileMode.Create)) { customFormatter.Serialize(stream, classA); } break; case "4": filePath = "..\\..\\..\\..\\TestResults\\CustomSerializationBookshelf.txt"; using (Stream stream = new FileStream(filePath, FileMode.Create)) { customFormatter.Serialize(stream, bookshelf); } break; case "5": filePath = "..\\..\\..\\..\\TestResults\\jsonFileClassA.json"; ClassA classADeserialized = JsonSerializer.Deserialize <ClassA>(filePath); break; case "6": filePath = "..\\..\\..\\..\\TestResults\\jsonFileBookshelf.json"; Bookshelf bookshelfDeserialized = JsonSerializer.Deserialize <Bookshelf>(filePath); break; case "7": filePath = "..\\..\\..\\..\\TestResults\\CustomSerializationClassA.txt"; using (Stream stream = new FileStream(filePath, FileMode.Open)) { ClassA deserialized = (ClassA)customFormatter.Deserialize(stream); } break; case "8": filePath = "..\\..\\..\\..\\TestResults\\CustomSerializationBookshelf.txt"; using (Stream stream = new FileStream(filePath, FileMode.Open)) { Bookshelf deserialized = (Bookshelf)customFormatter.Deserialize(stream); } break; default: break; } } }