コード例 #1
0
        /// <summary>
        /// Builds a new person
        /// </summary>
        private static Xml.Person GenerateXmlPerson()
        {
            Xml.Person person = new Xml.Person();
            person.Id = LastXmlId;

            person.Name   = $"Name{LastXmlId}";
            person.Email  = $"Email{LastXmlId}";
            person.Phones = new Xml.PersonPhoneNumber[3];
            Xml.PersonPhoneNumber xmlPhoneNumber = new Xml.PersonPhoneNumber {
                Number = $"mobile{LastXmlId}", Type = Xml.PersonPhoneNumberType.Mobile
            };
            person.Phones[0] = xmlPhoneNumber;
            xmlPhoneNumber   = new Xml.PersonPhoneNumber {
                Number = $"home{LastXmlId}", Type = Xml.PersonPhoneNumberType.Home
            };
            person.Phones[1] = xmlPhoneNumber;
            xmlPhoneNumber   = new Xml.PersonPhoneNumber {
                Number = $"work{LastXmlId}", Type = Xml.PersonPhoneNumberType.Work
            };
            person.Phones[2] = xmlPhoneNumber;
            LastXmlId++;
            return(person);
        }
コード例 #2
0
        /// <summary>
        /// Builds a person based on user input
        /// </summary>
        private static object[] PromptForAddress(TextReader input, TextWriter output)
        {
            Person person = new Person();

            Xml.Person  xmlPerson  = new Xml.Person();
            Json.Person jsonPerson = new Json.Person();

            output.Write("Enter person ID: ");
            int id = int.Parse(input.ReadLine());

            person.Id     = id;
            xmlPerson.Id  = (uint)id;
            jsonPerson.Id = id;

            output.Write("Enter name: ");
            var name = input.ReadLine();

            person.Name     = name;
            xmlPerson.Name  = name;
            jsonPerson.Name = name;

            output.Write("Enter email address (blank for none): ");
            string email = input.ReadLine();

            if (email.Length > 0)
            {
                person.Email     = email;
                xmlPerson.Email  = email;
                jsonPerson.Email = email;
            }

            while (true)
            {
                output.Write("Enter a phone number (or leave blank to finish): ");
                string number = input.ReadLine();
                if (number.Length == 0)
                {
                    break;
                }

                Person.Types.PhoneNumber phoneNumber = new Person.Types.PhoneNumber {
                    Number = number
                };
                Xml.PersonPhoneNumber xmlPhoneNumber = new Xml.PersonPhoneNumber {
                    Number = number
                };
                Json.PhoneNumber jsonPhoneNumber = new Json.PhoneNumber {
                    Number = number
                };

                output.Write("Is this a mobile, home, or work phone? ");
                String type = input.ReadLine();
                switch (type)
                {
                case "mobile":
                    phoneNumber.Type     = Person.Types.PhoneType.Mobile;
                    xmlPhoneNumber.Type  = Xml.PersonPhoneNumberType.Mobile;
                    jsonPhoneNumber.Type = Json.PhoneType.Mobile;
                    break;

                case "home":
                    phoneNumber.Type     = Person.Types.PhoneType.Home;
                    xmlPhoneNumber.Type  = Xml.PersonPhoneNumberType.Home;
                    jsonPhoneNumber.Type = Json.PhoneType.Home;
                    break;

                case "work":
                    phoneNumber.Type     = Person.Types.PhoneType.Work;
                    xmlPhoneNumber.Type  = Xml.PersonPhoneNumberType.Work;
                    jsonPhoneNumber.Type = Json.PhoneType.Work;
                    break;

                default:
                    output.Write("Unknown phone type. Using default.");
                    break;
                }

                person.Phones.Add(phoneNumber);

                Xml.PersonPhoneNumber[] newValues;

                if (xmlPerson.Phones != null)
                {
                    newValues = new Xml.PersonPhoneNumber[xmlPerson.Phones.Length + 1];
                    Array.Copy(xmlPerson.Phones, newValues, xmlPerson.Phones.Length);
                    newValues[xmlPerson.Phones.Length] = xmlPhoneNumber;
                }
                else
                {
                    newValues    = new Xml.PersonPhoneNumber[1];
                    newValues[0] = xmlPhoneNumber;
                }

                xmlPerson.Phones = newValues;

                if (jsonPerson.Phones == null)
                {
                    jsonPerson.Phones = new System.Collections.ObjectModel.ObservableCollection <Json.PhoneNumber>();
                }

                jsonPerson.Phones.Add(jsonPhoneNumber);
            }

            return(new object[] { person, xmlPerson, jsonPerson });
        }
