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;
namespace NJsonSchema
{
internal class ExtensionDataDeserializationConverter : JsonConverter
{
public override bool CanRead => true;
public override bool CanWrite => false;
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
IJsonExtensionObject jsonExtensionObject = (IJsonExtensionObject)Activator.CreateInstance(objectType);
serializer.Populate(reader, jsonExtensionObject);
DeserializeExtensionDataSchemas(jsonExtensionObject, serializer);
return jsonExtensionObject;
}
public override bool CanConvert(Type objectType)
{
return true;
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
throw new NotImplementedException();
}
internal 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);
}
}
}
private object TryDeserializeValueSchemas(object value, JsonSerializer serializer)
{
JObject jObject;
if ((jObject = (value as JObject)) != null) {
if (jObject.Property("type") != null || jObject.Property("properties") != null)
try {
return jObject.ToObject<JsonSchema4>(serializer);
} catch {
}
Dictionary<string, object> dictionary = new Dictionary<string, object>();
{
foreach (JProperty item in jObject.Properties()) {
dictionary[item.Name] = TryDeserializeValueSchemas(item.Value, serializer);
}
return dictionary;
}
}
JArray source;
if ((source = (value as JArray)) != null)
return (from i in source
select TryDeserializeValueSchemas(i, serializer)).ToArray();
JValue jValue;
if ((jValue = (value as JValue)) != null)
return jValue.Value;
return value;
}
}
}