JsonPathUtilities
Utilities to work with JSON paths.
using Newtonsoft.Json.Serialization;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
namespace NJsonSchema
{
public static class JsonPathUtilities
{
public static string GetJsonPath(object rootObject, object searchedObject)
{
return GetJsonPath(rootObject, searchedObject, new DefaultContractResolver());
}
public static string GetJsonPath(object rootObject, object searchedObject, IContractResolver contractResolver)
{
return GetJsonPaths(rootObject, new List<object> {
searchedObject
}, contractResolver)[searchedObject];
}
public static IReadOnlyDictionary<object, string> GetJsonPaths(object rootObject, IEnumerable<object> searchedObjects, IContractResolver contractResolver)
{
if (rootObject == null)
throw new ArgumentNullException("rootObject");
Dictionary<object, string> dictionary = searchedObjects.ToDictionary((Func<object, object>)((object o) => o), (Func<object, string>)((object o) => null));
FindJsonPaths(rootObject, dictionary, "#", new HashSet<object>(), contractResolver);
if (dictionary.Any((KeyValuePair<object, string> p) => p.Value == null))
throw new InvalidOperationException("Could not find the JSON path of a referenced schema: Manually referenced schemas must be added to the 'Definitions' of a parent schema.");
return dictionary;
}
private static bool FindJsonPaths(object obj, Dictionary<object, string> searchedObjects, string basePath, HashSet<object> checkedObjects, IContractResolver contractResolver)
{
if (obj == null || obj is string || checkedObjects.Contains(obj))
return false;
if (searchedObjects.ContainsKey(obj)) {
searchedObjects[obj] = basePath;
if (searchedObjects.All((KeyValuePair<object, string> p) => p.Value != null))
return true;
}
checkedObjects.Add(obj);
if (obj is IDictionary) {
foreach (object key in ((IDictionary)obj).Keys) {
if (FindJsonPaths(((IDictionary)obj)[key], searchedObjects, basePath + "/" + key?.ToString(), checkedObjects, contractResolver))
return true;
}
} else if (obj is IEnumerable) {
int num = 0;
foreach (object item in (IEnumerable)obj) {
if (FindJsonPaths(item, searchedObjects, basePath + "/" + num.ToString(), checkedObjects, contractResolver))
return true;
num++;
}
} else {
Type type = obj.GetType();
JsonObjectContract jsonObjectContract = contractResolver.ResolveContract(type) as JsonObjectContract;
if (jsonObjectContract != null) {
foreach (JsonProperty item2 in from p in jsonObjectContract.Properties
where !p.Ignored
select p) {
object value = item2.ValueProvider.GetValue(obj);
if (value != null && FindJsonPaths(value, searchedObjects, basePath + "/" + item2.PropertyName, checkedObjects, contractResolver))
return true;
}
if (obj is IJsonExtensionObject) {
PropertyInfo runtimeProperty = type.GetRuntimeProperty("ExtensionData");
if ((object)runtimeProperty != null && FindJsonPaths(runtimeProperty.GetValue(obj), searchedObjects, basePath, checkedObjects, contractResolver))
return true;
}
}
}
return false;
}
}
}