Files
nadekobot/src/NadekoBot/_common/JsonConverters/NumberToStringConverter.cs
Kwoth ab93380d7c 5.1
2024-06-13 18:54:21 +00:00

30 lines
1.1 KiB
C#

using System.Globalization;
using System.Text.Json;
using System.Text.Json.Serialization;
public class NumberToStringConverter : JsonConverter<object>
{
public override bool CanConvert(Type typeToConvert)
=> typeof(string) == typeToConvert;
public override object Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
switch (reader.TokenType)
{
case JsonTokenType.Number:
return reader.TryGetInt64(out var l)
? l.ToString()
: reader.GetDouble().ToString(CultureInfo.InvariantCulture);
case JsonTokenType.String:
return reader.GetString() ?? string.Empty;
default:
{
using var document = JsonDocument.ParseValue(ref reader);
return document.RootElement.Clone().ToString();
}
}
}
public override void Write(Utf8JsonWriter writer, object value, JsonSerializerOptions options)
=> writer.WriteStringValue(value.ToString());
}