forked from nventive/UnoApplicationTemplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStringJsonConverter.cs
35 lines (30 loc) · 951 Bytes
/
StringJsonConverter.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
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace ApplicationTemplate;
/// <summary>
/// This converter reads non-string tokens as strings.
/// </summary>
public class StringJsonConverter : JsonConverter<string>
{
public override string Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
switch (reader.TokenType)
{
case JsonTokenType.Number:
var stringValue = reader.GetInt32();
return stringValue.ToString(CultureInfo.InvariantCulture);
case JsonTokenType.String:
return reader.GetString();
default:
throw new JsonException($"The token type {reader.TokenType} is not supported by {nameof(StringJsonConverter)}.");
}
}
public override void Write(Utf8JsonWriter writer, string value, JsonSerializerOptions options)
{
writer.WriteStringValue(value);
}
}