~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/Newtonsoft.Json/Src/Newtonsoft.Json.Tests/Documentation/JsonSchemaTests.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
Import upstream version 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#region License
 
2
// Copyright (c) 2007 James Newton-King
 
3
//
 
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
 
11
// conditions:
 
12
//
 
13
// The above copyright notice and this permission notice shall be
 
14
// included in all copies or substantial portions of the Software.
 
15
//
 
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.
 
24
#endregion
 
25
 
 
26
#if !(NET35 || NET20 || PORTABLE)
 
27
using System;
 
28
using System.Collections.Generic;
 
29
using System.ComponentModel;
 
30
using System.Dynamic;
 
31
using System.IO;
 
32
using System.Linq;
 
33
using System.Runtime.Serialization;
 
34
using System.Text;
 
35
using Newtonsoft.Json.Converters;
 
36
using Newtonsoft.Json.Linq;
 
37
#if !NETFX_CORE
 
38
using NUnit.Framework;
 
39
#else
 
40
using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
 
41
using TestFixture = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestClassAttribute;
 
42
using Test = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestMethodAttribute;
 
43
#endif
 
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;
 
51
 
 
52
namespace Newtonsoft.Json.Tests.Documentation
 
53
{
 
54
  public class JsonSchemaTests
 
55
  {
 
56
    public void IsValidBasic()
 
57
    {
 
58
      #region IsValidBasic
 
59
      string schemaJson = @"{
 
60
        'description': 'A person',
 
61
        'type': 'object',
 
62
        'properties':
 
63
        {
 
64
          'name': {'type':'string'},
 
65
          'hobbies': {
 
66
            'type': 'array',
 
67
            'items': {'type':'string'}
 
68
          }
 
69
        }
 
70
      }";
 
71
 
 
72
      JsonSchema schema = JsonSchema.Parse(schemaJson);
 
73
 
 
74
      JObject person = JObject.Parse(@"{
 
75
        'name': 'James',
 
76
        'hobbies': ['.NET', 'Blogging', 'Reading', 'Xbox', 'LOLCATS']
 
77
      }");
 
78
 
 
79
      bool valid = person.IsValid(schema);
 
80
      // true
 
81
      #endregion
 
82
    }
 
83
 
 
84
    public void IsValidMessages()
 
85
    {
 
86
      string schemaJson = @"{
 
87
        'description': 'A person',
 
88
        'type': 'object',
 
89
        'properties':
 
90
        {
 
91
          'name': {'type':'string'},
 
92
          'hobbies': {
 
93
            'type': 'array',
 
94
            'items': {'type':'string'}
 
95
          }
 
96
        }
 
97
      }";
 
98
 
 
99
      #region IsValidMessages
 
100
      JsonSchema schema = JsonSchema.Parse(schemaJson);
 
101
 
 
102
      JObject person = JObject.Parse(@"{
 
103
        'name': null,
 
104
        'hobbies': ['Invalid content', 0.123456789]
 
105
      }");
 
106
 
 
107
      IList<string> messages;
 
108
      bool valid = person.IsValid(schema, out messages);
 
109
      // false
 
110
      // Invalid type. Expected String but got Null. Line 2, position 21.
 
111
      // Invalid type. Expected String but got Float. Line 3, position 51.
 
112
      #endregion
 
113
    }
 
114
 
 
115
    public void JsonValidatingReader()
 
116
    {
 
117
      string schemaJson = "{}";
 
118
 
 
119
      #region JsonValidatingReader
 
120
      string json = @"{
 
121
        'name': 'James',
 
122
        'hobbies': ['.NET', 'Blogging', 'Reading', 'Xbox', 'LOLCATS']
 
123
      }";
 
124
 
 
125
      JsonTextReader reader = new JsonTextReader(new StringReader(json));
 
126
 
 
127
      JsonValidatingReader validatingReader = new JsonValidatingReader(reader);
 
128
      validatingReader.Schema = JsonSchema.Parse(schemaJson);
 
129
 
 
130
      IList<string> messages = new List<string>();
 
131
      validatingReader.ValidationEventHandler += (o, a) => messages.Add(a.Message);
 
132
 
 
133
      JsonSerializer serializer = new JsonSerializer();
 
134
      Person p = serializer.Deserialize<Person>(validatingReader);
 
135
      #endregion
 
136
    }
 
137
 
 
138
    public void LoadJsonSchema()
 
139
    {
 
140
      #region LoadJsonSchema
 
141
      // load from a string
 
142
      JsonSchema schema1 = JsonSchema.Parse(@"{'type':'object'}");
 
143
 
 
144
      // load from a file
 
145
      using (TextReader reader = File.OpenText(@"c:\schema\Person.json"))
 
146
      {
 
147
        JsonSchema schema2 = JsonSchema.Read(new JsonTextReader(reader));
 
148
 
 
149
        // do stuff
 
150
      }
 
151
      #endregion
 
152
    }
 
153
 
 
154
    public void ManuallyCreateJsonSchema()
 
155
    {
 
156
      #region ManuallyCreateJsonSchema
 
157
      JsonSchema schema = new JsonSchema();
 
158
      schema.Type = JsonSchemaType.Object;
 
159
      schema.Properties = new Dictionary<string, JsonSchema>
 
160
        {
 
161
          {"name", new JsonSchema {Type = JsonSchemaType.String}},
 
162
          {
 
163
            "hobbies", new JsonSchema
 
164
              {
 
165
                Type = JsonSchemaType.Array,
 
166
                Items = new List<JsonSchema> { new JsonSchema {Type = JsonSchemaType.String} }
 
167
              }
 
168
          },
 
169
        };
 
170
 
 
171
      JObject person = JObject.Parse(@"{
 
172
        'name': 'James',
 
173
        'hobbies': ['.NET', 'Blogging', 'Reading', 'Xbox', 'LOLCATS']
 
174
      }");
 
175
 
 
176
      bool valid = person.IsValid(schema);
 
177
      // true
 
178
      #endregion
 
179
 
 
180
      Assert.IsTrue(valid);
 
181
    }
 
182
  }
 
183
}
 
184
#endif
 
 
b'\\ No newline at end of file'