コード例 #3
0
        /// <summary>
        /// Entry point - loads an existing addressbook or creates a new one,
        /// then writes it back to the file.
        /// </summary>
        public static int Main(string[] args)
        {
            if (args.Length != 3)
            {
                Console.Error.WriteLine("Usage:  AddPerson ADDRESS_BOOK_FILE");
                return(-1);
            }

            AddressBook addressBook;

            Xml.AddressBook  xmlAddressBook;
            Json.AddressBook jsonAddressBook;

            if (File.Exists(args[0]))
            {
                using (Stream file = File.OpenRead(args[0]))
                {
                    addressBook = AddressBook.Parser.ParseFrom(file);
                }
            }
            else
            {
                Console.WriteLine("{0}: File not found. Creating a new file.", args[0]);
                addressBook = new AddressBook();
            }

            if (File.Exists(args[1]))
            {
                using (Stream file = File.OpenRead(args[1]))
                {
                    XmlSerializer xmlSerializer = new XmlSerializer(typeof(Xml.AddressBook));
                    xmlAddressBook = xmlSerializer.Deserialize(file) as Xml.AddressBook;
                }
            }
            else
            {
                Console.WriteLine("{0}: File not found. Creating a new file.", args[1]);
                xmlAddressBook = new Xml.AddressBook();
            }

            if (File.Exists(args[2]))
            {
                using (StreamReader stream = new StreamReader(args[2]))
                {
                    JsonSerializer serializer = new JsonSerializer();
                    JsonTextReader reader     = new JsonTextReader(stream);
                    jsonAddressBook = serializer.Deserialize(reader, typeof(Json.AddressBook)) as Json.AddressBook;
                }
            }
            else
            {
                Console.WriteLine("{0}: File not found. Creating a new file.", args[2]);
                jsonAddressBook = new Json.AddressBook();
            }

            // Add an address.
            var persons = PromptForAddress(Console.In, Console.Out);

            addressBook.People.Add(persons[0] as Person);

            Xml.Person[] newValue;
            if (xmlAddressBook.people != null)
            {
                newValue = new Xml.Person[xmlAddressBook.people.Length + 1];
                Array.Copy(xmlAddressBook.people, newValue, xmlAddressBook.people.Length);
                newValue[xmlAddressBook.people.Length] = persons[1] as Xml.Person;
            }
            else
            {
                newValue    = new Xml.Person[1];
                newValue[0] = persons[1] as Xml.Person;
            }

            xmlAddressBook.people = newValue;

            if (jsonAddressBook.People == null)
            {
                jsonAddressBook.People = new System.Collections.ObjectModel.ObservableCollection <Json.Person>();
            }

            jsonAddressBook.People.Add(persons[2] as Json.Person);

            // Write the new proto address book back to disk.
            using (Stream output = File.OpenWrite(args[0]))
            {
                addressBook.WriteTo(output);
            }

            // Write the new xml address book back to disk.
            using (Stream output = File.OpenWrite(args[1]))
            {
                XmlSerializer xmlSerializer = new XmlSerializer(typeof(Xml.AddressBook));
                xmlSerializer.Serialize(output, xmlAddressBook);
            }

            // Write the new json address book back to disk
            using (StreamWriter stream = new StreamWriter(args[2]))
            {
                JsonSerializer serializer = new JsonSerializer();
                JsonTextWriter writer     = new JsonTextWriter(stream);
                serializer.Serialize(writer, jsonAddressBook, typeof(Json.AddressBook));
            }
            return(0);
        }
