public JsonReferenceUpdater(object rootObject, JsonReferenceResolver referenceResolver, IContractResolver contractResolver)
     : base(contractResolver)
 {
     _rootObject        = rootObject;
     _referenceResolver = referenceResolver;
     _contractResolver  = contractResolver;
 }
예제 #2
0
        /// <summary>Deserializes a JSON string to a <see cref="JsonSchema4"/>. </summary>
        /// <param name="data">The JSON string. </param>
        /// <param name="documentPath">The document path (URL or file path) for resolving relative document references.</param>
        /// <param name="jsonReferenceResolver">The JSON document resolver.</param>
        /// <returns>The JSON Schema.</returns>
        public static JsonSchema4 FromJson(string data, string documentPath, JsonReferenceResolver jsonReferenceResolver)
        {
            data = JsonSchemaReferenceUtilities.ConvertJsonReferences(data);
            var schema = JsonConvert.DeserializeObject <JsonSchema4>(data, new JsonSerializerSettings
            {
                ConstructorHandling        = ConstructorHandling.Default,
                ReferenceLoopHandling      = ReferenceLoopHandling.Serialize,
                PreserveReferencesHandling = PreserveReferencesHandling.Objects
            });

            schema.DocumentPath = documentPath;

            if (jsonReferenceResolver != null && !string.IsNullOrEmpty(documentPath))
            {
                jsonReferenceResolver.AddDocumentReference(documentPath, schema);
            }

            JsonSchemaReferenceUtilities.UpdateSchemaReferences(schema, jsonReferenceResolver);
            return(schema);
        }
예제 #3
0
        /// <summary>Loads a JSON Schema from a given file path (only available in .NET 4.x).</summary>
        /// <param name="filePath">The file path.</param>
        /// <param name="jsonReferenceResolver">The JSON document resolver.</param>
        /// <returns>The JSON Schema.</returns>
        public static JsonSchema4 FromFile(string filePath, JsonReferenceResolver jsonReferenceResolver)
        {
            var data = DynamicApis.FileReadAllText(filePath);

            return(FromJson(data, filePath, jsonReferenceResolver));
        }
예제 #4
0
        /// <summary>Deserializes a JSON string to a <see cref="JsonSchema"/>. </summary>
        /// <param name="data">The JSON string. </param>
        /// <param name="documentPath">The document path (URL or file path) for resolving relative document references.</param>
        /// <param name="cancellationToken">The cancellation token</param>
        /// <returns>The JSON Schema.</returns>
        public static async Task <JsonSchema> FromJsonAsync(string data, string documentPath, CancellationToken cancellationToken = default)
        {
            var factory = JsonReferenceResolver.CreateJsonReferenceResolverFactory(new DefaultTypeNameGenerator());

            return(await FromJsonAsync(data, documentPath, factory, cancellationToken).ConfigureAwait(false));
        }
예제 #5
0
        /// <summary>Loads a JSON Schema from a given URL (only available in .NET 4.x).</summary>
        /// <param name="url">The URL to the document.</param>
        /// <returns>The JSON Schema.</returns>
        /// <exception cref="NotSupportedException">The HttpClient.GetAsync API is not available on this platform.</exception>
        public static async Task <JsonSchema> FromUrlAsync(string url)
        {
            var factory = JsonReferenceResolver.CreateJsonReferenceResolverFactory(new DefaultTypeNameGenerator());

            return(await FromUrlAsync(url, factory).ConfigureAwait(false));
        }
 /// <summary>Updates all <see cref="IJsonReferenceBase.Reference"/> properties from the
 /// available <see cref="IJsonReferenceBase.Reference"/> properties.</summary>
 /// <param name="referenceResolver">The JSON document resolver.</param>
 /// <param name="rootObject">The root object.</param>
 /// <param name="contractResolver">The contract resolver.</param>
 /// <param name="cancellationToken">The cancellation token</param>
 public static async Task UpdateSchemaReferencesAsync(object rootObject, JsonReferenceResolver referenceResolver,
                                                      IContractResolver contractResolver, CancellationToken cancellationToken = default)
 {
     var updater = new JsonReferenceUpdater(rootObject, referenceResolver, contractResolver);
     await updater.VisitAsync(rootObject, cancellationToken).ConfigureAwait(false);
 }
 /// <summary>Updates all <see cref="IJsonReferenceBase.Reference"/> properties from the
 /// available <see cref="IJsonReferenceBase.Reference"/> properties.</summary>
 /// <param name="referenceResolver">The JSON document resolver.</param>
 /// <param name="rootObject">The root object.</param>
 /// <param name="cancellationToken">The cancellation token</param>
 public static Task UpdateSchemaReferencesAsync(object rootObject, JsonReferenceResolver referenceResolver, CancellationToken cancellationToken = default) =>
 UpdateSchemaReferencesAsync(rootObject, referenceResolver, new DefaultContractResolver(), cancellationToken);
 /// <summary>Updates all <see cref="IJsonReferenceBase.Reference"/> properties from the
 /// available <see cref="IJsonReferenceBase.Reference"/> properties.</summary>
 /// <param name="referenceResolver">The JSON document resolver.</param>
 /// <param name="rootObject">The root object.</param>
 public static async Task UpdateSchemaReferencesAsync(object rootObject, JsonReferenceResolver referenceResolver)
 {
     var updater = new JsonReferenceUpdater(rootObject, referenceResolver);
     await updater.VisitAsync(rootObject).ConfigureAwait(false);
 }
 /// <summary>Updates all <see cref="JsonSchema4.SchemaReference"/> properties from the
 /// available <see cref="JsonSchema4.SchemaReferencePath"/> properties.</summary>
 /// <param name="jsonReferenceResolver">The JSON document resolver.</param>
 /// <param name="root">The root.</param>
 public static void UpdateSchemaReferences(object root, JsonReferenceResolver jsonReferenceResolver)
 {
     UpdateSchemaReferences(root, root, new HashSet <object>(), jsonReferenceResolver);
 }
        private static void UpdateSchemaReferences(object root, object obj, HashSet <object> checkedObjects, JsonReferenceResolver jsonReferenceResolver)
        {
            if (obj == null || obj is string)
            {
                return;
            }

            var schema = obj as JsonSchema4;

            if (schema != null && schema.SchemaReferencePath != null)
            {
                schema.SchemaReference = jsonReferenceResolver.ResolveReference(root, schema.SchemaReferencePath);
            }

            if (obj is IDictionary)
            {
                foreach (var item in ((IDictionary)obj).Values)
                {
                    UpdateSchemaReferences(root, item, checkedObjects, jsonReferenceResolver);
                }
            }
            else if (obj is IEnumerable)
            {
                foreach (var item in ((IEnumerable)obj).OfType <object>().ToArray())
                {
                    UpdateSchemaReferences(root, item, checkedObjects, jsonReferenceResolver);
                }
            }

            if (!(obj is JToken))
            {
                foreach (var property in ReflectionCache.GetProperties(obj.GetType()).Where(p =>
                                                                                            p.CanRead && p.IsIndexer == false && p.MemberInfo is PropertyInfo &&
                                                                                            p.CustomAttributes.JsonIgnoreAttribute == null))
                {
                    var value = property.GetValue(obj);
                    if (value != null)
                    {
                        if (!checkedObjects.Contains(value))
                        {
                            checkedObjects.Add(value);
                            UpdateSchemaReferences(root, value, checkedObjects, jsonReferenceResolver);
                        }
                    }
                }
            }
        }
