Example #1
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="referenceResolverFactory">The JSON reference resolver factory.</param>
        /// <returns>The JSON Schema.</returns>
        public static async Task <JsonSchema4> FromJsonAsync(string data, string documentPath, Func <JsonSchema4, JsonReferenceResolver> referenceResolverFactory)
        {
            data = JsonSchemaReferenceUtilities.ConvertJsonReferences(data);

            var settings = new JsonSerializerSettings
            {
                ContractResolver           = CreateJsonSerializerContractResolver(SchemaType.JsonSchema),
                MetadataPropertyHandling   = MetadataPropertyHandling.Ignore,
                ConstructorHandling        = ConstructorHandling.Default,
                ReferenceLoopHandling      = ReferenceLoopHandling.Serialize,
                PreserveReferencesHandling = PreserveReferencesHandling.Objects
            };

            JsonSchemaSerializationContext.CurrentSchemaType = SchemaType.JsonSchema;
            var schema = JsonConvert.DeserializeObject <JsonSchema4>(data, settings);

            schema.DocumentPath = documentPath;

            var referenceResolver = referenceResolverFactory(schema);

            if (!string.IsNullOrEmpty(documentPath))
            {
                referenceResolver.AddDocumentReference(documentPath, schema);
            }

            await JsonSchemaReferenceUtilities.UpdateSchemaReferencesAsync(schema, referenceResolver).ConfigureAwait(false);

            return(schema);
        }
Example #2
0
        /// <summary>Deserializes a JSON string to a <see cref="JsonSchema4"/>. </summary>
        /// <param name="data">The JSON string. </param>
        /// <returns></returns>
        public static JsonSchema4 FromJson(string data)
        {
            data = JsonSchemaReferenceUtilities.ConvertJsonReferences(data);
            var schema = JsonConvert.DeserializeObject <JsonSchema4>(data, new JsonSerializerSettings
            {
                ConstructorHandling        = ConstructorHandling.Default,
                ReferenceLoopHandling      = ReferenceLoopHandling.Serialize,
                PreserveReferencesHandling = PreserveReferencesHandling.Objects
            });

            JsonSchemaReferenceUtilities.UpdateSchemaReferences(schema);
            return(schema);
        }
Example #3
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);
        }
Example #4
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="referenceResolverFactory">The JSON reference resolver factory.</param>
        /// <returns>The JSON Schema.</returns>
        public static async Task <JsonSchema4> FromJsonAsync(string data, string documentPath, Func <JsonSchema4, JsonReferenceResolver> referenceResolverFactory)
        {
            data = JsonSchemaReferenceUtilities.ConvertJsonReferences(data);
            var schema = JsonConvert.DeserializeObject <JsonSchema4>(data, new JsonSerializerSettings
            {
                ConstructorHandling        = ConstructorHandling.Default,
                ReferenceLoopHandling      = ReferenceLoopHandling.Serialize,
                PreserveReferencesHandling = PreserveReferencesHandling.Objects
            });

            schema.DocumentPath = documentPath;

            var referenceResolver = referenceResolverFactory(schema);

            if (!string.IsNullOrEmpty(documentPath))
            {
                referenceResolver.AddDocumentReference(documentPath, schema);
            }

            await JsonSchemaReferenceUtilities.UpdateSchemaReferencesAsync(schema, referenceResolver).ConfigureAwait(false);

            return(schema);
        }