Example #1
0
        public static void WriteSchema(
            TextWriter writer,
            JsonSchema schema,
            Formatting formatting = Formatting.Indented)
        {
            var stringWriter = new StringWriter();
            var jsonWriter   = new JsonTextWriter(stringWriter);

            WriteSchema(jsonWriter, schema, formatting);

            // Change "$$ref" to "$ref" before we ask write it to the output.
            string output = RefProperty.ConvertToOutput(stringWriter.ToString());

            writer.Write(output);
        }
Example #2
0
        public static JsonSchema ReadSchema(string jsonText, string filePath)
        {
            // Change "$ref" to "$$ref" before we ask Json.NET to deserialize it,
            // because Json.NET treats "$ref" specially.
            jsonText = RefProperty.ConvertFromInput(jsonText);

            var traceWriter = new SchemaValidationExceptionCapturingTraceWriter();

            var serializer = new JsonSerializer
            {
                ContractResolver = new JsonSchemaContractResolver(),
                TraceWriter      = traceWriter
            };

            JsonSchema schema;

            using (var jsonReader = new JsonTextReader(new StringReader(jsonText)))
            {
                try
                {
                    schema = serializer.Deserialize <JsonSchema>(jsonReader);
                }
                catch (JsonReaderException ex)
                {
                    throw new JsonSyntaxException(filePath, ex);
                }
            }


            if (traceWriter.SchemaValidationExceptions.Any())
            {
                throw new SchemaValidationException(traceWriter.SchemaValidationExceptions);
            }

            return(schema);
        }
Example #3
0
        public static JsonSchema ReadSchema(string jsonText, string filePath)
        {
            SchemaValidationErrorAccumulator.Instance.Clear();

            // Change "$ref" to "$$ref" before we ask Json.NET to deserialize it,
            // because Json.NET treats "$ref" specially.
            jsonText = RefProperty.ConvertFromInput(jsonText);

            var serializer = new JsonSerializer
            {
                ContractResolver = new JsonSchemaContractResolver(),
            };

            JsonSchema schema;

            using (var jsonReader = new JsonTextReader(new StringReader(jsonText)))
            {
                try
                {
                    schema = serializer.Deserialize <JsonSchema>(jsonReader);
                }
                catch (JsonReaderException ex)
                {
                    throw new JsonSyntaxException(filePath, ex);
                }
                finally
                {
                    if (SchemaValidationErrorAccumulator.Instance.HasErrors)
                    {
                        throw SchemaValidationErrorAccumulator.Instance.ToException();
                    }
                }
            }

            return(schema);
        }