| | | 1 | | #region Usings |
| | | 2 | | using System.Text.Json; |
| | | 3 | | using System.Text.Json.Serialization; |
| | | 4 | | #endregion |
| | | 5 | | |
| | | 6 | | namespace ZhEaIsNsAaBn.Utilities; |
| | | 7 | | |
| | | 8 | | /// <summary>How serious an <see cref="Error"/> is. Compared by <see cref="Value"/>.</summary> |
| | | 9 | | /// <remarks> |
| | | 10 | | /// JSON: a plain string, e.g. <c>"High"</c> — not <c>{"Value":"High"}</c> (the default for a |
| | | 11 | | /// single-property record). <see cref="Unknown"/> writes as <c>"Unknown"</c> and reads back from |
| | | 12 | | /// <c>"Unknown"</c> or <c>""</c>; its in-memory <see cref="Value"/> stays an empty string. |
| | | 13 | | /// </remarks> |
| | | 14 | | [ JsonConverter( typeof( SeverityJsonConverter ) ) ] |
| | | 15 | | public sealed record Severity |
| | | 16 | | { |
| | | 17 | | /// <summary>Serious; needs attention.</summary> |
| | 3 | 18 | | public static readonly Severity High = new( nameof( High ) ); |
| | | 19 | | |
| | | 20 | | /// <summary>Minor.</summary> |
| | 3 | 21 | | public static readonly Severity Low = new( nameof( Low ) ); |
| | | 22 | | |
| | | 23 | | /// <summary>Normal; the default for new errors.</summary> |
| | 3 | 24 | | public static readonly Severity Medium = new( nameof( Medium ) ); |
| | | 25 | | |
| | | 26 | | /// <summary>Not set. <see cref="Value"/> is an empty string.</summary> |
| | 3 | 27 | | public static readonly Severity Unknown = new( string.Empty ); |
| | | 28 | | |
| | | 29 | | |
| | 24 | 30 | | private Severity( string value ) => Value = value; |
| | | 31 | | |
| | | 32 | | |
| | | 33 | | /// <summary>The severity name: <c>High</c>, <c>Medium</c>, <c>Low</c>, or empty for <see cref="Unknown"/>.</summary |
| | | 34 | | public string Value { get; init; } |
| | | 35 | | |
| | | 36 | | |
| | | 37 | | /// <summary>Gets the severity name.</summary> |
| | | 38 | | /// <param name="value">Receives <see cref="Value"/>.</param> |
| | 0 | 39 | | public void Deconstruct( out string value ) => value = Value; |
| | | 40 | | |
| | | 41 | | |
| | | 42 | | /// <summary>Reads/writes a <see cref="Severity"/> as a plain JSON string.</summary> |
| | | 43 | | public sealed class SeverityJsonConverter : JsonConverter<Severity> |
| | | 44 | | { |
| | | 45 | | /// <summary>Reads a plain JSON string into the matching severity.</summary> |
| | | 46 | | /// <param name="reader">The JSON reader, positioned on the severity value.</param> |
| | | 47 | | /// <param name="typeToConvert">Always <see cref="Severity"/>.</param> |
| | | 48 | | /// <param name="options">The serializer options.</param> |
| | | 49 | | /// <returns>The matching severity; <see cref="Unknown"/> for <c>null</c>, <c>""</c> or <c>"Unknown"</c>.</retur |
| | | 50 | | /// <exception cref="JsonException">The JSON value is not a string.</exception> |
| | | 51 | | public override Severity? Read( ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options ) |
| | | 52 | | { |
| | 12 | 53 | | if ( reader.TokenType is JsonTokenType.Null ) { return Unknown; } |
| | | 54 | | |
| | 12 | 55 | | if ( reader.TokenType is not JsonTokenType.String ) |
| | | 56 | | { |
| | 0 | 57 | | throw new JsonException( "Severity must be a JSON string, e.g. \"High\"." ); |
| | | 58 | | } |
| | | 59 | | |
| | 12 | 60 | | return reader.GetString() switch |
| | 12 | 61 | | { |
| | 6 | 62 | | nameof( High ) => High, |
| | 0 | 63 | | nameof( Medium ) => Medium, |
| | 0 | 64 | | nameof( Low ) => Low, |
| | 6 | 65 | | nameof( Unknown ) or "" or null => Unknown, |
| | 0 | 66 | | var other => new Severity( other ), |
| | 12 | 67 | | }; |
| | | 68 | | } |
| | | 69 | | |
| | | 70 | | /// <summary>Writes a severity as a plain JSON string.</summary> |
| | | 71 | | /// <param name="writer">The JSON writer.</param> |
| | | 72 | | /// <param name="value">The severity to write; <see cref="Unknown"/> writes as <c>"Unknown"</c>.</param> |
| | | 73 | | /// <param name="options">The serializer options.</param> |
| | | 74 | | public override void Write( Utf8JsonWriter writer, Severity value, JsonSerializerOptions options ) |
| | 18 | 75 | | => writer.WriteStringValue( value.Value.Length == 0 ? nameof( Unknown ) : value.Value ); |
| | | 76 | | } |
| | | 77 | | } |