Example #1
0
        public void TestInitialize()
        {
            _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;

            _testC = new ClassC("", new DateTime(), null);

            _fileName = "own_results.txt";
            OurFormatter formatter = new OurFormatter();

            File.Delete(_fileName);
            _serializeStream = File.Open(_fileName, FileMode.Create, FileAccess.ReadWrite);
            formatter.Serialize(_serializeStream, _classC);
            _serializeStream.Close();

            _deserializeStream = File.Open(_fileName, FileMode.Open, FileAccess.ReadWrite);
            _testC             = (ClassC)formatter.Deserialize(_deserializeStream);
            _deserializeStream.Close();

            _testA = _testC.A;
            _testB = _testA.B;
        }
Example #2
0
        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;
            }
            }
        }