Пример #1
0
        private void GenerateKnownTypes(Type type, ISchemaResolver schemaResolver, ISchemaDefinitionAppender schemaDefinitionAppender)
        {
            foreach (dynamic knownTypeAttribute in type.GetTypeInfo().GetCustomAttributes().Where(a => a.GetType().Name == "KnownTypeAttribute"))
            {
                var typeDescription = JsonObjectTypeDescription.FromType(knownTypeAttribute.Type, null, Settings.DefaultEnumHandling);
                var isIntegerEnum   = typeDescription.Type == JsonObjectType.Integer;

                if (!schemaResolver.HasSchema(knownTypeAttribute.Type, isIntegerEnum))
                {
                    var knownSchema = Generate(knownTypeAttribute.Type, schemaResolver, schemaDefinitionAppender);
                    schemaDefinitionAppender.Append(knownSchema.ActualSchema);
                }
            }
        }
Пример #2
0
        private void GenerateKnownTypes(Type type, JsonSchema4 rootSchema, ISchemaDefinitionAppender schemaDefinitionAppender, ISchemaResolver schemaResolver)
        {
            foreach (var knownTypeAttribute in type.GetTypeInfo().GetCustomAttributes <KnownTypeAttribute>())
            {
                var typeDescription = JsonObjectTypeDescription.FromType(knownTypeAttribute.Type, null, Settings.DefaultEnumHandling);
                var isIntegerEnum   = typeDescription.Type == JsonObjectType.Integer;

                if (!schemaResolver.HasSchema(knownTypeAttribute.Type, isIntegerEnum))
                {
                    var knownSchema = Generate(knownTypeAttribute.Type, rootSchema, null, schemaDefinitionAppender, schemaResolver);
                    schemaDefinitionAppender.Append(rootSchema, knownSchema.ActualSchema);
                }
            }
        }
Пример #3
0
        /// <summary>Gets the JSON path of the given object.</summary>
        /// <param name="root">The root object.</param>
        /// <param name="objectToSearch">The object to search.</param>
        /// <param name="schemaDefinitionAppender">Appends the <paramref name="objectToSearch"/> to the 'definitions' if it could not be found.</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 objectToSearch, ISchemaDefinitionAppender schemaDefinitionAppender = null)
        {
            var path = GetJsonPath(root, objectToSearch, "#", new HashSet<object>());
            if (path == null)
            {
                if (schemaDefinitionAppender != null && objectToSearch is JsonSchema4)
                {
                    schemaDefinitionAppender.Append(root, (JsonSchema4)objectToSearch);
                    return GetJsonPath(root, objectToSearch, schemaDefinitionAppender);
                }
                else
                    throw new InvalidOperationException("Could not find the JSON path of a child object.");

            }
            return path;
        }
Пример #4
0
        /// <summary>Gets the JSON path of the given object.</summary>
        /// <param name="root">The root object.</param>
        /// <param name="objectToSearch">The object to search.</param>
        /// <param name="schemaDefinitionAppender">Appends the <paramref name="objectToSearch"/> to the 'definitions' if it could not be found.</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 objectToSearch, ISchemaDefinitionAppender schemaDefinitionAppender = null)
        {
            var path = GetJsonPath(root, objectToSearch, "#", new List <object>());

            if (path == null)
            {
                if (schemaDefinitionAppender != null && objectToSearch is JsonSchema4)
                {
                    schemaDefinitionAppender.Append(root, (JsonSchema4)objectToSearch);
                    return(GetJsonPath(root, objectToSearch, schemaDefinitionAppender));
                }
                else
                {
                    throw new InvalidOperationException("Could not find the JSON path of a child object.");
                }
            }
            return(path);
        }
Пример #5
0
        /// <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="schemaDefinitionAppender">Appends the <paramref name="searchedObject"/> to the 'definitions' if it could not be found.</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, ISchemaDefinitionAppender schemaDefinitionAppender = null)
        {
            var path = GetJsonPath(root, searchedObject, "#", new HashSet <object>());

            if (path == null)
            {
                var searchedSchema = searchedObject as JsonSchema4;
                if (schemaDefinitionAppender != null && searchedSchema != null)
                {
                    schemaDefinitionAppender.Append(searchedSchema);
                    return(GetJsonPath(root, searchedObject, schemaDefinitionAppender));
                }
                else
                {
                    throw new InvalidOperationException("Could not find the JSON path of a child object.");
                }
            }
            return(path);
        }