2
// Copyright (c) 2007 James Newton-King
4
// Permission is hereby granted, free of charge, to any person
5
// obtaining a copy of this software and associated documentation
6
// files (the "Software"), to deal in the Software without
7
// restriction, including without limitation the rights to use,
8
// copy, modify, merge, publish, distribute, sublicense, and/or sell
9
// copies of the Software, and to permit persons to whom the
10
// Software is furnished to do so, subject to the following
13
// The above copyright notice and this permission notice shall be
14
// included in all copies or substantial portions of the Software.
16
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23
// OTHER DEALINGS IN THE SOFTWARE.
26
#if !(NET35 || NET20 || PORTABLE)
28
using System.Collections.Generic;
29
using System.ComponentModel;
33
using System.Runtime.Serialization;
35
using Newtonsoft.Json.Converters;
36
using Newtonsoft.Json.Linq;
38
using NUnit.Framework;
40
using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
41
using TestFixture = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestClassAttribute;
42
using Test = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestMethodAttribute;
44
using Newtonsoft.Json.Schema;
45
using Newtonsoft.Json.Serialization;
46
using Newtonsoft.Json.Tests.TestObjects;
47
using Newtonsoft.Json.Utilities;
48
using System.Globalization;
49
using ErrorEventArgs = Newtonsoft.Json.Serialization.ErrorEventArgs;
50
using File = System.IO.File;
52
namespace Newtonsoft.Json.Tests.Documentation
54
public class JsonSchemaTests
56
public void IsValidBasic()
59
string schemaJson = @"{
60
'description': 'A person',
64
'name': {'type':'string'},
67
'items': {'type':'string'}
72
JsonSchema schema = JsonSchema.Parse(schemaJson);
74
JObject person = JObject.Parse(@"{
76
'hobbies': ['.NET', 'Blogging', 'Reading', 'Xbox', 'LOLCATS']
79
bool valid = person.IsValid(schema);
84
public void IsValidMessages()
86
string schemaJson = @"{
87
'description': 'A person',
91
'name': {'type':'string'},
94
'items': {'type':'string'}
99
#region IsValidMessages
100
JsonSchema schema = JsonSchema.Parse(schemaJson);
102
JObject person = JObject.Parse(@"{
104
'hobbies': ['Invalid content', 0.123456789]
107
IList<string> messages;
108
bool valid = person.IsValid(schema, out messages);
110
// Invalid type. Expected String but got Null. Line 2, position 21.
111
// Invalid type. Expected String but got Float. Line 3, position 51.
115
public void JsonValidatingReader()
117
string schemaJson = "{}";
119
#region JsonValidatingReader
122
'hobbies': ['.NET', 'Blogging', 'Reading', 'Xbox', 'LOLCATS']
125
JsonTextReader reader = new JsonTextReader(new StringReader(json));
127
JsonValidatingReader validatingReader = new JsonValidatingReader(reader);
128
validatingReader.Schema = JsonSchema.Parse(schemaJson);
130
IList<string> messages = new List<string>();
131
validatingReader.ValidationEventHandler += (o, a) => messages.Add(a.Message);
133
JsonSerializer serializer = new JsonSerializer();
134
Person p = serializer.Deserialize<Person>(validatingReader);
138
public void LoadJsonSchema()
140
#region LoadJsonSchema
141
// load from a string
142
JsonSchema schema1 = JsonSchema.Parse(@"{'type':'object'}");
145
using (TextReader reader = File.OpenText(@"c:\schema\Person.json"))
147
JsonSchema schema2 = JsonSchema.Read(new JsonTextReader(reader));
154
public void ManuallyCreateJsonSchema()
156
#region ManuallyCreateJsonSchema
157
JsonSchema schema = new JsonSchema();
158
schema.Type = JsonSchemaType.Object;
159
schema.Properties = new Dictionary<string, JsonSchema>
161
{"name", new JsonSchema {Type = JsonSchemaType.String}},
163
"hobbies", new JsonSchema
165
Type = JsonSchemaType.Array,
166
Items = new List<JsonSchema> { new JsonSchema {Type = JsonSchemaType.String} }
171
JObject person = JObject.Parse(@"{
173
'hobbies': ['.NET', 'Blogging', 'Reading', 'Xbox', 'LOLCATS']
176
bool valid = person.IsValid(schema);
180
Assert.IsTrue(valid);
b'\\ No newline at end of file'