-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathIntJsonConverter.cs
More file actions
30 lines (26 loc) · 919 Bytes
/
Copy pathIntJsonConverter.cs
File metadata and controls
30 lines (26 loc) · 919 Bytes
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
using System;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace IPData.Http.Serializer
{
internal class IntJsonConverter : JsonConverter<int>
{
public override int Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType == JsonTokenType.String)
{
var stringValue = reader.GetString();
if (int.TryParse(stringValue, out var value))
{
return value;
}
throw new JsonException($"Unable to convert \"{stringValue}\" to System.Int32.");
}
return reader.GetInt32();
}
public override void Write(Utf8JsonWriter writer, int value, JsonSerializerOptions options)
{
writer.WriteNumberValue(value);
}
}
}