本文整理汇总了C#中JsonValidatingReader类的典型用法代码示例。如果您正苦于以下问题:C# JsonValidatingReader类的具体用法?C# JsonValidatingReader怎么用?C# JsonValidatingReader使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
JsonValidatingReader类属于命名空间,在下文中一共展示了JsonValidatingReader类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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);
}
开发者ID:bladefist,项目名称:Newtonsoft.Json,代码行数:8,代码来源:JsonValidatingReaderTests.cs
示例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())
{
}
}
}
开发者ID:ThePiachu,项目名称:BitNet,代码行数:22,代码来源:Extensions.cs
示例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
}
开发者ID:GorelH,项目名称:ravendb,代码行数:40,代码来源:JsonValidatingReaderAndSerializer.cs
示例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());
});
}
开发者ID:bladefist,项目名称:Newtonsoft.Json,代码行数:16,代码来源:JsonValidatingReaderTests.cs
示例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);
}
开发者ID:bladefist,项目名称:Newtonsoft.Json,代码行数:22,代码来源:JsonValidatingReaderTests.cs
示例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);
}
开发者ID:bladefist,项目名称:Newtonsoft.Json,代码行数:101,代码来源:JsonValidatingReaderTests.cs
示例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);
}
开发者ID:bladefist,项目名称:Newtonsoft.Json,代码行数:38,代码来源:JsonValidatingReaderTests.cs
示例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);
}
开发者ID:bladefist,项目名称:Newtonsoft.Json,代码行数:23,代码来源:JsonValidatingReaderTests.cs
示例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);
}
开发者ID:bladefist,项目名称:Newtonsoft.Json,代码行数:36,代码来源:JsonValidatingReaderTests.cs
示例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);
}
开发者ID:bladefist,项目名称:Newtonsoft.Json,代码行数:38,代码来源:JsonValidatingReaderTests.cs
示例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);
}
开发者ID:bladefist,项目名称:Newtonsoft.Json,代码行数:34,代码来源:JsonValidatingReaderTests.cs
示例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]);
}
开发者ID:bladefist,项目名称:Newtonsoft.Json,代码行数:11,代码来源:JsonValidatingReaderTests.cs
示例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();
});
}
开发者ID:bladefist,项目名称:Newtonsoft.Json,代码行数:15,代码来源:JsonValidatingReaderTests.cs
示例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);
}
开发者ID:bladefist,项目名称:Newtonsoft.Json,代码行数:12,代码来源:JsonValidatingReaderTests.cs
示例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();
});
}
开发者ID:bladefist,项目名称:Newtonsoft.Json,代码行数:15,代码来源:JsonValidatingReaderTests.cs
示例16: ReadAsInt32
public void ReadAsInt32()
{
JsonSchema s = new JsonSchemaGenerator().Generate(typeof (int));
JsonReader reader = new JsonValidatingReader(new JsonTextReader(new StringReader(@"1")))
{
Schema = s
};
int? i = reader.ReadAsInt32();
Assert.AreEqual(1, i);
}
开发者ID:bladefist,项目名称:Newtonsoft.Json,代码行数:12,代码来源:JsonValidatingReaderTests.cs
示例17: ReadAsBytes
public void ReadAsBytes()
{
JsonSchema s = new JsonSchemaGenerator().Generate(typeof (byte[]));
byte[] data = Encoding.UTF8.GetBytes("Hello world");
JsonReader reader = new JsonValidatingReader(new JsonTextReader(new StringReader(@"""" + Convert.ToBase64String(data) + @"""")))
{
Schema = s
};
byte[] bytes = reader.ReadAsBytes();
CollectionAssert.AreEquivalent(data, bytes);
}
开发者ID:bladefist,项目名称:Newtonsoft.Json,代码行数:14,代码来源:JsonValidatingReaderTests.cs
示例18: ValidateTypes
public void ValidateTypes()
{
string schemaJson = @"{
""description"":""A person"",
""type"":""object"",
""properties"":
{
""name"":{""type"":""string""},
""hobbies"":
{
""type"":""array"",
""items"": {""type"":""string""}
}
}
}";
string json = @"{'name':""James"",'hobbies':[""pie"",'cake']}";
Json.Schema.ValidationEventArgs validationEventArgs = null;
JsonValidatingReader reader = new JsonValidatingReader(new JsonTextReader(new StringReader(json)));
reader.ValidationEventHandler += (sender, args) => { validationEventArgs = args; };
JsonSchema schema = JsonSchema.Parse(schemaJson);
reader.Schema = schema;
Assert.AreEqual(schema, reader.Schema);
Assert.AreEqual(0, reader.Depth);
Assert.AreEqual("", reader.Path);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.StartObject, reader.TokenType);
Assert.AreEqual("", reader.Path);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.PropertyName, reader.TokenType);
Assert.AreEqual("name", reader.Value.ToString());
Assert.AreEqual("name", reader.Path);
Assert.AreEqual(1, reader.Depth);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.String, reader.TokenType);
Assert.AreEqual("James", reader.Value.ToString());
Assert.AreEqual(typeof (string), reader.ValueType);
Assert.AreEqual('"', reader.QuoteChar);
Assert.AreEqual("name", reader.Path);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.PropertyName, reader.TokenType);
Assert.AreEqual("hobbies", reader.Value.ToString());
Assert.AreEqual('\'', reader.QuoteChar);
Assert.AreEqual("hobbies", reader.Path);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.StartArray, reader.TokenType);
Assert.AreEqual("hobbies", reader.Path);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.String, reader.TokenType);
Assert.AreEqual("pie", reader.Value.ToString());
Assert.AreEqual('"', reader.QuoteChar);
Assert.AreEqual("hobbies[0]", reader.Path);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.String, reader.TokenType);
Assert.AreEqual("cake", reader.Value.ToString());
Assert.AreEqual("hobbies[1]", reader.Path);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.EndArray, reader.TokenType);
Assert.AreEqual("hobbies", reader.Path);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.EndObject, reader.TokenType);
Assert.AreEqual("", reader.Path);
Assert.IsFalse(reader.Read());
Assert.IsNull(validationEventArgs);
}
开发者ID:bladefist,项目名称:Newtonsoft.Json,代码行数:78,代码来源:JsonValidatingReaderTests.cs
示例19: FloatLessThanMinimumValue
public void FloatLessThanMinimumValue()
{
string schemaJson = @"{
""type"":""number"",
""minimum"":5
}";
string json = "1.1";
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.Float, reader.TokenType);
Assert.AreEqual("Float 1.1 is less than minimum value of 5. Line 1, position 3.", validationEventArgs.Message);
Assert.IsNotNull(validationEventArgs);
}
开发者ID:bladefist,项目名称:Newtonsoft.Json,代码行数:21,代码来源:JsonValidatingReaderTests.cs
示例20: ReadAsInt32InArrayIncomplete
public void ReadAsInt32InArrayIncomplete()
{
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.None, reader.TokenType);
Assert.AreEqual(null, validationEventArgs);
}
开发者ID:bladefist,项目名称:Newtonsoft.Json,代码行数:33,代码来源:JsonValidatingReaderTests.cs
注:本文中的JsonValidatingReader类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论