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

« back to all changes in this revision

Viewing changes to external/Newtonsoft.Json/Src/Newtonsoft.Json/Schema/JsonSchemaWriter.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
using System.Collections.Generic;
 
27
using Newtonsoft.Json.Linq;
 
28
using Newtonsoft.Json.Utilities;
 
29
#if NET20
 
30
using Newtonsoft.Json.Utilities.LinqBridge;
 
31
#else
 
32
using System.Linq;
 
33
#endif
 
34
 
 
35
namespace Newtonsoft.Json.Schema
 
36
{
 
37
  internal class JsonSchemaWriter
 
38
  {
 
39
    private readonly JsonWriter _writer;
 
40
    private readonly JsonSchemaResolver _resolver;
 
41
 
 
42
    public JsonSchemaWriter(JsonWriter writer, JsonSchemaResolver resolver)
 
43
    {
 
44
      ValidationUtils.ArgumentNotNull(writer, "writer");
 
45
      _writer = writer;
 
46
      _resolver = resolver;
 
47
    }
 
48
 
 
49
    private void ReferenceOrWriteSchema(JsonSchema schema)
 
50
    {
 
51
      if (schema.Id != null && _resolver.GetSchema(schema.Id) != null)
 
52
      {
 
53
        _writer.WriteStartObject();
 
54
        _writer.WritePropertyName(JsonSchemaConstants.ReferencePropertyName);
 
55
        _writer.WriteValue(schema.Id);
 
56
        _writer.WriteEndObject();
 
57
      }
 
58
      else
 
59
      {
 
60
        WriteSchema(schema);
 
61
      }
 
62
    }
 
63
 
 
64
    public void WriteSchema(JsonSchema schema)
 
65
    {
 
66
      ValidationUtils.ArgumentNotNull(schema, "schema");
 
67
 
 
68
      if (!_resolver.LoadedSchemas.Contains(schema))
 
69
        _resolver.LoadedSchemas.Add(schema);
 
70
 
 
71
      _writer.WriteStartObject();
 
72
      WritePropertyIfNotNull(_writer, JsonSchemaConstants.IdPropertyName, schema.Id);
 
73
      WritePropertyIfNotNull(_writer, JsonSchemaConstants.TitlePropertyName, schema.Title);
 
74
      WritePropertyIfNotNull(_writer, JsonSchemaConstants.DescriptionPropertyName, schema.Description);
 
75
      WritePropertyIfNotNull(_writer, JsonSchemaConstants.RequiredPropertyName, schema.Required);
 
76
      WritePropertyIfNotNull(_writer, JsonSchemaConstants.ReadOnlyPropertyName, schema.ReadOnly);
 
77
      WritePropertyIfNotNull(_writer, JsonSchemaConstants.HiddenPropertyName, schema.Hidden);
 
78
      WritePropertyIfNotNull(_writer, JsonSchemaConstants.TransientPropertyName, schema.Transient);
 
79
      if (schema.Type != null)
 
80
        WriteType(JsonSchemaConstants.TypePropertyName, _writer, schema.Type.Value);
 
81
      if (!schema.AllowAdditionalProperties)
 
82
      {
 
83
        _writer.WritePropertyName(JsonSchemaConstants.AdditionalPropertiesPropertyName);
 
84
        _writer.WriteValue(schema.AllowAdditionalProperties);
 
85
      }
 
86
      else
 
87
      {
 
88
        if (schema.AdditionalProperties != null)
 
89
        {
 
90
          _writer.WritePropertyName(JsonSchemaConstants.AdditionalPropertiesPropertyName);
 
91
          ReferenceOrWriteSchema(schema.AdditionalProperties);
 
92
        }
 
93
      }
 
94
      WriteSchemaDictionaryIfNotNull(_writer, JsonSchemaConstants.PropertiesPropertyName, schema.Properties);
 
95
      WriteSchemaDictionaryIfNotNull(_writer, JsonSchemaConstants.PatternPropertiesPropertyName, schema.PatternProperties);
 
96
      WriteItems(schema);
 
97
      WritePropertyIfNotNull(_writer, JsonSchemaConstants.MinimumPropertyName, schema.Minimum);
 
98
      WritePropertyIfNotNull(_writer, JsonSchemaConstants.MaximumPropertyName, schema.Maximum);
 
99
      WritePropertyIfNotNull(_writer, JsonSchemaConstants.ExclusiveMinimumPropertyName, schema.ExclusiveMinimum);
 
100
      WritePropertyIfNotNull(_writer, JsonSchemaConstants.ExclusiveMaximumPropertyName, schema.ExclusiveMaximum);
 
101
      WritePropertyIfNotNull(_writer, JsonSchemaConstants.MinimumLengthPropertyName, schema.MinimumLength);
 
102
      WritePropertyIfNotNull(_writer, JsonSchemaConstants.MaximumLengthPropertyName, schema.MaximumLength);
 
103
      WritePropertyIfNotNull(_writer, JsonSchemaConstants.MinimumItemsPropertyName, schema.MinimumItems);
 
104
      WritePropertyIfNotNull(_writer, JsonSchemaConstants.MaximumItemsPropertyName, schema.MaximumItems);
 
105
      WritePropertyIfNotNull(_writer, JsonSchemaConstants.DivisibleByPropertyName, schema.DivisibleBy);
 
106
      WritePropertyIfNotNull(_writer, JsonSchemaConstants.FormatPropertyName, schema.Format);
 
107
      WritePropertyIfNotNull(_writer, JsonSchemaConstants.PatternPropertyName, schema.Pattern);
 
108
      if (schema.Enum != null)
 
109
      {
 
110
        _writer.WritePropertyName(JsonSchemaConstants.EnumPropertyName);
 
111
        _writer.WriteStartArray();
 
112
        foreach (JToken token in schema.Enum)
 
113
        {
 
114
          token.WriteTo(_writer);
 
115
        }
 
116
        _writer.WriteEndArray();
 
117
      }
 
118
      if (schema.Default != null)
 
119
      {
 
120
        _writer.WritePropertyName(JsonSchemaConstants.DefaultPropertyName);
 
121
        schema.Default.WriteTo(_writer);
 
122
      }
 
123
      if (schema.Options != null)
 
124
      {
 
125
        _writer.WritePropertyName(JsonSchemaConstants.OptionsPropertyName);
 
126
        _writer.WriteStartArray();
 
127
        foreach (KeyValuePair<JToken, string> option in schema.Options)
 
128
        {
 
129
          _writer.WriteStartObject();
 
130
          _writer.WritePropertyName(JsonSchemaConstants.OptionValuePropertyName);
 
131
          option.Key.WriteTo(_writer);
 
132
          if (option.Value != null)
 
133
          {
 
134
            _writer.WritePropertyName(JsonSchemaConstants.OptionLabelPropertyName);
 
135
            _writer.WriteValue(option.Value);
 
136
          }
 
137
          _writer.WriteEndObject();
 
138
        }
 
139
        _writer.WriteEndArray();
 
140
      }
 
141
      if (schema.Disallow != null)
 
142
        WriteType(JsonSchemaConstants.DisallowPropertyName, _writer, schema.Disallow.Value);
 
143
      if (schema.Extends != null)
 
144
      {
 
145
        _writer.WritePropertyName(JsonSchemaConstants.ExtendsPropertyName);
 
146
        ReferenceOrWriteSchema(schema.Extends);
 
147
      }
 
148
      _writer.WriteEndObject();
 
149
    }
 
150
 
 
151
    private void WriteSchemaDictionaryIfNotNull(JsonWriter writer, string propertyName, IDictionary<string, JsonSchema> properties)
 
152
    {
 
153
      if (properties != null)
 
154
      {
 
155
        writer.WritePropertyName(propertyName);
 
156
        writer.WriteStartObject();
 
157
        foreach (KeyValuePair<string, JsonSchema> property in properties)
 
158
        {
 
159
          writer.WritePropertyName(property.Key);
 
160
          ReferenceOrWriteSchema(property.Value);
 
161
        }
 
162
        writer.WriteEndObject();
 
163
      }
 
164
    }
 
165
 
 
166
    private void WriteItems(JsonSchema schema)
 
167
    {
 
168
      if (CollectionUtils.IsNullOrEmpty(schema.Items))
 
169
        return;
 
170
 
 
171
      _writer.WritePropertyName(JsonSchemaConstants.ItemsPropertyName);
 
172
 
 
173
      if (schema.Items.Count == 1)
 
174
      {
 
175
        ReferenceOrWriteSchema(schema.Items[0]);
 
176
        return;
 
177
      }
 
178
 
 
179
      _writer.WriteStartArray();
 
180
      foreach (JsonSchema itemSchema in schema.Items)
 
181
      {
 
182
        ReferenceOrWriteSchema(itemSchema);
 
183
      }
 
184
      _writer.WriteEndArray();
 
185
    }
 
186
 
 
187
    private void WriteType(string propertyName, JsonWriter writer, JsonSchemaType type)
 
188
    {
 
189
      IList<JsonSchemaType> types;
 
190
      if (System.Enum.IsDefined(typeof(JsonSchemaType), type))
 
191
        types = new List<JsonSchemaType> { type };
 
192
      else
 
193
        types = EnumUtils.GetFlagsValues(type).Where(v => v != JsonSchemaType.None).ToList();
 
194
 
 
195
      if (types.Count == 0)
 
196
        return;
 
197
 
 
198
      writer.WritePropertyName(propertyName);
 
199
 
 
200
      if (types.Count == 1)
 
201
      {
 
202
        writer.WriteValue(JsonSchemaBuilder.MapType(types[0]));
 
203
        return;
 
204
      }
 
205
 
 
206
      writer.WriteStartArray();
 
207
      foreach (JsonSchemaType jsonSchemaType in types)
 
208
      {
 
209
        writer.WriteValue(JsonSchemaBuilder.MapType(jsonSchemaType));
 
210
      }
 
211
      writer.WriteEndArray();
 
212
    }
 
213
 
 
214
    private void WritePropertyIfNotNull(JsonWriter writer, string propertyName, object value)
 
215
    {
 
216
      if (value != null)
 
217
      {
 
218
        writer.WritePropertyName(propertyName);
 
219
        writer.WriteValue(value);
 
220
      }
 
221
    }
 
222
  }
 
223
}