本文整理汇总了C#中Newtonsoft.Json.Schema.JSchemaValidatingReader类的典型用法代码示例。如果您正苦于以下问题:C# JSchemaValidatingReader类的具体用法?C# JSchemaValidatingReader怎么用?C# JSchemaValidatingReader使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
JSchemaValidatingReader类属于Newtonsoft.Json.Schema命名空间,在下文中一共展示了JSchemaValidatingReader类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: AdheresToSchema
public JsonBrowserResponse AdheresToSchema([NotNull] JSchema schema)
{
var validatingReader = new JSchemaValidatingReader(new JsonTextReader(new StringReader(ResponseBody)));
validatingReader.Schema = schema;
var validationErrors = new List<SchemaValidationEventArgs>();
validatingReader.ValidationEventHandler += (sender, args) => validationErrors.Add(args);
var serializer = new JsonSerializer();
try
{
serializer.Deserialize(validatingReader);
}
catch (JsonReaderException)
{
Assert.True(false, "The response body did not contain JSON.");
}
if (!validationErrors.Any())
{
return this;
}
var messages = validationErrors.Select(e => e.Message).ToList();
var message = string.Join(Environment.NewLine, messages);
Assert.True(
false,
$"Encountered validation errors against schema: {message}.{Environment.NewLine}Response body: {ResponseBody}");
return this;
}
示例2: ValidateStream
public void ValidateStream()
{
#region ValidateStream
string schemaJson = @"{
'description': 'A person',
'type': 'object',
'properties': {
'name': {'type': 'string'},
'hobbies': {
'type': 'array',
'items': {'type': 'string'}
}
}
}";
JSchema schema = JSchema.Parse(schemaJson);
using (StreamReader s = File.OpenText(@"c:\bigdata.json"))
using (JSchemaValidatingReader reader = new JSchemaValidatingReader(new JsonTextReader(s)))
{
// assign schema and setup event handler
reader.Schema = schema;
reader.ValidationEventHandler += (sender, args) =>
{
Console.WriteLine(args.Message);
};
// bigdata.json will be validated without loading the entire document into memory
while (reader.Read())
{
}
}
#endregion
}
示例3: ReadSpecTest
public void ReadSpecTest(SchemaSpecTest schemaSpecTest)
{
Console.WriteLine("Running reader JSON Schema {0} test {1}: {2}", schemaSpecTest.Version, schemaSpecTest.TestNumber, schemaSpecTest);
IList<string> errorMessages = new List<string>();
JSchemaPreloadedResolver resolver = GetResolver();
JSchema s = JSchema.Load(schemaSpecTest.Schema.CreateReader(), resolver);
JsonReader jsonReader = schemaSpecTest.Data.CreateReader();
using (JSchemaValidatingReader reader = new JSchemaValidatingReader(jsonReader))
{
reader.Schema = s;
reader.ValidationEventHandler += (sender, args) => errorMessages.Add(args.Message);
while (reader.Read())
{
}
}
bool isValid = (errorMessages.Count == 0);
Assert.AreEqual(schemaSpecTest.IsValid, isValid, schemaSpecTest.TestCaseDescription + " - " + schemaSpecTest.TestDescription + " - errors: " + StringHelpers.Join(", ", errorMessages));
}
示例4: GetByName
public MapInfo GetByName(string name)
{
var path = Path.Combine(_directory, name);
Map map;
string comments;
using (var file = File.OpenRead(path + ".json"))
{
var reader = new JSchemaValidatingReader(new JsonTextReader(new StreamReader(file)))
{
Schema = _schema
};
map = _json.Deserialize<Map>(reader);
}
try
{
using (var file = File.OpenRead(path + ".txt"))
{
comments = new StreamReader(file).ReadToEnd().TrimEnd();
}
}
catch (FileNotFoundException)
{
comments = null;
}
return new MapInfo(name, name + ".pie", comments, map);
}
示例5: LoadConfiguration
public static void LoadConfiguration(string configFile)
{
JsonTextReader reader = new JsonTextReader(File.OpenText(configFile));
JSchemaValidatingReader validatingReader = new JSchemaValidatingReader(reader);
JSchemaGenerator generator = new JSchemaGenerator();
validatingReader.Schema = generator.Generate(typeof(ConfigurationJson));
JsonSerializer serializer = new JsonSerializer();
configJson = serializer.Deserialize<ConfigurationJson>(validatingReader);
ConfigFileName = configFile;
}
示例6: CreateReader
private JSchemaValidatingReader CreateReader(string json, JSchema schema, out IList<SchemaValidationEventArgs> errors)
{
JsonReader reader = new JsonTextReader(new StringReader(json));
List<SchemaValidationEventArgs> localErrors = new List<SchemaValidationEventArgs>();
JSchemaValidatingReader validatingReader = new JSchemaValidatingReader(reader);
validatingReader.ValidationEventHandler += (sender, args) => { localErrors.Add(args); };
validatingReader.Schema = schema;
errors = localErrors;
return validatingReader;
}
示例7: Sample
public void Sample()
{
JSchema schema = JSchema.Parse(@"{
'type': 'array',
'items': {'type':'string'}
}");
JsonTextReader reader = new JsonTextReader(new StringReader(@"[
'Developer',
'Administrator'
]"));
JSchemaValidatingReader validatingReader = new JSchemaValidatingReader(reader);
validatingReader.Schema = schema;
validatingReader.ValidationEventHandler += (sender, args) => { throw new Exception(args.Message); };
JsonSerializer serializer = new JsonSerializer();
List<string> roles = serializer.Deserialize<List<string>>(validatingReader);
}
示例8: IntegerLessThanMinimumValue
public void IntegerLessThanMinimumValue()
{
string schemaJson = @"{
""type"":""integer"",
""minimum"":5
}";
string json = "1";
SchemaValidationEventArgs validationEventArgs = null;
JSchemaValidatingReader reader = new JSchemaValidatingReader(new JsonTextReader(new StringReader(json)));
reader.ValidationEventHandler += (sender, args) => { validationEventArgs = args; };
reader.Schema = JSchema.Parse(schemaJson);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.Integer, reader.TokenType);
Assert.AreEqual("Integer 1 is less than minimum value of 5. Path '', line 1, position 1.", validationEventArgs.Message);
Assert.AreEqual(1, validationEventArgs.ValidationError.Value);
Assert.AreEqual("#", validationEventArgs.ValidationError.SchemaId.ToString());
Assert.AreEqual(ErrorType.Minimum, validationEventArgs.ValidationError.ErrorType);
Assert.IsNotNull(validationEventArgs);
}
示例9: FloatLessThanMinimumValue
public void FloatLessThanMinimumValue()
{
string schemaJson = @"{
""type"":""number"",
""minimum"":5
}";
string json = "1.1";
SchemaValidationEventArgs validationEventArgs = null;
JSchemaValidatingReader reader = new JSchemaValidatingReader(new JsonTextReader(new StringReader(json)));
reader.ValidationEventHandler += (sender, args) => { validationEventArgs = args; };
reader.Schema = JSchema.Parse(schemaJson);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.Float, reader.TokenType);
Assert.AreEqual("Float 1.1 is less than minimum value of 5. Path '', line 1, position 3.", validationEventArgs.Message);
Assert.AreEqual(1.1d, validationEventArgs.ValidationError.Value);
Assert.IsNotNull(validationEventArgs);
}
示例10: ThrowExceptionWhenNoValidationEventHandler
public void ThrowExceptionWhenNoValidationEventHandler()
{
ExceptionAssert.Throws<JSchemaException>(() =>
{
string schemaJson = @"{
""type"":""integer"",
""maximum"":5
}";
JSchemaValidatingReader reader = new JSchemaValidatingReader(new JsonTextReader(new StringReader("10")));
reader.Schema = JSchema.Parse(schemaJson);
Assert.IsTrue(reader.Read());
}, "Integer 10 exceeds maximum value of 5. Path '', line 1, position 2.");
}
示例11: ReadAsDecimalFailure
public void ReadAsDecimalFailure()
{
ExceptionAssert.Throws<JSchemaException>(() =>
{
JSchema s = new JSchemaGenerator().Generate(typeof(decimal));
s.MultipleOf = 1;
JsonReader reader = new JSchemaValidatingReader(new JsonTextReader(new StringReader(@"5.5")))
{
Schema = s
};
reader.ReadAsDecimal();
}, "Float 5.5 is not a multiple of 1. Path '', line 1, position 3.");
}
示例12: ReadAsInt32InArrayIncomplete
public void ReadAsInt32InArrayIncomplete()
{
string schemaJson = @"{
""type"":""array"",
""items"":{
""type"":""integer""
},
""maxItems"":1
}";
string json = "[1,2";
SchemaValidationEventArgs validationEventArgs = null;
JSchemaValidatingReader reader = new JSchemaValidatingReader(new JsonTextReader(new StringReader(json)));
reader.ValidationEventHandler += (sender, args) => { validationEventArgs = args; };
reader.Schema = JSchema.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);
}
示例13: IntegerGreaterThanMaximumValue_BigInteger
public void IntegerGreaterThanMaximumValue_BigInteger()
{
string schemaJson = @"{
""type"":""integer"",
""maximum"":5
}";
string json = "99999999999999999999999999999999999999999999999999999999999999999999";
SchemaValidationEventArgs validationEventArgs = null;
JSchemaValidatingReader reader = new JSchemaValidatingReader(new JsonTextReader(new StringReader(json)));
reader.ValidationEventHandler += (sender, args) => { validationEventArgs = args; };
reader.Schema = JSchema.Parse(schemaJson);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.Integer, reader.TokenType);
Assert.AreEqual("Integer 99999999999999999999999999999999999999999999999999999999999999999999 exceeds maximum value of 5. Path '', line 1, position 68.", validationEventArgs.Message);
Assert.AreEqual(BigInteger.Parse("99999999999999999999999999999999999999999999999999999999999999999999"), validationEventArgs.ValidationError.Value);
Assert.AreEqual("", validationEventArgs.Path);
Assert.IsNotNull(validationEventArgs);
}
示例14: ReadAsInt32InArray
public void ReadAsInt32InArray()
{
string schemaJson = @"{
""type"":""array"",
""items"":{
""type"":""integer""
},
""maxItems"":1
}";
string json = "[1,2]";
SchemaValidationEventArgs validationEventArgs = null;
JSchemaValidatingReader reader = new JSchemaValidatingReader(new JsonTextReader(new StringReader(json)));
reader.ValidationEventHandler += (sender, args) => { validationEventArgs = args; };
reader.Schema = JSchema.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. Path '', line 1, position 5.", validationEventArgs.Message);
Assert.AreEqual(2, validationEventArgs.ValidationError.Value);
Assert.AreEqual("", validationEventArgs.Path);
}
示例15: BigIntegerDivisibleBy_Success
public void BigIntegerDivisibleBy_Success()
{
string schemaJson = @"{
""type"":""array"",
""items"":{
""type"":""number"",
""divisibleBy"":2
}
}";
string json = "[999999999999999999999999999999999999999999999999999999998]";
SchemaValidationEventArgs validationEventArgs = null;
JSchemaValidatingReader reader = new JSchemaValidatingReader(new JsonTextReader(new StringReader(json)));
reader.ValidationEventHandler += (sender, args) => { validationEventArgs = args; };
reader.Schema = JSchema.Parse(schemaJson);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.StartArray, reader.TokenType);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.Integer, reader.TokenType);
Assert.IsNull(validationEventArgs);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.EndArray, reader.TokenType);
}