本文整理汇总了C#中Newtonsoft.Json.Schema.JsonSchemaResolver类的典型用法代码示例。如果您正苦于以下问题:C# JsonSchemaResolver类的具体用法?C# JsonSchemaResolver怎么用?C# JsonSchemaResolver使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
JsonSchemaResolver类属于Newtonsoft.Json.Schema命名空间,在下文中一共展示了JsonSchemaResolver类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Generate
public JsonSchema Generate(Type type, JsonSchemaResolver resolver, bool rootSchemaNullable)
{
ValidationUtils.ArgumentNotNull((object) type, "type");
ValidationUtils.ArgumentNotNull((object) resolver, "resolver");
this._resolver = resolver;
return this.GenerateInternal(type, !rootSchemaNullable ? Required.Always : Required.Default, false);
}
示例2: AddSchema
public void AddSchema()
{
string first = @"{
""id"":""first"",
""type"":""object"",
""properties"":
{
""firstproperty"":{""type"":""string"",""maxLength"":10},
""secondproperty"":{
""type"":""object"",
""properties"":
{
""secondproperty_firstproperty"":{""type"":""string"",""maxLength"":10,""minLength"":7}
}
}
},
""additionalProperties"":{}
}";
string second = @"{
""id"":""second"",
""type"":""object"",
""extends"":{""$ref"":""first""},
""properties"":
{
""firstproperty"":{""type"":""string""},
""secondproperty"":{
""extends"":{
""properties"":
{
""secondproperty_firstproperty"":{""maxLength"":9,""minLength"":6}
}
},
""type"":""object"",
""properties"":
{
""secondproperty_firstproperty"":{}
}
},
""thirdproperty"":{""type"":""string""}
},
""additionalProperties"":false
}";
JsonSchemaResolver resolver = new JsonSchemaResolver();
JsonSchema firstSchema = JsonSchema.Parse(first, resolver);
JsonSchema secondSchema = JsonSchema.Parse(second, resolver);
JsonSchemaModelBuilder modelBuilder = new JsonSchemaModelBuilder();
JsonSchemaNode node = modelBuilder.AddSchema(null, secondSchema);
Assert.AreEqual(2, node.Schemas.Count);
Assert.AreEqual(2, node.Properties["firstproperty"].Schemas.Count);
Assert.AreEqual(3, node.Properties["secondproperty"].Schemas.Count);
Assert.AreEqual(3, node.Properties["secondproperty"].Properties["secondproperty_firstproperty"].Schemas.Count);
}
示例3: Example
public void Example()
{
#region Usage
string schemaJson;
JsonSchemaResolver resolver = new JsonSchemaResolver();
schemaJson = @"{
'id': 'person',
'type': 'object',
'properties': {
'name': {'type':'string'},
'age': {'type':'integer'}
}
}";
JsonSchema personSchema = JsonSchema.Parse(schemaJson, resolver);
schemaJson = @"{
'id': 'employee',
'type': 'object',
'extends': {'$ref':'person'},
'properties': {
'salary': {'type':'number'},
'jobTitle': {'type':'string'}
}
}";
JsonSchema employeeSchema = JsonSchema.Parse(schemaJson, resolver);
string json = @"{
'name': 'James',
'age': 29,
'salary': 9000.01,
'jobTitle': 'Junior Vice President'
}";
JObject employee = JObject.Parse(json);
bool valid = employee.IsValid(employeeSchema);
Console.WriteLine(valid);
// true
#endregion
}
示例4: InitializeStatics
private static void InitializeStatics(DbContext context)
{
if (!isInitialized)
{
lock (SyncRoot)
{
if (!isInitialized)
{
schemas = context.Set<Schema>().ToList();
sets = context.Set<Set>()
.Include(s => s.Schemas)
.ToList();
setParts = context.Set<SetPart>()
.Include(sp => sp.Schema)
.ToList();
JsonSchemaResolver resolver = new JsonSchemaResolver();
foreach (var set in sets)
{
foreach (var schema in set.Schemas)
{
schema.JsonSchema = JsonSchema.Parse(schema.SchemaText, resolver);
}
}
foreach (var setPart in setParts)
{
if (setPart.Schema != null)
{
setPart.Schema.JsonSchema = JsonSchema.Parse(setPart.Schema.SchemaText, resolver);
}
}
isInitialized = true;
}
}
}
}
示例5: Generate
/// <summary>
/// Generate a <see cref="JsonSchema"/> from the specified type.
/// </summary>
/// <param name="type">The type to generate a <see cref="JsonSchema"/> from.</param>
/// <param name="resolver">The <see cref="JsonSchemaResolver"/> used to resolve schema references.</param>
/// <param name="rootSchemaNullable">Specify whether the generated root <see cref="JsonSchema"/> will be nullable.</param>
/// <returns>A <see cref="JsonSchema"/> generated from the specified type.</returns>
public JsonSchema Generate(Type type, JsonSchemaResolver resolver, bool rootSchemaNullable)
{
ValidationUtils.ArgumentNotNull(type, "type");
ValidationUtils.ArgumentNotNull(resolver, "resolver");
_resolver = resolver;
return GenerateInternal(type, (!rootSchemaNullable) ? Required.Always : Required.Default);
}
示例6: Generate
/// <summary>
/// Generate a <see cref="JsonSchema"/> from the specified type.
/// </summary>
/// <param name="type">The type to generate a <see cref="JsonSchema"/> from.</param>
/// <param name="resolver">The <see cref="JsonSchemaResolver"/> used to resolve schema references.</param>
/// <returns>A <see cref="JsonSchema"/> generated from the specified type.</returns>
public JsonSchema Generate(Type type, JsonSchemaResolver resolver)
{
return Generate(type, resolver, false);
}
示例7: GetExtendedSchema
private JsonSchema GetExtendedSchema()
{
string first = @"{
""id"":""first"",
""type"":""object"",
""properties"":
{
""firstproperty"":{""type"":""string"",""required"":true}
},
""additionalProperties"":{}
}";
string second = @"{
""id"":""second"",
""type"":""object"",
""extends"":{""$ref"":""first""},
""properties"":
{
""secondproperty"":{""type"":""string"",""required"":true}
},
""additionalProperties"":false
}";
JsonSchemaResolver resolver = new JsonSchemaResolver();
JsonSchema firstSchema = JsonSchema.Parse(first, resolver);
JsonSchema secondSchema = JsonSchema.Parse(second, resolver);
return secondSchema;
}
示例8: ExtendedComplex
public void ExtendedComplex()
{
string first = @"{
""id"":""first"",
""type"":""object"",
""properties"":
{
""firstproperty"":{""type"":""string""},
""secondproperty"":{""type"":""string"",""maxLength"":10},
""thirdproperty"":{
""type"":""object"",
""properties"":
{
""thirdproperty_firstproperty"":{""type"":""string"",""maxLength"":10,""minLength"":7}
}
}
},
""additionalProperties"":{}
}";
string second = @"{
""id"":""second"",
""type"":""object"",
""extends"":{""$ref"":""first""},
""properties"":
{
""secondproperty"":{""type"":""any""},
""thirdproperty"":{
""extends"":{
""properties"":
{
""thirdproperty_firstproperty"":{""maxLength"":9,""minLength"":6,""pattern"":""hi2u""}
},
""additionalProperties"":{""maxLength"":9,""minLength"":6,""enum"":[""one"",""two""]}
},
""type"":""object"",
""properties"":
{
""thirdproperty_firstproperty"":{""pattern"":""hi""}
},
""additionalProperties"":{""type"":""string"",""enum"":[""two"",""three""]}
},
""fourthproperty"":{""type"":""string""}
},
""additionalProperties"":false
}";
JsonSchemaResolver resolver = new JsonSchemaResolver();
JsonSchema firstSchema = JsonSchema.Parse(first, resolver);
JsonSchema secondSchema = JsonSchema.Parse(second, resolver);
JsonSchemaModelBuilder modelBuilder = new JsonSchemaModelBuilder();
string json = @"{
'firstproperty':'blahblahblahblahblahblah',
'secondproperty':'secasecasecasecaseca',
'thirdproperty':{
'thirdproperty_firstproperty':'aaa',
'additional':'three'
}
}";
Json.Schema.ValidationEventArgs validationEventArgs = null;
List<string> errors = new List<string>();
JsonValidatingReader reader = new JsonValidatingReader(new JsonTextReader(new StringReader(json)));
reader.ValidationEventHandler += (sender, args) =>
{
validationEventArgs = args;
errors.Add(validationEventArgs.Path + " - " + validationEventArgs.Message);
};
reader.Schema = secondSchema;
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.StartObject, reader.TokenType);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.PropertyName, reader.TokenType);
Assert.AreEqual("firstproperty", reader.Value.ToString());
Assert.AreEqual(null, validationEventArgs);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.String, reader.TokenType);
Assert.AreEqual("blahblahblahblahblahblah", reader.Value.ToString());
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.PropertyName, reader.TokenType);
Assert.AreEqual("secondproperty", reader.Value.ToString());
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.String, reader.TokenType);
Assert.AreEqual("secasecasecasecaseca", reader.Value.ToString());
Assert.AreEqual(1, errors.Count);
Assert.AreEqual("secondproperty - String 'secasecasecasecaseca' exceeds maximum length of 10. Line 3, position 42.", errors[0]);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.PropertyName, reader.TokenType);
Assert.AreEqual("thirdproperty", reader.Value.ToString());
Assert.AreEqual(1, errors.Count);
//.........这里部分代码省略.........
示例9: JsonSchemaBuilder
public JsonSchemaBuilder(JsonSchemaResolver resolver)
{
_stack = new List<JsonSchema>();
_documentSchemas = new Dictionary<string, JsonSchema>();
_resolver = resolver;
}
示例10: Extends
public void Extends()
{
string json;
JsonSchemaResolver resolver = new JsonSchemaResolver();
json = @"{
""id"":""first"",
""type"":""object"",
""additionalProperties"":{}
}";
JsonSchema first = JsonSchema.Parse(json, resolver);
json =
@"{
""id"":""second"",
""type"":""object"",
""extends"":{""$ref"":""first""},
""additionalProperties"":{""type"":""string""}
}";
JsonSchema second = JsonSchema.Parse(json, resolver);
Assert.AreEqual(first, second.Extends);
json =
@"{
""id"":""third"",
""type"":""object"",
""extends"":{""$ref"":""second""},
""additionalProperties"":false
}";
JsonSchema third = JsonSchema.Parse(json, resolver);
Assert.AreEqual(second, third.Extends);
Assert.AreEqual(first, third.Extends.Extends);
StringWriter writer = new StringWriter();
JsonTextWriter jsonWriter = new JsonTextWriter(writer);
jsonWriter.Formatting = Formatting.Indented;
third.WriteTo(jsonWriter, resolver);
string writtenJson = writer.ToString();
Assert.AreEqual(@"{
""id"": ""third"",
""type"": ""object"",
""additionalProperties"": false,
""extends"": {
""$ref"": ""second""
}
}", writtenJson);
StringWriter writer1 = new StringWriter();
JsonTextWriter jsonWriter1 = new JsonTextWriter(writer1);
jsonWriter1.Formatting = Formatting.Indented;
third.WriteTo(jsonWriter1);
writtenJson = writer1.ToString();
Assert.AreEqual(@"{
""id"": ""third"",
""type"": ""object"",
""additionalProperties"": false,
""extends"": {
""id"": ""second"",
""type"": ""object"",
""additionalProperties"": {
""type"": ""string""
},
""extends"": {
""id"": ""first"",
""type"": ""object"",
""additionalProperties"": {}
}
}
}", writtenJson);
}
示例11: Parse
public static JsonSchema Parse(string json, JsonSchemaResolver resolver)
{
ValidationUtils.ArgumentNotNull((object) json, "json");
return JsonSchema.Read((JsonReader) new JsonTextReader((TextReader) new StringReader(json)), resolver);
}
示例12: WriteTo
/// <summary>
/// Writes this schema to a <see cref="JsonWriter"/> using the specified <see cref="JsonSchemaResolver"/>.
/// </summary>
/// <param name="writer">A <see cref="JsonWriter"/> into which this method will write.</param>
/// <param name="resolver">The resolver used.</param>
public void WriteTo(JsonWriter writer, JsonSchemaResolver resolver)
{
ValidationUtils.ArgumentNotNull(writer, "writer");
ValidationUtils.ArgumentNotNull(resolver, "resolver");
JsonSchemaWriter schemaWriter = new JsonSchemaWriter(writer, resolver);
schemaWriter.WriteSchema(this);
}
示例13: Parse
/// <summary>
/// Parses the specified json.
/// </summary>
/// <param name="json">The json.</param>
/// <param name="resolver">The resolver.</param>
/// <returns>A <see cref="JsonSchema"/> populated from the string that contains JSON.</returns>
public static JsonSchema Parse(string json, JsonSchemaResolver resolver)
{
ValidationUtils.ArgumentNotNull(json, "json");
JsonReader reader = new JsonTextReader(new StringReader(json));
return Read(reader, resolver);
}
示例14: JsonSchemaBuilder
public JsonSchemaBuilder(JsonSchemaResolver resolver)
{
_stack = new List<JsonSchema>();
_resolver = resolver;
}
示例15: Parse
// Token: 0x06000B60 RID: 2912
// RVA: 0x00041B78 File Offset: 0x0003FD78
public static JsonSchema Parse(string json, JsonSchemaResolver resolver)
{
ValidationUtils.ArgumentNotNull(json, "json");
JsonSchema result;
using (JsonReader jsonReader = new JsonTextReader(new StringReader(json)))
{
result = JsonSchema.Read(jsonReader, resolver);
}
return result;
}