public void Example() { #region Usage JsonSchema schema = new JsonSchema { Type = JsonSchemaType.Object }; // serialize JsonSchema to a string and then write string to a file File.WriteAllText(@"c:\schema.json", schema.ToString()); // serialize JsonSchema directly to a file using (StreamWriter file = File.CreateText(@"c:\schema.json")) using (JsonTextWriter writer = new JsonTextWriter(file)) { schema.WriteTo(writer); } #endregion }
public void WriteTo_PatternProperties() { JsonSchema schema = new JsonSchema(); schema.PatternProperties = new Dictionary<string, JsonSchema> { { "[abc]", new JsonSchema() } }; StringWriter writer = new StringWriter(); JsonTextWriter jsonWriter = new JsonTextWriter(writer); jsonWriter.Formatting = Formatting.Indented; schema.WriteTo(jsonWriter); string json = writer.ToString(); Assert.AreEqual(@"{ ""patternProperties"": { ""[abc]"": {} } }", json); }
public void WriteTo_ExclusiveMinimum_ExclusiveMaximum() { JsonSchema schema = new JsonSchema(); schema.ExclusiveMinimum = true; schema.ExclusiveMaximum = true; StringWriter writer = new StringWriter(); JsonTextWriter jsonWriter = new JsonTextWriter(writer); jsonWriter.Formatting = Formatting.Indented; schema.WriteTo(jsonWriter); string json = writer.ToString(); Assert.AreEqual(@"{ ""exclusiveMinimum"": true, ""exclusiveMaximum"": true }", json); }
public virtual async Task<bool> HandleContractMetadataAsync(ServerActionContext context) { try { string result = string.Empty; string actionName = context.HttpContext.Request.Query["action"]?.Trim(); MethodInfo action = null; if (!string.IsNullOrEmpty(actionName)) { action = _actionResolver.Resolve(context.Contract, actionName); } if (action == null) { var contractMetadata = CrateContractMetadata(context); result = JsonConvert.SerializeObject( contractMetadata, Formatting.Indented, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }); } else { context.Action = action; JsonSchema actionSchema = new JsonSchema { Properties = new Dictionary<string, JsonSchema>(), Description = $"Request and response parameters for action '{actionName}'." }; List<ParameterInfo> actionParameters = BoltFramework.GetSerializableParameters(action).ToList(); if (actionParameters.Any()) { JsonSchemaGenerator generator = new JsonSchemaGenerator(); JsonSchema arguments = new JsonSchema { Properties = actionParameters.ToDictionary( p => p.Name, p => generator.Generate(p.ParameterType)), Required = true, Type = JsonSchemaType.Object, }; actionSchema.Properties.Add("request", arguments); } if (context.ResponseType != typeof(void)) { JsonSchemaGenerator generator = new JsonSchemaGenerator(); actionSchema.Properties.Add("response", generator.Generate(context.ResponseType)); } using (var sw = new StringWriter()) { using (JsonTextWriter jw = new JsonTextWriter(sw)) { jw.Formatting = Formatting.Indented; actionSchema.WriteTo(jw); } result = sw.GetStringBuilder().ToString(); } } context.HttpContext.Response.ContentType = "application/json"; context.HttpContext.Response.StatusCode = 200; await context.HttpContext.Response.WriteAsync(result); return true; } catch (Exception e) { Logger.LogWarning( BoltLogId.HandleContractMetadataError, "Failed to generate Bolt metadata for contract '{0}'. Error: {1}", context.Contract.Name, e); return false; } }
public void WriteTo_PositionalItemsValidation_FalseWithItemsSchema() { JsonSchema schema = new JsonSchema(); schema.Items = new List<JsonSchema> { new JsonSchema { Type = JsonSchemaType.String } }; StringWriter writer = new StringWriter(); JsonTextWriter jsonWriter = new JsonTextWriter(writer); jsonWriter.Formatting = Formatting.Indented; schema.WriteTo(jsonWriter); string json = writer.ToString(); StringAssert.AreEqual(@"{ ""items"": { ""type"": ""string"" } }", json); }
public void WriteTo_PositionalItemsValidation_True() { JsonSchema schema = new JsonSchema(); schema.PositionalItemsValidation = true; StringWriter writer = new StringWriter(); JsonTextWriter jsonWriter = new JsonTextWriter(writer); jsonWriter.Formatting = Formatting.Indented; schema.WriteTo(jsonWriter); string json = writer.ToString(); StringAssert.AreEqual(@"{ ""items"": [] }", json); }