예제 #11
0
        /// <summary>Deserializes a JSON string to a <see cref="JsonSchema4"/>. </summary>
        /// <param name="data">The JSON string. </param>
        /// <param name="documentPath">The document path (URL or file path) for resolving relative document references.</param>
        /// <returns>The JSON Schema.</returns>
        public static async Task <JsonSchema4> FromJsonAsync(string data, string documentPath)
        {
            var factory = JsonReferenceResolver.CreateJsonReferenceResolverFactory(new JsonSchemaGeneratorSettings());

            return(await FromJsonAsync(data, documentPath, factory).ConfigureAwait(false));
        }
예제 #12
0
        /// <summary>Loads a JSON Schema from a given URL (only available in .NET 4.x).</summary>
        /// <param name="url">The URL to the document.</param>
        /// <returns>The JSON Schema.</returns>
        /// <exception cref="NotSupportedException">The HttpClient.GetAsync API is not available on this platform.</exception>
        public static async Task <JsonSchema4> FromUrlAsync(string url)
        {
            var factory = JsonReferenceResolver.CreateJsonReferenceResolverFactory(new JsonSchemaGeneratorSettings());

            return(await FromUrlAsync(url, factory).ConfigureAwait(false));
        }
 /// <summary>Updates all <see cref="JsonSchema4.SchemaReference"/> properties from the
 /// available <see cref="JsonSchema4.SchemaReferencePath"/> properties.</summary>
 /// <param name="referenceResolver">The JSON document resolver.</param>
 /// <param name="rootObject">The root object.</param>
 public static async Task UpdateSchemaReferencesAsync(object rootObject, JsonReferenceResolver referenceResolver)
 {
     await UpdateSchemaReferencesAsync(rootObject, rootObject, new HashSet <object>(), referenceResolver).ConfigureAwait(false);
 }
 public JsonReferenceUpdater(object rootObject, JsonReferenceResolver referenceResolver)
 {
     _rootObject        = rootObject;
     _referenceResolver = referenceResolver;
 }
예제 #15
0
        /// <summary>Loads a JSON Schema from a given URL (only available in .NET 4.x).</summary>
        /// <param name="url">The URL to the document.</param>
        /// <param name="jsonReferenceResolver">The JSON document resolver.</param>
        /// <returns>The JSON Schema.</returns>
        public static JsonSchema4 FromUrl(string url, JsonReferenceResolver jsonReferenceResolver)
        {
            var data = DynamicApis.HttpGet(url);

            return(FromJson(data, url, jsonReferenceResolver));
        }
 /// <summary>Updates all <see cref="IJsonReferenceBase.Reference"/> properties from the
 /// available <see cref="IJsonReferenceBase.Reference"/> properties.</summary>
 /// <param name="referenceResolver">The JSON document resolver.</param>
 /// <param name="rootObject">The root object.</param>
 public static Task UpdateSchemaReferencesAsync(object rootObject, JsonReferenceResolver referenceResolver) =>
 UpdateSchemaReferencesAsync(rootObject, referenceResolver, new DefaultContractResolver());
예제 #17
0
 /// <summary>Loads a JSON Schema from a given file path (only available in .NET 4.x).</summary>
 /// <param name="filePath">The file path.</param>
 /// <returns>The JSON Schema.</returns>
 public static async Task<JsonSchema4> FromFileAsync(string filePath)
 {
     var factory = JsonReferenceResolver.CreateJsonReferenceResolverFactory(new JsonSchemaGeneratorSettings());
     return await FromFileAsync(filePath, factory).ConfigureAwait(false);
 }