Files
YandexMusic/YandexMusic.API/Converters/IntToStringConverter.cs
FrigaT 21a0c5abe6
All checks were successful
Release / pack-and-publish (release) Successful in 45s
обновление json parse
2026-04-13 13:55:23 +03:00

34 lines
1.1 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System.Text.Json;
using System.Text.Json.Serialization;
namespace YandexMusic.API.Converters;
public class IntToStringConverter : JsonConverter<string>
{
public override string Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType == JsonTokenType.Number)
{
// Пытаемся извлечь число как int или long
if (reader.TryGetInt32(out int intValue))
return intValue.ToString();
if (reader.TryGetInt64(out long longValue))
return longValue.ToString();
}
else if (reader.TokenType == JsonTokenType.String)
{
return reader.GetString();
}
else if (reader.TokenType == JsonTokenType.Null)
{
return null;
}
throw new JsonException($"Не удалось преобразовать {reader.TokenType} в строку.");
}
public override void Write(Utf8JsonWriter writer, string value, JsonSerializerOptions options)
{
writer.WriteStringValue(value);
}
}