summaryrefslogtreecommitdiffstats
path: root/generator/plugins/dotnet/custom/CustomArrayConverter.cs
diff options
context:
space:
mode:
Diffstat (limited to 'generator/plugins/dotnet/custom/CustomArrayConverter.cs')
-rw-r--r--generator/plugins/dotnet/custom/CustomArrayConverter.cs42
1 files changed, 42 insertions, 0 deletions
diff --git a/generator/plugins/dotnet/custom/CustomArrayConverter.cs b/generator/plugins/dotnet/custom/CustomArrayConverter.cs
new file mode 100644
index 0000000..d34eb62
--- /dev/null
+++ b/generator/plugins/dotnet/custom/CustomArrayConverter.cs
@@ -0,0 +1,42 @@
+using Newtonsoft.Json;
+using Newtonsoft.Json.Linq;
+using System.Collections.Immutable;
+
+public class CustomArrayConverter<T> : JsonConverter<ImmutableArray<T>>
+{
+ public override ImmutableArray<T> ReadJson(JsonReader reader, Type objectType, ImmutableArray<T> existingValue, bool hasExistingValue, JsonSerializer serializer)
+ {
+ if (reader.TokenType == JsonToken.Null)
+ {
+ return default(ImmutableArray<T>);
+ }
+
+ JArray array = JArray.Load(reader);
+ ImmutableArray<T>.Builder builder = ImmutableArray.CreateBuilder<T>();
+
+ for (int i = 0; i < array.Count; i++)
+ {
+ builder.Add((T)array[i].ToObject(typeof(T))!);
+ }
+
+ return builder.ToImmutable();
+
+ }
+
+ public override void WriteJson(JsonWriter writer, ImmutableArray<T> value, JsonSerializer serializer)
+ {
+ if (value.IsDefault)
+ {
+ writer.WriteNull();
+ }
+ else
+ {
+ writer.WriteStartArray();
+ foreach (var item in value)
+ {
+ serializer.Serialize(writer, item);
+ }
+ writer.WriteEndArray();
+ }
+ }
+} \ No newline at end of file