コード例 #4
0
        /// <summary>
        /// Entry point - loads an existing addressbook or creates a new one,
        /// then writes it back to the file.
        /// </summary>
        public static int Main(string[] args)
        {
            if (args.Length != 3)
            {
                Console.Error.WriteLine("Usage:  GeneratePersons ADDRESS_BOOK_FILE");
                return(-1);
            }

            AddressBook addressBook;

            Xml.AddressBook  xmlAddressBook;
            Json.AddressBook jsonAddressBook;
            long             protoSave = 0, protoGen = 0, protoCount = 0,
                             xmlSave = 0, xmlGen = 0, xmlCount = 0,
                             jsonSave = 0, jsonGen = 0, jsonCount = 0;

            if (File.Exists(args[0]))
            {
                using (Stream file = File.OpenRead(args[0]))
                {
                    addressBook = AddressBook.Parser.ParseFrom(file);
                }
            }
            else
            {
                Console.WriteLine("{0}: File not found. Creating a new file.", args[0]);
                addressBook = new AddressBook();
            }

            if (File.Exists(args[1]))
            {
                using (Stream file = File.OpenRead(args[1]))
                {
                    XmlSerializer xmlSerializer = new XmlSerializer(typeof(Xml.AddressBook));
                    xmlAddressBook = xmlSerializer.Deserialize(file) as Xml.AddressBook;
                }
            }
            else
            {
                Console.WriteLine("{0}: File not found. Creating a new file.", args[1]);
                xmlAddressBook = new Xml.AddressBook();
            }

            if (File.Exists(args[2]))
            {
                using (StreamReader stream = new StreamReader(args[2]))
                {
                    JsonSerializer serializer = new JsonSerializer();
                    JsonTextReader reader     = new JsonTextReader(stream);
                    jsonAddressBook = serializer.Deserialize(reader, typeof(Json.AddressBook)) as Json.AddressBook;
                }
            }
            else
            {
                Console.WriteLine("{0}: File not found. Creating a new file.", args[2]);
                jsonAddressBook = new Json.AddressBook();
            }

            Console.WriteLine("Enter number of persons: ");
            string persons  = Console.ReadLine();
            int    nPersons = 0;

            while (!Int32.TryParse(persons, out nPersons))
            {
                Console.WriteLine("Enter Numeric Value");
                persons = Console.ReadLine();
            }

            // Add addresses proto.
            watch.Restart();
            for (int i = 0; i < nPersons; ++i)
            {
                addressBook.People.Add(GeneratePerson());
            }
            watch.Stop();
            protoGen   = watch.ElapsedMilliseconds;
            protoCount = addressBook.People.Count;

            // Add addresses Xml.
            watch.Restart();
            Xml.Person[] newValue;
            int          startIndex;

            if (xmlAddressBook.people != null)
            {
                newValue = new Xml.Person[xmlAddressBook.people.Length + nPersons];
                Array.Copy(xmlAddressBook.people, newValue, xmlAddressBook.people.Length);
                startIndex = xmlAddressBook.people.Length;
            }
            else
            {
                newValue   = new Xml.Person[nPersons];
                startIndex = 0;
            }

            for (int i = 0; i < nPersons; ++i)
            {
                newValue[i + startIndex] = GenerateXmlPerson();
            }

            xmlAddressBook.people = newValue;
            watch.Stop();
            xmlGen   = watch.ElapsedMilliseconds;
            xmlCount = xmlAddressBook.people.LongLength;

            // Add addresses json.
            watch.Restart();
            if (jsonAddressBook.People == null)
            {
                jsonAddressBook.People = new System.Collections.ObjectModel.ObservableCollection <Json.Person>();
            }

            for (int i = 0; i < nPersons; ++i)
            {
                jsonAddressBook.People.Add(GenerateJsonPerson());
            }
            watch.Stop();
            jsonGen   = watch.ElapsedMilliseconds;
            jsonCount = addressBook.People.Count;

            // Write the new proto address book back to disk.
            watch.Restart();
            using (Stream output = File.OpenWrite(args[0]))
            {
                addressBook.WriteTo(output);
            }
            watch.Stop();
            protoSave = watch.ElapsedMilliseconds;

            // Write the new xml address book back to disk.
            watch.Restart();
            using (Stream output = File.OpenWrite(args[1]))
            {
                XmlSerializer xmlSerializer = new XmlSerializer(typeof(Xml.AddressBook));
                xmlSerializer.Serialize(output, xmlAddressBook);
            }
            watch.Stop();
            xmlSave = watch.ElapsedMilliseconds;

            // Write the new json address book back to disk.
            watch.Restart();
            using (StreamWriter stream = new StreamWriter(args[2]))
            {
                JsonSerializer serializer = new JsonSerializer();
                JsonTextWriter writer     = new JsonTextWriter(stream);
                serializer.Serialize(writer, jsonAddressBook, typeof(Json.AddressBook));
            }
            watch.Stop();
            jsonSave = watch.ElapsedMilliseconds;

            Console.WriteLine("*****Protocole Buffer*****");
            Console.WriteLine($"Time to generate {nPersons} records (ms): {protoGen}");
            Console.WriteLine($"Total records: {protoCount}");
            Console.WriteLine($"Time to save(ms): {protoSave}");

            Console.WriteLine("*****Xml*****");
            Console.WriteLine($"Time to generate {nPersons} records (ms): {xmlGen}");
            Console.WriteLine($"Total records: {xmlCount}");
            Console.WriteLine($"Time to save(ms): {xmlSave}");

            Console.WriteLine("*****Json*****");
            Console.WriteLine($"Time to generate {nPersons} records (ms): {jsonGen}");
            Console.WriteLine($"Total records: {jsonCount}");
            Console.WriteLine($"Time to save(ms): {jsonSave}");

            return(0);
        }