From 38cc58d429046703637c9754a1a5e80520da8dfa Mon Sep 17 00:00:00 2001 From: Arlo Godfrey Date: Sat, 9 Mar 2024 13:07:06 -0600 Subject: [PATCH] Fixed polymorphic serialization --- .../ApplicationConnectionJsonConverter.cs | 22 ++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/src/Models/JsonConverters/ApplicationConnectionJsonConverter.cs b/src/Models/JsonConverters/ApplicationConnectionJsonConverter.cs index a93bf3e..f032a26 100644 --- a/src/Models/JsonConverters/ApplicationConnectionJsonConverter.cs +++ b/src/Models/JsonConverters/ApplicationConnectionJsonConverter.cs @@ -90,15 +90,27 @@ public override bool CanConvert(Type objectType) /// public override object? ReadJson(JsonReader reader, Type objectType, object? existingValue, JsonSerializer serializer) { - if (reader.Value is null) - return null; - if (reader.TokenType == JsonToken.Null) return null; - var token = JToken.Load(reader); + if (reader.TokenType == JsonToken.PropertyName) + { + return reader.Value; + } + + if (reader.TokenType == JsonToken.StartObject) + { + var jobj = JObject.Load(reader); + return ApplicationConnectionSerializationHelpers.ReadConnection(jobj, serializer); + } + + if (reader.TokenType == JsonToken.StartArray) + { + var jarray = JArray.Load(reader); + return ApplicationConnectionSerializationHelpers.ReadConnection(jarray, serializer); + } - return ApplicationConnectionSerializationHelpers.ReadConnection(token, serializer); + throw new NotSupportedException($"Token type {reader.TokenType} is not supported."); } ///