I'm developing the ASP .Net "Student Testing" Rest API.
I'm using the default System.Text.Json
(de)serializer with the .net v5.0
This API is used for student testing. The student chooses a test from the list and takes it.
There are several types of questions in my API:
- Text question - the student enters the answer into the input field
- Single choice question - the student chooses one of the proposed options
- Multiple choice question - the student chooses multiple options
After the student is confident in his answers, he submits them. Here is an example of the submitted answers in JSON format:
{
"testId": "07450745-0745-0745-0745-074507450745",
"answeredQuestions": [
{
"id": "abcdabcd-abcd-abcd-abcd-abcdabcdabcd",
"type": "text",
"givenAnswer": "Answer that student typed in the input box."
},
{
"id": "88888888-4444-4444-4444-dddddddddddd",
"type": "single-choice",
"selectedAnswerOptionId": "00000000-0000-0000-0000-000000000008"
},
{
"id": "12341234-1234-1234-1234-123412341234",
"type": "multiple-choice",
"selectedAnswerOptionIds": [
"00000000-0000-0000-0000-000000000001",
"00000000-0000-0000-0000-000000000002",
"00000000-0000-0000-0000-000000000004"
]
}
]
}
I have DTO questions implemented in ASP .Net Core API:
public abstract class AnsweredQuestion
{
public Guid Id { get; set; }
public abstract string Type { get; }
}
public class AnsweredTextQuestion : AnsweredQuestion
{
public override string Type => "text";
public string GivenAnswer { get; set; }
}
public class AnsweredSingleChoiceQuestion : AnsweredQuestion
{
public override string Type => "single-choice";
public Guid SelectedAnswerOptionId { get; set; }
}
public class AnsweredMultipleChoiceQuestion : AnsweredQuestion
{
public override string Type => "multiple-choice";
public ICollection<Guid> SelectedAnswerOptionIds { get; set; }
}
But I'm not really sure how to implement the presented test DTO.
If I just do something like ICollection<AnsweredQuestion>
for the JSON answeredQuestions
field, then all data related to the specific question type is lost (e.g. givenAnswer
, selectedAnswerOptionId
or selectedAnswerOptionIds
).
How to implement the submitted test DTO to support different types of questions?
In all my actions JSON deserialization happens automatically, but in that case it can be a problem.
I think the solution is to somehow change the deserialization logic to take into account the value of the type
field.