Esempio n. 1
0
        public override AddQuestionDto Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
        {
            QuestionTypeDiscriminator typeDiscriminator = QuestionConverterHelpers.GetTypeDiscriminator(ref reader);
            AddQuestionDto            question          = typeDiscriminator switch
            {
                QuestionTypeDiscriminator.MultipleChoice =>
                new AddQuestionWithChoiceAnswersDto {
                    ChoiceAnswerType = ChoiceAnswerType.MultipleChoice
                },
                QuestionTypeDiscriminator.SingleChoice =>
                new AddQuestionWithChoiceAnswersDto {
                    ChoiceAnswerType = ChoiceAnswerType.SingleChoice
                },
                QuestionTypeDiscriminator.WithWrittenAnswer => new AddQuestionWithWrittenAnswerDto(),
                _ => throw new JsonException()
            };

            return(HandlePropertiesLoop(question, ref reader, options));
        }
Esempio n. 2
0
        public override QuestionDto Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
        {
            QuestionTypeDiscriminator typeDiscriminator = QuestionConverterHelpers.GetTypeDiscriminator(ref reader);
            QuestionDto question = typeDiscriminator switch
            {
                QuestionTypeDiscriminator.MultipleChoice => new QuestionWithChoiceAnswersDto {
                    ChoiceAnswerType = ChoiceAnswerType.MultipleChoice
                },
                QuestionTypeDiscriminator.SingleChoice => new QuestionWithChoiceAnswersDto {
                    ChoiceAnswerType = ChoiceAnswerType.SingleChoice
                },
                QuestionTypeDiscriminator.WithWrittenAnswer => new QuestionWithWrittenAnswerDto(),
                _ => throw new JsonException()
            };

            while (reader.Read())
            {
                if (reader.TokenType == JsonTokenType.EndObject)
                {
                    return(question);
                }

                if (reader.TokenType != JsonTokenType.PropertyName)
                {
                    throw new JsonException();
                }

                string propertyName = reader.GetString();
                reader.Read();
                switch (propertyName)
                {
                case "choices":
                {
                    if (typeDiscriminator == QuestionTypeDiscriminator.MultipleChoice ||
                        typeDiscriminator == QuestionTypeDiscriminator.SingleChoice)
                    {
                        var choices = JsonSerializer.Deserialize <List <string> >(ref reader, options);
                        ((QuestionWithChoiceAnswersDto)question).Choices = choices;
                    }
                    else
                    {
                        reader.Skip();
                    }
                    break;
                }

                case "question":
                    string questionValue = reader.GetString();
                    question.Question = questionValue;
                    break;

                case "id":
                    int id = reader.GetInt32();
                    question.Id = id;
                    break;

                default:
                    reader.Skip();
                    break;
                }
            }
            throw new JsonException();
        }