static void Main(string[] args)
        {
            if (!Directory.Exists(directoryPath))
            {
                Directory.CreateDirectory(directoryPath);
            }

            #region Our Serialize / Deserialize methods

            // Poor man's Serialize / Deserialize
            Student bob = new Student()
            {
                FirstName  = "Bob",
                LastName   = "Bobsky",
                Age        = 33,
                IsPartTime = false
            };

            User myUser = new User()
            {
                Username = "******",
                Password = "******"
            };

            string bobString = SerializerDeserializer.SerializeStudent(bob);
            //Console.WriteLine(bobString);
            ReaderWriter.WriteFile(filePath, bobString);

            string  jsonStudent         = ReaderWriter.ReadFile(filePath);
            Student deserializedStudent = SerializerDeserializer.DeserializeStudent(jsonStudent);
            Console.WriteLine(deserializedStudent.FirstName);
            #endregion

            #region Newtonsoft JSON serialize / deserialize
            Console.WriteLine("----------- Newtonsoft JSON ------------");
            string bobSerializedNewtonsoft = JsonConvert.SerializeObject(bob);
            string userSerializeNewtonsoft = JsonConvert.SerializeObject(myUser);
            Console.WriteLine(bobSerializedNewtonsoft);
            Console.WriteLine(userSerializeNewtonsoft);

            Student deserializedBobNewtonsoft = JsonConvert.DeserializeObject <Student>(bobSerializedNewtonsoft);
            Console.WriteLine(deserializedBobNewtonsoft.LastName);
            User deserializedUserNewtonsoft = JsonConvert.DeserializeObject <User>(userSerializeNewtonsoft);
            Console.WriteLine(deserializedUserNewtonsoft.Password);
            #endregion
            Console.ReadLine();
        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            #region Our own serializer
            Student bob = new Student()
            {
                FirstName  = "Bob",
                LastName   = "Bobsky",
                Age        = 40,
                IsPartTime = false
            };
            Console.WriteLine("Serializing Bob:");
            string bobSerialized = OurJsonSerializer.SerializeStudent(bob);
            Console.WriteLine("Serialization complete!");
            Console.WriteLine(bobSerialized);
            Console.ReadLine();
            Console.WriteLine("Deserializing Bob:");
            Student bobDeserialized = OurJsonSerializer.DeserializeStudent(bobSerialized);
            Console.WriteLine("Deserialization complete!");
            Console.WriteLine(bobDeserialized.FirstName);
            Console.ReadLine();
            #endregion
            #region Newtonsoft.JSON
            // How to install:
            // 1. Right click on the project -> Manage NuGet Packages
            // 2. Click on the Browse tab
            // 3. Find the package that you want to install ( In our case Newtonsoft.JSON )
            // 4. Click the Install button on the right
            // 5. Confirm by clicking Ok
            // 6. ENJOY your library

            // Serialize with Newtonsoft.JSON
            // Newtonsoft.JSON works by name matching
            // If the names do not match it will throw an error
            // You can configure the names in the class it self if you want to custom map some names to others
            // For this configuration check out the Newtonsoft.JSON documentation which is added as a link on the github repo on the README
            string bobSerializedWithNewtonsoft = JsonConvert.SerializeObject(bob);
            Console.WriteLine("Bob is serialized with Newtonsoft.JSON");
            Console.WriteLine(bobSerializedWithNewtonsoft);
            Console.ReadLine();
            Student bobDeserializedWithNewtonsoftJson = JsonConvert.DeserializeObject <Student>(bobSerializedWithNewtonsoft);
            Console.WriteLine("Bob is deserialized with Newtonsoft.JSON");
            Console.WriteLine(bobDeserializedWithNewtonsoftJson.FirstName);
            Console.ReadLine();

            #endregion
        }
        static void Main(string[] args)
        {
            if (!Directory.Exists(directoryPath))
            {
                Directory.CreateDirectory(directoryPath);
            }


            Student viktor = new Student()
            {
                FirstName  = "Viktor",
                LastName   = "Jakovlev",
                Age        = 31,
                IsPartTime = false
            };


            // manual serialization and deserialization
            string viktorString = Serializer.SerializeStudent(viktor);

            ReaderWriter.WriteFile(filePath, viktorString);
            string jsonStudent = ReaderWriter.ReadFile(filePath);

            Student deserializedStudent = Serializer.DeserializeStudent(jsonStudent);

            Console.WriteLine(deserializedStudent.FirstName);

            // using Newtonsoft.Json nugat package for serialization and deserialization

            string viktorStringNewtonsoft = JsonConvert.SerializeObject(viktor);

            User newUser = new User()
            {
                Username = "******",
                Password = 123
            };

            string newUserStringNewtonsoft = JsonConvert.SerializeObject(newUser);
            //string newUserString = Serializer.SerializeStudent(newUser); will not work

            Student deserializedStudentNewtonsoft = JsonConvert.DeserializeObject <Student>(viktorStringNewtonsoft);

            Console.WriteLine(deserializedStudentNewtonsoft.FirstName);
        }
Exemplo n.º 4
0
        static void Main(string[] args)
        {
            if (!Directory.Exists(directoryPath))
            {
                Directory.CreateDirectory(directoryPath);
            }

            Student student = new Student()
            {
                FirstName  = "Trajan",
                LastName   = "Stevkovski",
                Age        = 33,
                IsPartTime = true
            };

            // serialize and write in file
            string studentString = serializer.SerializeStudent(student);

            Console.WriteLine(studentString);
            readerWriter.WriteFile(filePath, studentString);

            // read json from file and deserialize
            string  jsonContent = readerWriter.ReadFile(filePath);
            Student student1    = serializer.DeserializeStudent(jsonContent);

            Console.WriteLine($"{student1.FirstName} {student1.Age}");

            Console.ReadLine();

            string studentSerializedWithNewtonsoft = JsonConvert.SerializeObject(student);

            Console.WriteLine(studentSerializedWithNewtonsoft);

            Student studentDeserializedWithNewtonsoft =
                JsonConvert.DeserializeObject <Student>(studentSerializedWithNewtonsoft);

            Console.WriteLine($"{studentDeserializedWithNewtonsoft.FirstName} {studentDeserializedWithNewtonsoft.Age}");

            Console.ReadLine();
        }