-
Notifications
You must be signed in to change notification settings - Fork 0
/
GenderConverter.cs
37 lines (34 loc) · 1.15 KB
/
GenderConverter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json.Serialization;
using System.Text.Json;
using System.Threading.Tasks;
namespace TTSPlay.HT
{
public class GenderConverter : JsonConverter<Gender>
{
public override Gender Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType == JsonTokenType.String)
{
string genderStr = reader.GetString();
if (genderStr.Equals("male", StringComparison.OrdinalIgnoreCase))
{
return Gender.Male;
}
else if (genderStr.Equals("female", StringComparison.OrdinalIgnoreCase))
{
return Gender.Female;
}
}
throw new JsonException("Unable to parse Gender value.");
}
public override void Write(Utf8JsonWriter writer, Gender value, JsonSerializerOptions options)
{
string genderStr = value.ToString().ToLower();
writer.WriteStringValue(genderStr);
}
}
}