private AddQuestionDto HandlePropertiesLoop(AddQuestionDto question, ref Utf8JsonReader reader, JsonSerializerOptions options)
        {
            while (reader.Read())
            {
                if (reader.TokenType == JsonTokenType.EndObject)
                {
                    return(question);
                }

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

                string propertyName;
                propertyName = reader.GetString();
                reader.Read();
                var propertyExists = QuestionConverterHelpers.TryHandleProperty(question, propertyName, ref reader, options);
                if (!propertyExists)
                {
                    reader.Skip();
                }
            }
            throw new JsonException();
        }
 private static int GetTypeDiscriminatorValue(QuestionDto dto)
 {
     if (dto is QuestionWithWrittenAnswerDto)
     {
         return((int)QuestionTypeDiscriminator.WithWrittenAnswer);
     }
     else if (dto is QuestionWithChoiceAnswersDto dtoChoice)
     {
         return(QuestionConverterHelpers.GetTypeDiscriminatorValue(dtoChoice.ChoiceAnswerType));
     }
     else
     {
         throw new ArgumentException();
     }
 }
        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));
        }
        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();
        }
 public override void Write(Utf8JsonWriter writer, AddQuestionDto value, JsonSerializerOptions options)
 {
     QuestionConverterHelpers.Write(writer, value, options);
 }