예제 #1
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 JsonSchema4 FromFile(string filePath)
        {
            var data          = FullDotNetMethods.FileReadAllText(filePath);
            var rootDirectory = FullDotNetMethods.PathGetDirectoryName(filePath);

            return(FromJson(data, rootDirectory));
        }
        /// <summary>Gets the object from the given JSON path.</summary>
        /// <param name="root">The root object.</param>
        /// <param name="path">The JSON path.</param>
        /// <returns>The object or <c>null</c> when the object could not be found.</returns>
        /// <exception cref="InvalidOperationException">Could not resolve the path.</exception>
        /// <exception cref="NotSupportedException">Could not resolve the path.</exception>
        public static JsonSchema4 GetObjectFromJsonPath(object root, string path)
        {
            if (path == "#")
            {
                if (root is JsonSchema4)
                {
                    return((JsonSchema4)root);
                }

                throw new InvalidOperationException("Could not resolve the path '#' because the root object is not a JsonSchema4.");
            }
            else if (path.StartsWith("#/"))
            {
                var allSegments = path.Split('/').Skip(1).ToList();
                var schema      = GetObjectFromJsonPath(root, allSegments, allSegments, new HashSet <object>());
                if (schema == null)
                {
                    throw new InvalidOperationException("Could not resolve the path '" + path + "'.");
                }

                return(schema);
            }
            else if (path.StartsWith("http://") || path.StartsWith("https://"))
            {
                if (FullDotNetMethods.SupportsFullDotNetMethods)
                {
                    return(JsonSchema4.FromJson(FullDotNetMethods.HttpGet(path)));
                }
                else
                {
                    throw new NotSupportedException("Could not resolve the path '" + path +
                                                    "' because JSON web references are not supported on this platform.");
                }
            }
            else
            {
                if (FullDotNetMethods.SupportsFullDotNetMethods)
                {
                    var schema = root as JsonSchema4;
                    if (schema != null && schema.RootDirectory != null)
                    {
                        return(JsonSchema4.FromJson(FullDotNetMethods.FileReadAllText(FullDotNetMethods.PathCombine(schema.RootDirectory, path))));
                    }
                    else
                    {
                        throw new NotSupportedException("Could not resolve the path '" + path +
                                                        "' because no root path is available.");
                    }
                }
                else
                {
                    throw new NotSupportedException("Could not resolve the path '" + path +
                                                    "' because JSON file references are not supported on this platform.");
                }
            }
        }