summaryrefslogtreecommitdiffstats
path: root/generator/plugins/dotnet/custom/DocumentSelectorConverter.cs
diff options
context:
space:
mode:
Diffstat (limited to 'generator/plugins/dotnet/custom/DocumentSelectorConverter.cs')
-rw-r--r--generator/plugins/dotnet/custom/DocumentSelectorConverter.cs34
1 files changed, 34 insertions, 0 deletions
diff --git a/generator/plugins/dotnet/custom/DocumentSelectorConverter.cs b/generator/plugins/dotnet/custom/DocumentSelectorConverter.cs
new file mode 100644
index 0000000..3124416
--- /dev/null
+++ b/generator/plugins/dotnet/custom/DocumentSelectorConverter.cs
@@ -0,0 +1,34 @@
+using Newtonsoft.Json;
+using Newtonsoft.Json.Linq;
+using System;
+
+public class DocumentSelectorConverter : JsonConverter<DocumentSelector>
+{
+ public override void WriteJson(JsonWriter writer, DocumentSelector? value, JsonSerializer serializer)
+ {
+ if (value is null)
+ {
+ writer.WriteNull();
+ }
+ else
+ {
+ serializer.Serialize(writer, (DocumentFilter[])value);
+ }
+ }
+
+ public override DocumentSelector ReadJson(JsonReader reader, Type objectType, DocumentSelector? existingValue, bool hasExistingValue, JsonSerializer serializer)
+ {
+ if (reader.TokenType == JsonToken.Null)
+ {
+ return null!;
+ }
+ var token = JToken.Load(reader);
+ if (token.Type == JTokenType.Array)
+ {
+ var filters = token.ToObject<DocumentFilter[]>(serializer);
+ return new DocumentSelector(filters ?? Array.Empty<DocumentFilter>());
+ }
+
+ throw new JsonSerializationException("Invalid JSON for DocumentSelector");
+ }
+}