[CHANGE] Reworked parsers/converters. Decipher operation from script do not work!
This commit is contained in:
39
Manager.YouTube/Util/Converters/NumericJsonConverter.cs
Normal file
39
Manager.YouTube/Util/Converters/NumericJsonConverter.cs
Normal file
@@ -0,0 +1,39 @@
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace Manager.YouTube.Util.Converters;
|
||||
|
||||
public class NumericJsonConverter<T> : JsonConverter<T> where T : struct, IConvertible
|
||||
{
|
||||
public override T Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (reader.TokenType == JsonTokenType.Number)
|
||||
{
|
||||
// Direct numeric value
|
||||
return (T)Convert.ChangeType(reader.GetDouble(), typeof(T));
|
||||
}
|
||||
|
||||
if (reader.TokenType == JsonTokenType.String)
|
||||
{
|
||||
var str = reader.GetString();
|
||||
if (string.IsNullOrWhiteSpace(str))
|
||||
throw new JsonException("Empty string cannot be converted to a number.");
|
||||
|
||||
return (T)Convert.ChangeType(str, typeof(T));
|
||||
}
|
||||
|
||||
throw new JsonException($"Unexpected token {reader.TokenType} for type {typeof(T)}.");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw new JsonException($"Error converting value to {typeof(T)}.", ex);
|
||||
}
|
||||
}
|
||||
|
||||
public override void Write(Utf8JsonWriter writer, T value, JsonSerializerOptions options)
|
||||
{
|
||||
writer.WriteNumberValue(Convert.ToDouble(value));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user