<PackageReference Include="NJsonSchema" Version="11.5.1" />

ExtensionDataDeserializationConverter

Deserializes all JSON Schemas in the extension data property.
using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; using System.Linq; using System.Runtime.CompilerServices; namespace NJsonSchema { [System.Runtime.CompilerServices.NullableContext(1)] [System.Runtime.CompilerServices.Nullable(0)] internal sealed class ExtensionDataDeserializationConverter : JsonConverter { public override bool CanRead => true; public override bool CanWrite => false; [return: System.Runtime.CompilerServices.Nullable(2)] public override object ReadJson(JsonReader reader, Type objectType, [System.Runtime.CompilerServices.Nullable(2)] object existingValue, JsonSerializer serializer) { if (reader.TokenType != JsonToken.Null) { IJsonExtensionObject jsonExtensionObject = (IJsonExtensionObject)Activator.CreateInstance(objectType); serializer.Populate(reader, jsonExtensionObject); DeserializeExtensionDataSchemas(jsonExtensionObject, serializer); return jsonExtensionObject; } reader.Skip(); return null; } public override bool CanConvert(Type objectType) { return true; } public override void WriteJson(JsonWriter writer, [System.Runtime.CompilerServices.Nullable(2)] object value, JsonSerializer serializer) { throw new NotImplementedException(); } internal static void DeserializeExtensionDataSchemas(IJsonExtensionObject extensionObject, JsonSerializer serializer) { if (extensionObject.ExtensionData != null) { KeyValuePair<string, object>[] array = extensionObject.ExtensionData.ToArray(); for (int i = 0; i < array.Length; i++) { KeyValuePair<string, object> keyValuePair = array[i]; extensionObject.ExtensionData[keyValuePair.Key] = TryDeserializeValueSchemas(keyValuePair.Value, serializer); } } } [System.Runtime.CompilerServices.NullableContext(2)] private static object TryDeserializeValueSchemas(object value, [System.Runtime.CompilerServices.Nullable(1)] JsonSerializer serializer) { JObject jObject = value as JObject; if (jObject != null) { bool flag = jObject.Property("type") != null || jObject.Property("properties") != null; if (flag) { JProperty jProperty = jObject.Property("required"); if (jProperty != null) { JToken value2 = jProperty.Value; if (value2 != null && value2.Type == JTokenType.Boolean) flag = false; } } if (flag) try { return jObject.ToObject<JsonSchema>(serializer); } catch { } Dictionary<string, object> dictionary = new Dictionary<string, object>(jObject.Count); { foreach (JProperty item in jObject.Properties()) { dictionary[item.Name] = TryDeserializeValueSchemas(item.Value, serializer); } return dictionary; } } JArray jArray = value as JArray; if (jArray != null) return (from i in jArray select TryDeserializeValueSchemas(i, serializer)).ToArray(); JValue jValue = value as JValue; if (jValue != null) return jValue.Value; return value; } } }