本文整理汇总了C#中Newtonsoft.Json.Schema.JsonSchemaModelBuilder类的典型用法代码示例。如果您正苦于以下问题:C# JsonSchemaModelBuilder类的具体用法?C# JsonSchemaModelBuilder怎么用?C# JsonSchemaModelBuilder使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
JsonSchemaModelBuilder类属于Newtonsoft.Json.Schema命名空间,在下文中一共展示了JsonSchemaModelBuilder类的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: 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);
}
示例2: ValidateCurrentToken
private void ValidateCurrentToken()
{
// first time validate has been called. build model
if (_model == null)
{
JsonSchemaModelBuilder builder = new JsonSchemaModelBuilder();
_model = builder.Build(_schema);
if (!JsonTokenUtils.IsStartToken(_reader.TokenType))
Push(new SchemaScope(JTokenType.None, CurrentMemberSchemas));
}
switch (_reader.TokenType)
{
case JsonToken.StartObject:
ProcessValue();
IList<JsonSchemaModel> objectSchemas = CurrentMemberSchemas.Where(ValidateObject).ToList();
Push(new SchemaScope(JTokenType.Object, objectSchemas));
WriteToken(CurrentSchemas);
break;
case JsonToken.StartArray:
ProcessValue();
IList<JsonSchemaModel> arraySchemas = CurrentMemberSchemas.Where(ValidateArray).ToList();
Push(new SchemaScope(JTokenType.Array, arraySchemas));
WriteToken(CurrentSchemas);
break;
case JsonToken.StartConstructor:
ProcessValue();
Push(new SchemaScope(JTokenType.Constructor, null));
WriteToken(CurrentSchemas);
break;
case JsonToken.PropertyName:
WriteToken(CurrentSchemas);
foreach (JsonSchemaModel schema in CurrentSchemas)
{
ValidatePropertyName(schema);
}
break;
case JsonToken.Raw:
ProcessValue();
break;
case JsonToken.Integer:
ProcessValue();
WriteToken(CurrentMemberSchemas);
foreach (JsonSchemaModel schema in CurrentMemberSchemas)
{
ValidateInteger(schema);
}
break;
case JsonToken.Float:
ProcessValue();
WriteToken(CurrentMemberSchemas);
foreach (JsonSchemaModel schema in CurrentMemberSchemas)
{
ValidateFloat(schema);
}
break;
case JsonToken.String:
ProcessValue();
WriteToken(CurrentMemberSchemas);
foreach (JsonSchemaModel schema in CurrentMemberSchemas)
{
ValidateString(schema);
}
break;
case JsonToken.Boolean:
ProcessValue();
WriteToken(CurrentMemberSchemas);
foreach (JsonSchemaModel schema in CurrentMemberSchemas)
{
ValidateBoolean(schema);
}
break;
case JsonToken.Null:
ProcessValue();
WriteToken(CurrentMemberSchemas);
foreach (JsonSchemaModel schema in CurrentMemberSchemas)
{
ValidateNull(schema);
}
break;
case JsonToken.EndObject:
WriteToken(CurrentSchemas);
foreach (JsonSchemaModel schema in CurrentSchemas)
{
ValidateEndObject(schema);
}
Pop();
break;
case JsonToken.EndArray:
WriteToken(CurrentSchemas);
foreach (JsonSchemaModel schema in CurrentSchemas)
{
ValidateEndArray(schema);
}
Pop();
break;
case JsonToken.EndConstructor:
WriteToken(CurrentSchemas);
Pop();
//.........这里部分代码省略.........
示例3: 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);
//.........这里部分代码省略.........
示例4: ValidateCurrentToken
private void ValidateCurrentToken()
{
// first time validate has been called. build model
if (_model == null)
{
JsonSchemaModelBuilder builder = new JsonSchemaModelBuilder();
_model = builder.Build(_schema);
}
switch (_reader.TokenType)
{
case JsonToken.StartObject:
ProcessValue();
JsonSchemaModel objectSchema = (ValidateObject(CurrentMemberSchema))
? CurrentMemberSchema
: null;
Push(new SchemaScope(JTokenType.Object, objectSchema));
break;
case JsonToken.StartArray:
ProcessValue();
JsonSchemaModel arraySchema = (ValidateArray(CurrentMemberSchema))
? CurrentMemberSchema
: null;
Push(new SchemaScope(JTokenType.Array, arraySchema));
break;
case JsonToken.StartConstructor:
Push(new SchemaScope(JTokenType.Constructor, null));
break;
case JsonToken.PropertyName:
ValidatePropertyName(CurrentSchema);
break;
case JsonToken.Raw:
break;
case JsonToken.Integer:
ProcessValue();
ValidateInteger(CurrentMemberSchema);
break;
case JsonToken.Float:
ProcessValue();
ValidateFloat(CurrentMemberSchema);
break;
case JsonToken.String:
ProcessValue();
ValidateString(CurrentMemberSchema);
break;
case JsonToken.Boolean:
ProcessValue();
ValidateBoolean(CurrentMemberSchema);
break;
case JsonToken.Null:
ProcessValue();
ValidateNull(CurrentMemberSchema);
break;
case JsonToken.Undefined:
break;
case JsonToken.EndObject:
ValidateEndObject(CurrentSchema);
Pop();
break;
case JsonToken.EndArray:
ValidateEndArray(CurrentSchema);
Pop();
break;
case JsonToken.EndConstructor:
Pop();
break;
case JsonToken.Date:
break;
default:
throw new ArgumentOutOfRangeException();
}
}
示例5: 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();
JsonSchemaModel model = modelBuilder.Build(secondSchema);
Assert.AreEqual(4, model.Properties.Count);
Assert.AreEqual(JsonSchemaType.String, model.Properties["firstproperty"].Type);
Assert.AreEqual(JsonSchemaType.String, model.Properties["secondproperty"].Type);
Assert.AreEqual(10, model.Properties["secondproperty"].MaximumLength);
Assert.AreEqual(null, model.Properties["secondproperty"].Enum);
Assert.AreEqual(null, model.Properties["secondproperty"].Patterns);
Assert.AreEqual(JsonSchemaType.Object, model.Properties["thirdproperty"].Type);
Assert.AreEqual(3, model.Properties["thirdproperty"].AdditionalProperties.Enum.Count);
Assert.AreEqual("two", (string)model.Properties["thirdproperty"].AdditionalProperties.Enum[0]);
Assert.AreEqual("three", (string)model.Properties["thirdproperty"].AdditionalProperties.Enum[1]);
Assert.AreEqual("one", (string)model.Properties["thirdproperty"].AdditionalProperties.Enum[2]);
Assert.AreEqual(JsonSchemaType.String, model.Properties["thirdproperty"].Properties["thirdproperty_firstproperty"].Type);
Assert.AreEqual(9, model.Properties["thirdproperty"].Properties["thirdproperty_firstproperty"].MaximumLength);
Assert.AreEqual(7, model.Properties["thirdproperty"].Properties["thirdproperty_firstproperty"].MinimumLength);
Assert.AreEqual(2, model.Properties["thirdproperty"].Properties["thirdproperty_firstproperty"].Patterns.Count);
Assert.AreEqual("hi", model.Properties["thirdproperty"].Properties["thirdproperty_firstproperty"].Patterns[0]);
Assert.AreEqual("hi2u", model.Properties["thirdproperty"].Properties["thirdproperty_firstproperty"].Patterns[1]);
Assert.AreEqual(null, model.Properties["thirdproperty"].Properties["thirdproperty_firstproperty"].Properties);
Assert.AreEqual(null, model.Properties["thirdproperty"].Properties["thirdproperty_firstproperty"].Items);
Assert.AreEqual(null, model.Properties["thirdproperty"].Properties["thirdproperty_firstproperty"].AdditionalProperties);
}
示例6: Required
public void Required()
{
string schemaJson = @"{
""description"":""A person"",
""type"":""object"",
""properties"":
{
""name"":{""type"":""string""},
""hobbies"":{""type"":""string"",required:true},
""age"":{""type"":""integer"",required:true}
}
}";
JsonSchema schema = JsonSchema.Parse(schemaJson);
JsonSchemaModelBuilder modelBuilder = new JsonSchemaModelBuilder();
JsonSchemaModel model = modelBuilder.Build(schema);
Assert.AreEqual(JsonSchemaType.Object, model.Type);
Assert.AreEqual(3, model.Properties.Count);
Assert.AreEqual(false, model.Properties["name"].Required);
Assert.AreEqual(true, model.Properties["hobbies"].Required);
Assert.AreEqual(true, model.Properties["age"].Required);
}
示例7: CircularReference
public void CircularReference()
{
string json = @"{
""id"":""CircularReferenceArray"",
""description"":""CircularReference"",
""type"":[""array""],
""items"":{""$ref"":""CircularReferenceArray""}
}";
JsonSchema schema = JsonSchema.Parse(json);
JsonSchemaModelBuilder modelBuilder = new JsonSchemaModelBuilder();
JsonSchemaModel model = modelBuilder.Build(schema);
Assert.AreEqual(JsonSchemaType.Array, model.Type);
Assert.AreEqual(model, model.Items[0]);
}
示例8: CircularReference
public void CircularReference()
{
string json = @"{
""id"":""CircularReferenceArray"",
""description"":""CircularReference"",
""type"":[""array""],
""items"":{""$ref"":""CircularReferenceArray""}
}";
JsonSchema schema = JsonSchema.Parse(json);
JsonSchemaModelBuilder modelBuilder = new JsonSchemaModelBuilder();
JsonSchemaNode node = modelBuilder.AddSchema(null, schema);
Assert.AreEqual(1, node.Schemas.Count);
Assert.AreEqual(node, node.Items[0]);
}
示例9: ValidateCurrentToken
private void ValidateCurrentToken()
{
// first time validate has been called. build model
if (_model == null)
{
JsonSchemaModelBuilder builder = new JsonSchemaModelBuilder();
_model = builder.Build(_schema);
}
//ValidateValueToken();
switch (_reader.TokenType)
{
case JsonToken.StartObject:
ProcessValue();
IList<JsonSchemaModel> objectSchemas = CurrentMemberSchemas.Where(ValidateObject).ToList();
Push(new SchemaScope(JTokenType.Object, objectSchemas));
break;
case JsonToken.StartArray:
ProcessValue();
IList<JsonSchemaModel> arraySchemas = CurrentMemberSchemas.Where(ValidateArray).ToList();
Push(new SchemaScope(JTokenType.Array, arraySchemas));
break;
case JsonToken.StartConstructor:
Push(new SchemaScope(JTokenType.Constructor, null));
break;
case JsonToken.PropertyName:
foreach (JsonSchemaModel schema in CurrentSchemas)
{
ValidatePropertyName(schema);
}
break;
case JsonToken.Raw:
break;
case JsonToken.Integer:
ProcessValue();
foreach (JsonSchemaModel schema in CurrentMemberSchemas)
{
ValidateInteger(schema);
}
break;
case JsonToken.Float:
ProcessValue();
foreach (JsonSchemaModel schema in CurrentMemberSchemas)
{
ValidateFloat(schema);
}
break;
case JsonToken.String:
ProcessValue();
foreach (JsonSchemaModel schema in CurrentMemberSchemas)
{
ValidateString(schema);
}
break;
case JsonToken.Boolean:
ProcessValue();
foreach (JsonSchemaModel schema in CurrentMemberSchemas)
{
ValidateBoolean(schema);
}
break;
case JsonToken.Null:
ProcessValue();
foreach (JsonSchemaModel schema in CurrentMemberSchemas)
{
ValidateNull(schema);
}
break;
case JsonToken.Undefined:
break;
case JsonToken.EndObject:
foreach (JsonSchemaModel schema in CurrentSchemas)
{
ValidateEndObject(schema);
}
Pop();
break;
case JsonToken.EndArray:
foreach (JsonSchemaModel schema in CurrentSchemas)
{
ValidateEndArray(schema);
}
Pop();
break;
case JsonToken.EndConstructor:
Pop();
break;
case JsonToken.Date:
break;
default:
throw new ArgumentOutOfRangeException();
}
}
示例10: ValidateCurrentToken
// Token: 0x0600064A RID: 1610
// RVA: 0x000363C0 File Offset: 0x000345C0
private void ValidateCurrentToken()
{
if (this._model == null)
{
JsonSchemaModelBuilder jsonSchemaModelBuilder = new JsonSchemaModelBuilder();
this._model = jsonSchemaModelBuilder.Build(this._schema);
if (!JsonTokenUtils.IsStartToken(this._reader.TokenType))
{
this.Push(new JsonValidatingReader.SchemaScope(JTokenType.None, this.CurrentMemberSchemas));
}
}
switch (this._reader.TokenType)
{
case JsonToken.None:
return;
case JsonToken.StartObject:
{
this.ProcessValue();
IList<JsonSchemaModel> schemas = Enumerable.ToList<JsonSchemaModel>(Enumerable.Where<JsonSchemaModel>(this.CurrentMemberSchemas, new Func<JsonSchemaModel, bool>(this.ValidateObject)));
this.Push(new JsonValidatingReader.SchemaScope(JTokenType.Object, schemas));
this.WriteToken(this.CurrentSchemas);
return;
}
case JsonToken.StartArray:
{
this.ProcessValue();
IList<JsonSchemaModel> schemas2 = Enumerable.ToList<JsonSchemaModel>(Enumerable.Where<JsonSchemaModel>(this.CurrentMemberSchemas, new Func<JsonSchemaModel, bool>(this.ValidateArray)));
this.Push(new JsonValidatingReader.SchemaScope(JTokenType.Array, schemas2));
this.WriteToken(this.CurrentSchemas);
return;
}
case JsonToken.StartConstructor:
this.ProcessValue();
this.Push(new JsonValidatingReader.SchemaScope(JTokenType.Constructor, null));
this.WriteToken(this.CurrentSchemas);
return;
case JsonToken.PropertyName:
this.WriteToken(this.CurrentSchemas);
using (IEnumerator<JsonSchemaModel> enumerator = this.CurrentSchemas.GetEnumerator())
{
while (enumerator.MoveNext())
{
JsonSchemaModel current = enumerator.Current;
this.ValidatePropertyName(current);
}
return;
}
break;
case JsonToken.Raw:
this.ProcessValue();
return;
case JsonToken.Integer:
this.ProcessValue();
this.WriteToken(this.CurrentMemberSchemas);
using (IEnumerator<JsonSchemaModel> enumerator2 = this.CurrentMemberSchemas.GetEnumerator())
{
while (enumerator2.MoveNext())
{
JsonSchemaModel current2 = enumerator2.Current;
this.ValidateInteger(current2);
}
return;
}
goto IL_1DE;
case JsonToken.Float:
goto IL_1DE;
case JsonToken.String:
goto IL_22B;
case JsonToken.Boolean:
goto IL_278;
case JsonToken.Null:
goto IL_2C2;
case JsonToken.Undefined:
case JsonToken.Date:
case JsonToken.Bytes:
this.WriteToken(this.CurrentMemberSchemas);
return;
case JsonToken.EndObject:
this.WriteToken(this.CurrentSchemas);
foreach (JsonSchemaModel current3 in this.CurrentSchemas)
{
this.ValidateEndObject(current3);
}
this.Pop();
return;
case JsonToken.EndArray:
this.WriteToken(this.CurrentSchemas);
foreach (JsonSchemaModel current4 in this.CurrentSchemas)
{
this.ValidateEndArray(current4);
}
this.Pop();
return;
case JsonToken.EndConstructor:
this.WriteToken(this.CurrentSchemas);
this.Pop();
return;
}
//.........这里部分代码省略.........
示例11: Required
public void Required()
{
string schemaJson = @"{
""description"":""A person"",
""type"":""object"",
""required"": [ ""hobbies"", ""age"" ],
""properties"":
{
""name"":{""type"":""string""},
""hobbies"":{""type"":""string""},
""age"":{""type"":""integer""}
}
}";
JsonSchema schema = JsonSchema.Parse(schemaJson);
JsonSchemaModelBuilder modelBuilder = new JsonSchemaModelBuilder();
JsonSchemaModel model = modelBuilder.Build(schema);
Assert.AreEqual(JsonSchemaType.Object, model.Type);
Assert.AreEqual(3, model.Properties.Count);
Assert.IsFalse(model.Required.Contains("name"));
Assert.IsTrue(model.Required.Contains("hobbies"));
Assert.IsTrue(model.Required.Contains("age"));
}