-Examples
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
using Newtonsoft.Json.Linq;
|
||||
using Newtonsoft.Json.Schema;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace Newtonsoft.Json.Tests.Documentation.Examples.Schema
|
||||
{
|
||||
public class CreateJsonSchemaManually
|
||||
{
|
||||
public void Example()
|
||||
{
|
||||
JsonSchema schema = new JsonSchema();
|
||||
schema.Type = JsonSchemaType.Object;
|
||||
schema.Properties = new Dictionary<string, JsonSchema>
|
||||
{
|
||||
{"name", new JsonSchema {Type = JsonSchemaType.String}},
|
||||
{
|
||||
"hobbies", new JsonSchema
|
||||
{
|
||||
Type = JsonSchemaType.Array,
|
||||
Items = new List<JsonSchema> { new JsonSchema {Type = JsonSchemaType.String} }
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
string schemaJson = schema.ToString();
|
||||
|
||||
Console.WriteLine(schemaJson);
|
||||
// {
|
||||
// "type": "object",
|
||||
// "properties": {
|
||||
// "name": {
|
||||
// "type": "string"
|
||||
// },
|
||||
// "hobbies": {
|
||||
// "type": "array",
|
||||
// "items": {
|
||||
// "type": "string"
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
JObject person = JObject.Parse(@"{
|
||||
'name': 'James',
|
||||
'hobbies': ['.NET', 'Blogging', 'Reading', 'Xbox', 'LOLCATS']
|
||||
}");
|
||||
|
||||
bool valid = person.IsValid(schema);
|
||||
|
||||
Console.WriteLine(valid);
|
||||
// true
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
using Newtonsoft.Json.Linq;
|
||||
using Newtonsoft.Json.Schema;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace Newtonsoft.Json.Tests.Documentation.Examples.Schema
|
||||
{
|
||||
public class JTokenIsValid
|
||||
{
|
||||
public void Example()
|
||||
{
|
||||
string schemaJson = @"{
|
||||
'description': 'A person',
|
||||
'type': 'object',
|
||||
'properties': {
|
||||
'name': {'type':'string'},
|
||||
'hobbies': {
|
||||
'type': 'array',
|
||||
'items': {'type':'string'}
|
||||
}
|
||||
}
|
||||
}";
|
||||
|
||||
JsonSchema schema = JsonSchema.Parse(schemaJson);
|
||||
|
||||
JObject person = JObject.Parse(@"{
|
||||
'name': 'James',
|
||||
'hobbies': ['.NET', 'Blogging', 'Reading', 'Xbox', 'LOLCATS']
|
||||
}");
|
||||
|
||||
bool valid = person.IsValid(schema);
|
||||
|
||||
Console.WriteLine(valid);
|
||||
// true
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
using Newtonsoft.Json.Linq;
|
||||
using Newtonsoft.Json.Schema;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace Newtonsoft.Json.Tests.Documentation.Examples.Schema
|
||||
{
|
||||
public class JTokenIsValidWithMessages
|
||||
{
|
||||
public void Example()
|
||||
{
|
||||
string schemaJson = @"{
|
||||
'description': 'A person',
|
||||
'type': 'object',
|
||||
'properties': {
|
||||
'name': {'type':'string'},
|
||||
'hobbies': {
|
||||
'type': 'array',
|
||||
'items': {'type':'string'}
|
||||
}
|
||||
}
|
||||
}";
|
||||
|
||||
JsonSchema schema = JsonSchema.Parse(schemaJson);
|
||||
|
||||
JObject person = JObject.Parse(@"{
|
||||
'name': null,
|
||||
'hobbies': ['Invalid content', 0.123456789]
|
||||
}");
|
||||
|
||||
IList<string> messages;
|
||||
bool valid = person.IsValid(schema, out messages);
|
||||
|
||||
Console.WriteLine(valid);
|
||||
// false
|
||||
|
||||
foreach (string message in messages)
|
||||
{
|
||||
Console.WriteLine(message);
|
||||
}
|
||||
// Invalid type. Expected String but got Null. Line 2, position 21.
|
||||
// Invalid type. Expected String but got Float. Line 3, position 51.
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
using Newtonsoft.Json.Linq;
|
||||
using Newtonsoft.Json.Schema;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace Newtonsoft.Json.Tests.Documentation.Examples.Schema
|
||||
{
|
||||
public class JTokenValidateWithEvent
|
||||
{
|
||||
public void Example()
|
||||
{
|
||||
string schemaJson = @"{
|
||||
'description': 'A person',
|
||||
'type': 'object',
|
||||
'properties': {
|
||||
'name': {'type':'string'},
|
||||
'hobbies': {
|
||||
'type': 'array',
|
||||
'items': {'type':'string'}
|
||||
}
|
||||
}
|
||||
}";
|
||||
|
||||
JsonSchema schema = JsonSchema.Parse(schemaJson);
|
||||
|
||||
JObject person = JObject.Parse(@"{
|
||||
'name': null,
|
||||
'hobbies': ['Invalid content', 0.123456789]
|
||||
}");
|
||||
|
||||
IList<string> messages = new List<string>();
|
||||
ValidationEventHandler validationEventHandler = (sender, args) => { messages.Add(args.Message); };
|
||||
|
||||
person.Validate(schema, validationEventHandler);
|
||||
|
||||
foreach (string message in messages)
|
||||
{
|
||||
Console.WriteLine(message);
|
||||
}
|
||||
// Invalid type. Expected String but got Null. Line 2, position 21.
|
||||
// Invalid type. Expected String but got Float. Line 3, position 51.
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
using Newtonsoft.Json.Linq;
|
||||
using Newtonsoft.Json.Schema;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace Newtonsoft.Json.Tests.Documentation.Examples.Schema
|
||||
{
|
||||
public class JsonSchemaLoad
|
||||
{
|
||||
public void Example()
|
||||
{
|
||||
string schemaJson = @"{'type': 'object'}";
|
||||
|
||||
JsonSchema schema;
|
||||
using (TextReader file = File.OpenText(@"c:\schema.json"))
|
||||
using (JsonTextReader reader = new JsonTextReader(file))
|
||||
{
|
||||
schema = JsonSchema.Read(reader);
|
||||
}
|
||||
|
||||
JObject o = JObject.Parse(@"{}");
|
||||
|
||||
bool valid = o.IsValid(schema);
|
||||
|
||||
Console.WriteLine(valid);
|
||||
// true
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
using Newtonsoft.Json.Linq;
|
||||
using Newtonsoft.Json.Schema;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace Newtonsoft.Json.Tests.Documentation.Examples.Schema
|
||||
{
|
||||
public class JsonSchemaParse
|
||||
{
|
||||
public void Example()
|
||||
{
|
||||
string schemaJson = @"{
|
||||
'description': 'A person',
|
||||
'type': 'object',
|
||||
'properties': {
|
||||
'name': {'type':'string'},
|
||||
'hobbies': {
|
||||
'type': 'array',
|
||||
'items': {'type':'string'}
|
||||
}
|
||||
}
|
||||
}";
|
||||
|
||||
JsonSchema schema = JsonSchema.Parse(schemaJson);
|
||||
|
||||
Console.WriteLine(schema.Type);
|
||||
// Object
|
||||
|
||||
foreach (var property in schema.Properties)
|
||||
{
|
||||
Console.WriteLine(property.Key + " - " + property.Value.Type);
|
||||
}
|
||||
// name - String
|
||||
// hobbies - Array
|
||||
}
|
||||
}
|
||||
}
|
||||
+57
@@ -0,0 +1,57 @@
|
||||
using Newtonsoft.Json.Schema;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace Newtonsoft.Json.Tests.Documentation.Examples.Schema
|
||||
{
|
||||
public class JsonValidatingReaderAndSerializer
|
||||
{
|
||||
public class Person
|
||||
{
|
||||
public string Name { get; set; }
|
||||
public IList<string> Hobbies { get; set; }
|
||||
}
|
||||
|
||||
public void Example()
|
||||
{
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
using Newtonsoft.Json.Schema;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace Newtonsoft.Json.Tests.Documentation.Examples.Schema
|
||||
{
|
||||
public class LoadJsonSchemaFromFile
|
||||
{
|
||||
public void Example()
|
||||
{
|
||||
// read file into a string and parse JsonSchema from the string
|
||||
JsonSchema schema1 = JsonSchema.Parse(File.ReadAllText(@"c:\schema.json"));
|
||||
|
||||
// read JsonSchema directly from a file
|
||||
using (StreamReader file = File.OpenText(@"c:\schema.json"))
|
||||
using (JsonTextReader reader = new JsonTextReader(file))
|
||||
{
|
||||
JsonSchema schema2 = JsonSchema.Read(reader);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
using Newtonsoft.Json.Linq;
|
||||
using Newtonsoft.Json.Schema;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace Newtonsoft.Json.Tests.Documentation.Examples.Schema
|
||||
{
|
||||
public class RefJsonSchemaResolver
|
||||
{
|
||||
public void Example()
|
||||
{
|
||||
string schemaJson;
|
||||
JsonSchemaResolver resolver = new JsonSchemaResolver();
|
||||
|
||||
schemaJson = @"{
|
||||
""id"": ""person"",
|
||||
""type"": ""object"",
|
||||
""properties"": {
|
||||
""name"": {""type"":""string""},
|
||||
""age"": {""type"":""integer""}
|
||||
}
|
||||
}";
|
||||
|
||||
JsonSchema personSchema = JsonSchema.Parse(schemaJson, resolver);
|
||||
|
||||
schemaJson = @"{
|
||||
""id"": ""employee"",
|
||||
""type"": ""object"",
|
||||
""extends"": {""$ref"":""person""},
|
||||
""properties"": {
|
||||
""salary"": {""type"":""number""},
|
||||
""jobTitle"": {""type"":""string""}
|
||||
}
|
||||
}";
|
||||
|
||||
JsonSchema employeeSchema = JsonSchema.Parse(schemaJson, 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
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
using Newtonsoft.Json.Schema;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace Newtonsoft.Json.Tests.Documentation.Examples.Schema
|
||||
{
|
||||
public class SaveJsonSchemaToFile
|
||||
{
|
||||
public void Example()
|
||||
{
|
||||
JsonSchema schema = JsonSchema.Parse(@"{'type': 'object'}");
|
||||
|
||||
// serialize JsonSchema to a string and then write string to a file
|
||||
File.WriteAllText(@"c:\schema.json", schema.ToString());
|
||||
|
||||
// serialize JsonSchema directly to a file
|
||||
using (StreamWriter file = File.CreateText(@"c:\schema.json"))
|
||||
using (JsonTextWriter writer = new JsonTextWriter(file))
|
||||
{
|
||||
schema.WriteTo(writer);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user