private async Task <IJsonReference> ResolveFileReferenceWithAlreadyResolvedCheckAsync(string fullJsonPath, string jsonPath, bool append) { try { var arr = Regex.Split(fullJsonPath, @"(?=#)"); var filePath = arr[0]; if (!_resolvedObjects.ContainsKey(filePath)) { var schema = await ResolveFileReferenceAsync(filePath).ConfigureAwait(false); schema.DocumentPath = jsonPath; if (schema is JsonSchema4 && append) { _schemaResolver.AppendSchema((JsonSchema4)schema, filePath.Split('/', '\\').Last().Split('.').First()); } _resolvedObjects[filePath] = schema; } var result = _resolvedObjects[filePath]; return(arr.Length == 1 ? result : await ResolveReferenceAsync(result, arr[1]).ConfigureAwait(false)); } catch (Exception exception) { throw new InvalidOperationException("Could not resolve the JSON path '" + jsonPath + "' with the full JSON path '" + fullJsonPath + "'.", exception); } }
private async Task <JsonSchema4> ResolveFileReferenceWithAlreadyResolvedCheckAsync(string fullJsonPath, string jsonPath) { try { var arr = Regex.Split(fullJsonPath, @"(?=#)"); if (!_resolvedSchemas.ContainsKey(arr[0])) { var schema = await ResolveFileReferenceAsync(arr[0]).ConfigureAwait(false); _schemaResolver.AppendSchema(schema, null); _resolvedSchemas[arr[0]] = schema; } var result = _resolvedSchemas[arr[0]]; return(arr.Length == 1 ? result : await ResolveReferenceAsync(result, arr[1]).ConfigureAwait(false)); } catch (Exception exception) { throw new InvalidOperationException("Could not resolve the JSON path '" + jsonPath + "' with the full JSON path '" + fullJsonPath + "'.", exception); } }
/// <summary>Gets the JSON path of the given object.</summary> /// <param name="root">The root object.</param> /// <param name="searchedObject">The object to search.</param> /// <param name="schemaResolver">The schema resolver.</param> /// <returns>The path or <c>null</c> when the object could not be found.</returns> /// <exception cref="InvalidOperationException">Could not find the JSON path of a child object.</exception> public static string GetJsonPath(object root, object searchedObject, JsonSchemaResolver schemaResolver = null) { var path = GetJsonPath(root, searchedObject, "#", new HashSet <object>()); if (path == null) { var searchedSchema = searchedObject as JsonSchema4; if (schemaResolver != null && searchedSchema != null) { schemaResolver.AppendSchema(searchedSchema, null); return(GetJsonPath(root, searchedObject, schemaResolver)); } else { throw new InvalidOperationException("Could not find the JSON path of a child object."); } } return(path); }