本文整理汇总了C#中JsonValidatingReader类的典型用法代码示例。如果您正苦于以下问题:C# JsonValidatingReader类的具体用法?C# JsonValidatingReader怎么用?C# JsonValidatingReader使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
JsonValidatingReader类属于命名空间,在下文中一共展示了JsonValidatingReader类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CheckInnerReader
public void CheckInnerReader()
{
string json = "{'name':'James','hobbies':['pie','cake']}";
JsonReader reader = new JsonTextReader(new StringReader(json));
JsonValidatingReader validatingReader = new JsonValidatingReader(reader);
Assert.AreEqual(reader, validatingReader.Reader);
}
示例2: Validate
/// <summary>
/// Validates the specified <see cref="JToken"/>.
/// </summary>
/// <param name="source">The source <see cref="JToken"/> to test.</param>
/// <param name="schema">The schema to test with.</param>
/// <param name="validationEventHandler">The validation event handler.</param>
public static void Validate(this JToken source, JsonSchema schema, ValidationEventHandler validationEventHandler)
{
ValidationUtils.ArgumentNotNull(source, "source");
ValidationUtils.ArgumentNotNull(schema, "schema");
using (JsonValidatingReader reader = new JsonValidatingReader(source.CreateReader()))
{
reader.Schema = schema;
if (validationEventHandler != null)
reader.ValidationEventHandler += validationEventHandler;
while (reader.Read())
{
}
}
}
示例3: Example
public void Example()
{
#region Usage
string schemaJson = @"{
'description': 'A person',
'type': 'object',
'properties': {
'name': {'type':'string'},
'hobbies': {
'type': 'array',
'items': {'type':'string'}
}
}
}";
string json = @"{
'name': 'James',
'hobbies': ['.NET', 'Blogging', 'Reading', 'Xbox', 'LOLCATS']
}";
JsonTextReader reader = new JsonTextReader(new StringReader(json));
JsonValidatingReader validatingReader = new JsonValidatingReader(reader);
validatingReader.Schema = JsonSchema.Parse(schemaJson);
IList<string> messages = new List<string>();
validatingReader.ValidationEventHandler += (o, a) => messages.Add(a.Message);
JsonSerializer serializer = new JsonSerializer();
Person p = serializer.Deserialize<Person>(validatingReader);
Console.WriteLine(p.Name);
// James
bool isValid = (messages.Count == 0);
Console.WriteLine(isValid);
// true
#endregion
}
示例4: ThrowExceptionWhenNoValidationEventHandler
public void ThrowExceptionWhenNoValidationEventHandler()
{
ExceptionAssert.Throws<JsonSchemaException>("Integer 10 exceeds maximum value of 5. Line 1, position 2.",
() =>
{
string schemaJson = @"{
""type"":""integer"",
""maximum"":5
}";
JsonValidatingReader reader = new JsonValidatingReader(new JsonTextReader(new StringReader("10")));
reader.Schema = JsonSchema.Parse(schemaJson);
Assert.IsTrue(reader.Read());
});
}
示例5: IntegerGreaterThanMaximumValue
public void IntegerGreaterThanMaximumValue()
{
string schemaJson = @"{
""type"":""integer"",
""maximum"":5
}";
string json = "10";
Json.Schema.ValidationEventArgs validationEventArgs = null;
JsonValidatingReader reader = new JsonValidatingReader(new JsonTextReader(new StringReader(json)));
reader.ValidationEventHandler += (sender, args) => { validationEventArgs = args; };
reader.Schema = JsonSchema.Parse(schemaJson);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.Integer, reader.TokenType);
Assert.AreEqual("Integer 10 exceeds maximum value of 5. Line 1, position 2.", validationEventArgs.Message);
Assert.AreEqual("", validationEventArgs.Path);
Assert.IsNotNull(validationEventArgs);
}
示例6: DuplicateErrorsTest
//.........这里部分代码省略.........
""Date"":{
""type"":""string"",
""format"":""date-time"",
""required"":true
},
""MACLevels"":{
""type"":""object"",
""required"":true,
""properties"":{
""MACLevel"":{
""type"":""array"",
""required"":true,
""items"":[
{
""required"":true,
""properties"":{
""IDName"":{
""type"":""string"",
""required"":true
},
""Order"":{
""type"":""string"",
""required"":true
},
""IDDesc"":{
""type"":""string"",
""required"":true
},
""IsActive"":{
""type"":""string"",
""required"":true
}
}
}
]
}
}
}
}
}
}
}";
string json = @"{
""ErrorDemoDatabase"":{
""URL"":""localhost:3164"",
""Version"":""1.0"",
""Date"":""6.23.2010, 9:35:18.121"",
""MACLevels"":{
""MACLevel"":[
{
""@IDName"":""Developer"",
""Order"":""0"",
""IDDesc"":""DeveloperDesc"",
""IsActive"":""True""
},
{
""IDName"":""Technician"",
""Order"":""1"",
""IDDesc"":""TechnicianDesc"",
""IsActive"":""True""
},
{
""IDName"":""Administrator"",
""Order"":""2"",
""IDDesc"":""AdministratorDesc"",
""IsActive"":""True""
},
{
""IDName"":""PowerUser"",
""Order"":""3"",
""IDDesc"":""PowerUserDesc"",
""IsActive"":""True""
},
{
""IDName"":""Operator"",
""Order"":""4"",
""IDDesc"":""OperatorDesc"",
""IsActive"":""True""
}
]
}
}
}";
IList<ValidationEventArgs> validationEventArgs = new List<ValidationEventArgs>();
JsonValidatingReader reader = new JsonValidatingReader(new JsonTextReader(new StringReader(json)));
reader.ValidationEventHandler += (sender, args) =>
{
validationEventArgs.Add(args);
};
reader.Schema = JsonSchema.Parse(schema);
while (reader.Read())
{
}
Assert.AreEqual(1, validationEventArgs.Count);
}
示例7: MissingNonRequiredProperties
public void MissingNonRequiredProperties()
{
string schemaJson = @"{
""description"":""A person"",
""type"":""object"",
""properties"":
{
""name"":{""type"":""string"",""required"":true},
""hobbies"":{""type"":""string"",""required"":false},
""age"":{""type"":""integer""}
}
}";
string json = "{'name':'James'}";
Json.Schema.ValidationEventArgs validationEventArgs = null;
JsonValidatingReader reader = new JsonValidatingReader(new JsonTextReader(new StringReader(json)));
reader.ValidationEventHandler += (sender, args) => { validationEventArgs = args; };
reader.Schema = JsonSchema.Parse(schemaJson);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.StartObject, reader.TokenType);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.PropertyName, reader.TokenType);
Assert.AreEqual("name", reader.Value.ToString());
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.String, reader.TokenType);
Assert.AreEqual("James", reader.Value.ToString());
Assert.IsNull(validationEventArgs);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.EndObject, reader.TokenType);
Assert.IsNull(validationEventArgs);
}
示例8: InvalidDataType
public void InvalidDataType()
{
string schemaJson = @"{
""type"":""string"",
""minItems"":2,
""maxItems"":3,
""items"":{}
}";
string json = "[null,null,null,null]";
Json.Schema.ValidationEventArgs validationEventArgs = null;
JsonValidatingReader reader = new JsonValidatingReader(new JsonTextReader(new StringReader(json)));
reader.ValidationEventHandler += (sender, args) => { validationEventArgs = args; };
reader.Schema = JsonSchema.Parse(schemaJson);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.StartArray, reader.TokenType);
Assert.AreEqual(@"Invalid type. Expected String but got Array. Line 1, position 1.", validationEventArgs.Message);
Assert.IsNotNull(validationEventArgs);
}
示例9: BooleanNotInEnum
public void BooleanNotInEnum()
{
string schemaJson = @"{
""type"":""array"",
""items"":{
""type"":""boolean"",
""enum"":[true]
},
""maxItems"":3
}";
string json = "[true,false]";
Json.Schema.ValidationEventArgs validationEventArgs = null;
JsonValidatingReader reader = new JsonValidatingReader(new JsonTextReader(new StringReader(json)));
reader.ValidationEventHandler += (sender, args) => { validationEventArgs = args; };
reader.Schema = JsonSchema.Parse(schemaJson);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.StartArray, reader.TokenType);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.Boolean, reader.TokenType);
Assert.AreEqual(null, validationEventArgs);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.Boolean, reader.TokenType);
Assert.AreEqual(@"Value false is not defined in enum. Line 1, position 11.", validationEventArgs.Message);
Assert.AreEqual("[1]", validationEventArgs.Path);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.EndArray, reader.TokenType);
Assert.IsNotNull(validationEventArgs);
}
示例10: FloatExceedsMaxDecimalPlaces
public void FloatExceedsMaxDecimalPlaces()
{
string schemaJson = @"{
""type"":""array"",
""items"":{
""type"":""number"",
""divisibleBy"":0.1
}
}";
string json = "[1.1,2.2,4.001]";
Json.Schema.ValidationEventArgs validationEventArgs = null;
JsonValidatingReader reader = new JsonValidatingReader(new JsonTextReader(new StringReader(json)));
reader.ValidationEventHandler += (sender, args) => { validationEventArgs = args; };
reader.Schema = JsonSchema.Parse(schemaJson);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.StartArray, reader.TokenType);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.Float, reader.TokenType);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.Float, reader.TokenType);
Assert.AreEqual(null, validationEventArgs);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.Float, reader.TokenType);
Assert.AreEqual(@"Float 4.001 is not evenly divisible by 0.1. Line 1, position 14.", validationEventArgs.Message);
Assert.AreEqual("[2]", validationEventArgs.Path);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.EndArray, reader.TokenType);
Assert.IsNotNull(validationEventArgs);
}
示例11: ReadAsInt32InArray
public void ReadAsInt32InArray()
{
string schemaJson = @"{
""type"":""array"",
""items"":{
""type"":""integer""
},
""maxItems"":1
}";
string json = "[1,2]";
Json.Schema.ValidationEventArgs validationEventArgs = null;
JsonValidatingReader reader = new JsonValidatingReader(new JsonTextReader(new StringReader(json)));
reader.ValidationEventHandler += (sender, args) => { validationEventArgs = args; };
reader.Schema = JsonSchema.Parse(schemaJson);
reader.Read();
Assert.AreEqual(JsonToken.StartArray, reader.TokenType);
reader.ReadAsInt32();
Assert.AreEqual(JsonToken.Integer, reader.TokenType);
Assert.AreEqual(null, validationEventArgs);
reader.ReadAsInt32();
Assert.AreEqual(JsonToken.Integer, reader.TokenType);
Assert.AreEqual(null, validationEventArgs);
reader.ReadAsInt32();
Assert.AreEqual(JsonToken.EndArray, reader.TokenType);
Assert.AreEqual("Array item count 2 exceeds maximum count of 1. Line 1, position 5.", validationEventArgs.Message);
Assert.AreEqual("", validationEventArgs.Path);
}
示例12: ReadAsInt32FromSerializer
public void ReadAsInt32FromSerializer()
{
JsonValidatingReader reader = new JsonValidatingReader(new JsonTextReader(new StringReader("[1,2,3]")));
reader.Schema = new JsonSchemaGenerator().Generate(typeof(int[]));
int[] values = new JsonSerializer().Deserialize<int[]>(reader);
Assert.AreEqual(3, values.Length);
Assert.AreEqual(1, values[0]);
Assert.AreEqual(2, values[1]);
Assert.AreEqual(3, values[2]);
}
示例13: ReadAsDecimalFailure
public void ReadAsDecimalFailure()
{
ExceptionAssert.Throws<JsonSchemaException>("Float 5.5 is not evenly divisible by 1. Line 1, position 3.",
() =>
{
JsonSchema s = new JsonSchemaGenerator().Generate(typeof(decimal));
s.DivisibleBy = 1;
JsonReader reader = new JsonValidatingReader(new JsonTextReader(new StringReader(@"5.5")))
{
Schema = s
};
reader.ReadAsDecimal();
});
}
示例14: ReadAsDecimal
public void ReadAsDecimal()
{
JsonSchema s = new JsonSchemaGenerator().Generate(typeof (decimal));
JsonReader reader = new JsonValidatingReader(new JsonTextReader(new StringReader(@"1.5")))
{
Schema = s
};
decimal? d = reader.ReadAsDecimal();
Assert.AreEqual(1.5m, d);
}
示例15: ReadAsInt32Failure
public void ReadAsInt32Failure()
{
ExceptionAssert.Throws<JsonSchemaException>("Integer 5 exceeds maximum value of 2. Line 1, position 1.",
() =>
{
JsonSchema s = new JsonSchemaGenerator().Generate(typeof(int));
s.Maximum = 2;
JsonReader reader = new JsonValidatingReader(new JsonTextReader(new StringReader(@"5")))
{
Schema = s
};
reader.ReadAsInt32();
});
}