| | | 1 | | using System.Text.Json; |
| | | 2 | | using System.Text.Json.Serialization; |
| | | 3 | | |
| | | 4 | | namespace Elsa.Secrets.Models; |
| | | 5 | | |
| | | 6 | | public class CaseInsensitiveHashSetConverter : JsonConverter<HashSet<string>> |
| | | 7 | | { |
| | | 8 | | public override HashSet<string> Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) |
| | | 9 | | { |
| | 8 | 10 | | if (reader.TokenType == JsonTokenType.Null) |
| | 0 | 11 | | return new HashSet<string>(StringComparer.OrdinalIgnoreCase); |
| | | 12 | | |
| | 8 | 13 | | if (reader.TokenType != JsonTokenType.StartArray) |
| | 0 | 14 | | throw new JsonException("Expected an array of strings."); |
| | | 15 | | |
| | 8 | 16 | | var values = new HashSet<string>(StringComparer.OrdinalIgnoreCase); |
| | 9 | 17 | | while (reader.Read()) |
| | | 18 | | { |
| | 9 | 19 | | if (reader.TokenType == JsonTokenType.EndArray) |
| | 8 | 20 | | return values; |
| | | 21 | | |
| | 1 | 22 | | if (reader.TokenType != JsonTokenType.String) |
| | 0 | 23 | | throw new JsonException("Expected a string tag."); |
| | | 24 | | |
| | 1 | 25 | | var value = reader.GetString(); |
| | 1 | 26 | | if (!string.IsNullOrWhiteSpace(value)) |
| | 1 | 27 | | values.Add(value); |
| | | 28 | | } |
| | | 29 | | |
| | 0 | 30 | | throw new JsonException("Unexpected end of JSON while reading secret tags."); |
| | | 31 | | } |
| | | 32 | | |
| | | 33 | | public override void Write(Utf8JsonWriter writer, HashSet<string> value, JsonSerializerOptions options) |
| | | 34 | | { |
| | 7 | 35 | | writer.WriteStartArray(); |
| | 16 | 36 | | foreach (var item in value.Order(StringComparer.OrdinalIgnoreCase)) |
| | 1 | 37 | | writer.WriteStringValue(item); |
| | 7 | 38 | | writer.WriteEndArray(); |
| | 7 | 39 | | } |
| | | 40 | | } |