Ejemplo n.º 1
0
        /// <summary>
        /// To save as new file
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (SaveFileDialog.ShowDialog() == DialogResult.OK)
            {
                CurrentFilePath = SaveFileDialog.FileName;
                List <string> convertedLines = CSVSerialization.ToCSV(SerializedData);

                File.WriteAllLines(CurrentFilePath, convertedLines, Encoding.Default);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// To open file and load data
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (OpenFileDialog.ShowDialog() == DialogResult.OK)
            {
                CurrentFilePath = OpenFileDialog.FileName;
                List <string> UnparsedLines = new List <string>();

                try
                {
                    using (StreamReader fs = new StreamReader(CurrentFilePath, Encoding.Default))
                    {
                        while (!fs.EndOfStream)
                        {
                            UnparsedLines.Add(fs.ReadLine());
                        }
                    }

                    SerializedData = CSVSerialization.Parse(UnparsedLines);

                    // Checking if list if displayed data is needed
                    if (SerializedData.Count <= NumberOfRows.Value)
                    {
                        filterUsed = false;
                        DataGridView.DataSource = new BindingSource(SerializedData, null);
                    }
                    else
                    {
                        for (int i = 0; i < NumberOfRows.Value; i++)
                        {
                            SerializedDataHalved.Add(SerializedData[i]);
                            DataGridView.DataSource = new BindingSource(SerializedDataHalved, null);
                            filterUsed = true;
                        }
                    }
                }
                catch (IOException)
                {
                    MessageBox.Show("File is used by another process", "Error");
                    CurrentFilePath = null;
                    SerializedData  = null;
                }
                catch (ArgumentException ex)
                {
                    MessageBox.Show($"Wrong data format, cant open file\n{ex.Message}", "Error");
                    CurrentFilePath = null;
                    SerializedData  = null;
                }
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Append to existing file
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void appendToToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (AppendFileDialog.ShowDialog() == DialogResult.OK)
            {
                CurrentFilePath = AppendFileDialog.FileName;
                List <string> convertedLines = CSVSerialization.ToCSV(SerializedData);
                FileStream    fs             = new FileStream(CurrentFilePath, FileMode.Append);

                using (StreamWriter sw = new StreamWriter(fs, Encoding.Default))
                {
                    foreach (var item in convertedLines)
                    {
                        sw.WriteLine(item);
                    }
                }
            }
        }
        public void DeserializeATest()
        {
            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 <A> csv = new CSVSerialization <A>("ObiektA.csv", a1);
            A a2 = csv.deserialize();

            Assert.AreEqual(a1.Name, a2.Name);
            Assert.AreEqual(a1.Number, a2.Number);
            Assert.AreEqual(a1.Date, a2.Date);
            Assert.AreEqual(a1.ObjectB.Name, a2.ObjectB.Name);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// To save current file
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dfToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (CurrentFilePath != null)
            {
                List <string> convertedLines = CSVSerialization.ToCSV(SerializedData);
                FileStream    fs             = new FileStream(CurrentFilePath, FileMode.Create);

                using (StreamWriter sw = new StreamWriter(fs, Encoding.Default))
                {
                    foreach (var item in convertedLines)
                    {
                        sw.WriteLine(item);
                    }
                }
            }
            else
            {
                MessageBox.Show("No current file availible, please use \"Save as\" option", "Error");
            }
        }
        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);
        }
Ejemplo n.º 7
0
        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');
        }