本文整理汇总了C#中Newtonsoft.Json.Schema.JsonSchema.WriteTo方法的典型用法代码示例。如果您正苦于以下问题:C# JsonSchema.WriteTo方法的具体用法?C# JsonSchema.WriteTo怎么用?C# JsonSchema.WriteTo使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Newtonsoft.Json.Schema.JsonSchema
的用法示例。
在下文中一共展示了JsonSchema.WriteTo方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Example
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
}
示例2: WriteTo_PatternProperties
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);
}
示例3: WriteTo_ExclusiveMinimum_ExclusiveMaximum
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);
}
示例4: HandleContractMetadataAsync
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;
}
}
示例5: WriteTo_PositionalItemsValidation_FalseWithItemsSchema
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);
}
示例6: WriteTo_PositionalItemsValidation_True
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);
}