diff options
author | Emma [it/its]@Rory& <root@rory.gay> | 2024-05-30 21:39:12 +0200 |
---|---|---|
committer | Emma [it/its]@Rory& <root@rory.gay> | 2024-05-30 21:39:12 +0200 |
commit | 4bdea63982dae9c17b7a5fbda38d505655b8d4b3 (patch) | |
tree | 8ca9c6bad5f9526c5b36d707f08406fc3bbe2848 /LibMatrix/Extensions/JsonConverters.cs | |
parent | Log warning if registering a duplicate type (diff) | |
download | LibMatrix-4bdea63982dae9c17b7a5fbda38d505655b8d4b3.tar.xz |
Diffstat (limited to 'LibMatrix/Extensions/JsonConverters.cs')
-rw-r--r-- | LibMatrix/Extensions/JsonConverters.cs | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/LibMatrix/Extensions/JsonConverters.cs b/LibMatrix/Extensions/JsonConverters.cs new file mode 100644 index 0000000..eed3fb2 --- /dev/null +++ b/LibMatrix/Extensions/JsonConverters.cs @@ -0,0 +1,29 @@ +using System.Globalization; +using System.Text.Json; +using System.Text.Json.Serialization; + +namespace LibMatrix.Extensions; + +public class JsonFloatStringConverter : JsonConverter<float> { + public override float Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) + => float.Parse(reader.GetString()!); + + public override void Write(Utf8JsonWriter writer, float value, JsonSerializerOptions options) + => writer.WriteStringValue(value.ToString(CultureInfo.InvariantCulture)); +} + +public class JsonDoubleStringConverter : JsonConverter<double> { + public override double Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) + => double.Parse(reader.GetString()!); + + public override void Write(Utf8JsonWriter writer, double value, JsonSerializerOptions options) + => writer.WriteStringValue(value.ToString(CultureInfo.InvariantCulture)); +} + +public class JsonDecimalStringConverter : JsonConverter<decimal> { + public override decimal Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) + => decimal.Parse(reader.GetString()!); + + public override void Write(Utf8JsonWriter writer, decimal value, JsonSerializerOptions options) + => writer.WriteStringValue(value.ToString(CultureInfo.InvariantCulture)); +} \ No newline at end of file |