mirror of
https://github.com/barelyprofessional/KfChatDotNet.git
synced 2026-05-02 04:22:04 -04:00
Big update. Replaced Newtonsoft with System.Text.Json where possible, removed Spectre, tried to suppress the pile of compiler warnings I get on the GUI project, and tried to correct an issue where sometimes the session token retrieved is not usable.
This commit is contained in:
37
KickWsClient/Converters/StringOrObjectConverter.cs
Normal file
37
KickWsClient/Converters/StringOrObjectConverter.cs
Normal file
@@ -0,0 +1,37 @@
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace KickWsClient.Converters;
|
||||
|
||||
// Literal mess, but it is AI generated so what do you expect. Couldn't be bothered writing this myself.
|
||||
public class StringOrObjectConverter<T> : JsonConverter<T>
|
||||
{
|
||||
public override bool CanConvert(Type typeToConvert)
|
||||
{
|
||||
return typeof(T).IsAssignableFrom(typeToConvert) || (typeof(T) == typeof(string) && typeToConvert == typeof(object));
|
||||
}
|
||||
|
||||
public override T Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
||||
{
|
||||
if (reader.TokenType == JsonTokenType.String)
|
||||
{
|
||||
return (T)(object)reader.GetString()!;
|
||||
}
|
||||
else
|
||||
{
|
||||
return JsonSerializer.Deserialize<T>(ref reader, options)!;
|
||||
}
|
||||
}
|
||||
|
||||
public override void Write(Utf8JsonWriter writer, T value, JsonSerializerOptions options)
|
||||
{
|
||||
if (value is string)
|
||||
{
|
||||
writer.WriteStringValue((string)(object)value);
|
||||
}
|
||||
else
|
||||
{
|
||||
JsonSerializer.Serialize(writer, value, options);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user