обновление json parse
All checks were successful
Release / pack-and-publish (release) Successful in 45s
All checks were successful
Release / pack-and-publish (release) Successful in 45s
This commit is contained in:
34
YandexMusic.API/Converters/IntToStringConverter.cs
Normal file
34
YandexMusic.API/Converters/IntToStringConverter.cs
Normal file
@@ -0,0 +1,34 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user