Click or drag to resize
Json.NET Schema

Using JSchemaPreloadedResolver

 

This sample uses a JSchemaPreloadedResolver to resolve schema references from different JSON documents.

Sample
Usage
string personSchemaJson = @"{
  'type': 'object',
  'properties': {
    'name': { 'type': 'string' },
    'age': { 'type': 'integer' }
  }
}";

JSchemaPreloadedResolver resolver = new JSchemaPreloadedResolver();
resolver.Add(new Uri("person.json", UriKind.RelativeOrAbsolute), personSchemaJson);

// the external 'person.json' schema will be found using the resolver
// the internal 'salary' schema will be found using the default resolution logic
JSchema employeeSchema = JSchema.Parse(@"{
  'type': 'object',
  'allOf': [
    { '$ref': 'person.json' }
  ],
  'properties': {
    'salary': { '$ref': '#/definitions/salary' },
    'jobTitle': { 'type': 'string' }
  },
  'definitions': {
    'salary': { 'type': 'number' }
  }
}", 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