/// <summary> /// Read json from file if it exists, otherwise create a new json /// </summary> private void ReadJson() { if (JsonFileExists()) { //Open file using (StreamReader reader = new StreamReader(PathWithFilename())) { string text = reader.ReadToEnd(); _json = JsonConvert.DeserializeObject <FormResultSerializable>(text); } } }
/// <summary> /// Creates a new json object /// </summary> private void InitializeJson() { _json = new FormResultSerializable { FormId = _formId, NumberOfResults = 0, TestResults = new List <int>(), Questions = new QuestionSerializable[_form.Questions.Count()] }; int questionCounter = 0; foreach (QuestionItem formQuestion in _form.Questions) { _json.Questions[questionCounter] = new QuestionSerializable { QuestionId = formQuestion.Id.ToString(), Type = formQuestion.DocumentTypeAlias, Answers = new AnswerSerializable[formQuestion.Children.Count()] }; int answerCounter = 0; foreach (AnswerItem answer in formQuestion.Children.As <AnswerItem>()) { bool isCorrect = false; if (answer.DocumentTypeAlias == "correctAnswer") { isCorrect = true; } else if (answer.DocumentTypeAlias == "wrongAnswer") { isCorrect = false; } _json.Questions[questionCounter].Answers[answerCounter] = new AnswerSerializable { TimesClicked = 0, IsCorrect = isCorrect, AnswerId = answer.Id.ToString() }; answerCounter++; } questionCounter++; } }