public void SaveSurveys() { SurveyContainer surveys = new SurveyContainer(); surveys.answers1 = answers1; surveys.answers4 = answers4; surveys.answers5 = answers5; surveys.answers6 = answers6; surveys.answers7 = answers7; surveys.answers8 = answers8; string suffix = getSuffix(currentYear); string jpath = "..\\..\\Surveys\\" + suffix + ".json"; var _jsonSettings = new JsonSerializerSettings() { TypeNameHandling = TypeNameHandling.Auto, ConstructorHandling = ConstructorHandling.AllowNonPublicDefaultConstructor, PreserveReferencesHandling = PreserveReferencesHandling.Objects, ObjectCreationHandling = ObjectCreationHandling.Auto }; string jsonString = JsonConvert.SerializeObject(surveys, Formatting.Indented, _jsonSettings); File.WriteAllText(jpath, jsonString); }
public void InitializeSurveys() { string suffix = getSuffix(currentYear); string jpath = "..\\..\\Surveys\\" + suffix + ".json"; string tpath = "..\\..\\Surveys\\" + suffix + "_"; surveyBoard = Program.sb; controller = Program.c; if (File.Exists(jpath)) { var _jsonSettings = new JsonSerializerSettings() { TypeNameHandling = TypeNameHandling.Auto, ConstructorHandling = ConstructorHandling.AllowNonPublicDefaultConstructor, PreserveReferencesHandling = PreserveReferencesHandling.Objects, ObjectCreationHandling = ObjectCreationHandling.Auto }; string json = File.ReadAllText(jpath); SurveyContainer surveys = JsonConvert.DeserializeObject <SurveyContainer>(json, _jsonSettings); answers1 = surveys.answers1; answers4 = surveys.answers4; answers5 = surveys.answers5; answers6 = surveys.answers6; answers7 = surveys.answers7; answers8 = surveys.answers8; } else { int[] files = { 1, 4, 5, 6, 7, 8 }; string[] lines; string[] line; string t; int p; SurveyData surveyData; Question question = new Question(); int answerID = 1; foreach (int file in files) { surveyData = new SurveyData(); lines = System.IO.File.ReadAllLines(tpath + file.ToString() + ".txt"); int count = 0; //for ( count < lines.Length; count++) while (count < lines.Length) { for (int i = 0; i < file + 1; i++) { line = lines[count].Split('~'); t = line[0]; p = int.Parse(line[1]); if (i == 0) { question = new Question(); question.text = t; question.maxPoint = p; } else { question.answers.Add(new Answer(t, p, answerID)); answerID++; } count++; } question.SortAnswers(); surveyData.AddNewQuestion(question); } switch (file) { case 1: answers1 = surveyData; break; case 4: answers4 = surveyData; break; case 5: answers5 = surveyData; break; case 6: answers6 = surveyData; break; case 7: answers7 = surveyData; break; case 8: answers8 = surveyData; break; } } } StartNewGame(); }