static void Main(string[] args) { #region variables for main int selection = 0; String command = ""; String DEFAULTCURFILE = "cur"; String DEFAULTLANGFILE = "lang"; String DEFAULTCOUNTRYFILE = "country"; String JSON = ".json"; String XML = ".XML"; Currency cu = new Currency(); Language lang = new Language(); Country country = new Country(); #endregion end of variables for main //******************************** // line 50ish to 365 are the control statments for the menu // the the tests for default value or user entered specific file // 5, 10, 15 are display menus that access toString of the class objects // the type is passed in a run time // *********************************************************************** #region main while loop while (true) { selection = UserSelection(); if (selection == 1) { Console.WriteLine("Enter path of a JSON file or type default"); command = Console.ReadLine(); if (command.Equals("default")) { JsonDseralizer jds = new JsonDseralizer(); jds.setdeserilizer(DEFAULTCURFILE + JSON, cu); } else { JsonDseralizer jds = new JsonDseralizer(); try { jds.setdeserilizer(command, cu); } catch (IOException e) { Console.WriteLine("Invalid file\n "); } } } else if (selection == 2) { Console.WriteLine("Enter path of a XML file or type default"); command = Console.ReadLine(); if (command.Equals("default")) { XMLDseralizer xmlds = new XMLDseralizer(); xmlds.setdeserilizer(DEFAULTCURFILE + XML, cu); } else { XMLDseralizer xmlds = new XMLDseralizer(); try { xmlds.setdeserilizer(command, cu); } catch (IOException e) { Console.WriteLine("Invalid file\n "); } } } else if (selection == 3) // write all good { Console.WriteLine("Enter path of Json file or type default"); command = Console.ReadLine(); if (command.Equals("default")) { JsonSeralizer jsonSer = new JsonSeralizer(); jsonSer.setSerilizer(DEFAULTCURFILE + JSON, cu); } else { JsonSeralizer jsonSer = new JsonSeralizer(); try { jsonSer.setSerilizer(command, cu); } catch (IOException e) { Console.WriteLine("Invalid file name\n"); } } } else if (selection == 4) // xml write cu { Console.WriteLine("Enter path of XML file or type default"); command = Console.ReadLine(); if (command.Equals("default")) { MyXMLSerilizer xmlSer = new MyXMLSerilizer(); xmlSer.setSerilizer(DEFAULTCURFILE + XML, cu); } else { MyXMLSerilizer xmlSer = new MyXMLSerilizer(); try { xmlSer.setSerilizer(command, cu); } catch (IOException e) { Console.WriteLine("Invalid file name\n"); } } } else if (selection == 5) // display { Console.WriteLine(""); Console.WriteLine(cu.ToString() + "\n"); } else if (selection == 6) { Console.WriteLine("Enter path of a JSON file or type default"); command = Console.ReadLine(); if (command.Equals("default")) { JsonDseralizer jds = new JsonDseralizer(); jds.setdeserilizer(DEFAULTLANGFILE + JSON, lang); } else { JsonDseralizer jds = new JsonDseralizer(); try { jds.setdeserilizer(command, lang); } catch (IOException e) { Console.WriteLine("Invalid file "); } } } else if (selection == 7) { Console.WriteLine("Enter path of a XML file or type default"); command = Console.ReadLine(); if (command.Equals("default")) { XMLDseralizer xmlds = new XMLDseralizer(); xmlds.setdeserilizer(DEFAULTLANGFILE + XML, lang); } else { XMLDseralizer xmlds = new XMLDseralizer(); try { xmlds.setdeserilizer(command, lang); } catch (IOException e) { Console.WriteLine("Invalid file "); } } } else if (selection == 8) { Console.WriteLine("Enter path of Json file or type default"); command = Console.ReadLine(); if (command.Equals("default")) { JsonSeralizer jsonSer = new JsonSeralizer(); jsonSer.setSerilizer(DEFAULTLANGFILE + JSON, lang); } else { JsonSeralizer jsonSer = new JsonSeralizer(); try { jsonSer.setSerilizer(command, lang); } catch (IOException e) { Console.WriteLine("Invalid file name\n"); } } } else if (selection == 9) { Console.WriteLine("Enter path of XML file or type default"); command = Console.ReadLine(); if (command.Equals("default")) { MyXMLSerilizer xmlSer = new MyXMLSerilizer(); xmlSer.setSerilizer(DEFAULTLANGFILE + XML, lang); } else { MyXMLSerilizer xmlSer = new MyXMLSerilizer(); try { xmlSer.setSerilizer(command, lang); } catch (IOException e) { Console.WriteLine("Invalid file name\n"); } } } else if (selection == 10) // display { Console.WriteLine(); Console.WriteLine(lang.ToString() + "\n"); } else if (selection == 11) { Console.WriteLine("Enter path of a JSON file or type default"); command = Console.ReadLine(); if (command.Equals("default")) { JsonDseralizer jds = new JsonDseralizer(); jds.setdeserilizer(DEFAULTCOUNTRYFILE + JSON, country); } else { JsonDseralizer jds = new JsonDseralizer(); try { jds.setdeserilizer(command, country); } catch (IOException e) { Console.WriteLine("Invalid file "); } } } else if (selection == 12) { Console.WriteLine("Enter path of XML file or type default"); command = Console.ReadLine(); if (command.Equals("default")) { XMLDseralizer xmlDeSer = new XMLDseralizer(); xmlDeSer.setdeserilizer(DEFAULTCOUNTRYFILE + XML, country); } else { XMLDseralizer xmlDeSer = new XMLDseralizer(); try { xmlDeSer.setdeserilizer(command, country); } catch (IOException e) { Console.WriteLine("Invalid file name\n"); } } } else if (selection == 13) // write json { Console.WriteLine("Enter path of Json file or type default"); command = Console.ReadLine(); if (command.Equals("default")) { JsonSeralizer jsonSer = new JsonSeralizer(); jsonSer.setSerilizer(DEFAULTCOUNTRYFILE + JSON, country); } else { JsonSeralizer jsonSer = new JsonSeralizer(); try { jsonSer.setSerilizer(command, country); } catch (IOException e) { Console.WriteLine("Invalid file name\n"); } } } else if (selection == 14) { Console.WriteLine("Enter path of XML file or type default"); command = Console.ReadLine(); if (command.Equals("default")) { MyXMLSerilizer xmlSer = new MyXMLSerilizer(); xmlSer.setSerilizer(DEFAULTCOUNTRYFILE + XML, country); } else { MyXMLSerilizer xmlSer = new MyXMLSerilizer(); try { xmlSer.setSerilizer(command, country); } catch (IOException e) { Console.WriteLine("Invalid file name\n"); } } } else if (selection == 15) // display { Console.WriteLine(); Console.WriteLine(country.ToString() + "\n"); } #endregion end of main while loop selection = -1; // resets selection command = ""; // resets command // no need for 16 because UserSelection will never pass back // just exits in that other loop } }
static void Main(string[] args) { Currency cu = new Currency(); Language lang = new Language(); Country country = new Country(); /* * Country country = new Country * { * Name = "United States", * Capitial = "Washington DC", * Region = "North America", * Subregion = "blank", * CurrenyP = new Currency * { * Code = "1", * Name = "Dollars", * Symbol = "$", * }, * languageP = new Language * { * Name = "English", * NativeName = "English", * Iso639_1 = "en", * Iso639_2 = "sp", * }, * }; */ String filename = "TestCountry3.json"; JsonSeralizer jw = new JsonSeralizer(); // could have kept it a normal type but that might be confusing jw.setSerilizer(filename, lang); /* * FileStream writer = new FileStream(filename, FileMode.Create, * FileAccess.Write); * * DataContractJsonSerializer ser; * * ser = new DataContractJsonSerializer(typeof(Country)); * * ser.WriteObject(writer, country); * writer.Close(); */ Country country2; String filename2 = filename; FileStream reader = new FileStream(filename2, FileMode.Open, FileAccess.Read); DataContractJsonSerializer inputSerializer; inputSerializer = new DataContractJsonSerializer(typeof(Country)); country2 = (Country)inputSerializer.ReadObject(reader); reader.Close(); Console.WriteLine(country2.Region); }