using System.Text.Json; using System.Text.Json.Serialization; namespace StabilityMatrix.Core.Converters.Json; /// /// Json converter for types that serialize to string by `ToString()` and /// can be created by `Activator.CreateInstance(Type, string)` /// public class StringJsonConverter : JsonConverter { /// public override T? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { if (reader.TokenType != JsonTokenType.String) { throw new JsonException(); } var value = reader.GetString(); if (value is null) { throw new JsonException(); } return (T?) Activator.CreateInstance(typeToConvert, value); } /// public override void Write(Utf8JsonWriter writer, T? value, JsonSerializerOptions options) { writer.WriteStringValue(value?.ToString()); } }