PluralKit/Myriad/Serialization/JsonStringConverter.cs

20 lines
658 B
C#
Raw Normal View History

2020-12-22 12:15:26 +00:00
using System.Text.Json;
using System.Text.Json.Serialization;
namespace Myriad.Serialization;
public class JsonStringConverter: JsonConverter<object>
2020-12-22 12:15:26 +00:00
{
public override object? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
2020-12-22 12:15:26 +00:00
{
var str = JsonSerializer.Deserialize<string>(ref reader);
var inner = JsonSerializer.Deserialize(str!, typeToConvert, options);
return inner;
}
2020-12-22 12:15:26 +00:00
public override void Write(Utf8JsonWriter writer, object value, JsonSerializerOptions options)
{
var inner = JsonSerializer.Serialize(value, options);
writer.WriteStringValue(inner);
2020-12-22 12:15:26 +00:00
}
}