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
using System.Collections.Generic;
27
using Newtonsoft.Json.Linq;
28
using Newtonsoft.Json.Utilities;
30
using Newtonsoft.Json.Utilities.LinqBridge;
35
namespace Newtonsoft.Json.Schema
37
internal class JsonSchemaWriter
39
private readonly JsonWriter _writer;
40
private readonly JsonSchemaResolver _resolver;
42
public JsonSchemaWriter(JsonWriter writer, JsonSchemaResolver resolver)
44
ValidationUtils.ArgumentNotNull(writer, "writer");
49
private void ReferenceOrWriteSchema(JsonSchema schema)
51
if (schema.Id != null && _resolver.GetSchema(schema.Id) != null)
53
_writer.WriteStartObject();
54
_writer.WritePropertyName(JsonSchemaConstants.ReferencePropertyName);
55
_writer.WriteValue(schema.Id);
56
_writer.WriteEndObject();
64
public void WriteSchema(JsonSchema schema)
66
ValidationUtils.ArgumentNotNull(schema, "schema");
68
if (!_resolver.LoadedSchemas.Contains(schema))
69
_resolver.LoadedSchemas.Add(schema);
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)
83
_writer.WritePropertyName(JsonSchemaConstants.AdditionalPropertiesPropertyName);
84
_writer.WriteValue(schema.AllowAdditionalProperties);
88
if (schema.AdditionalProperties != null)
90
_writer.WritePropertyName(JsonSchemaConstants.AdditionalPropertiesPropertyName);
91
ReferenceOrWriteSchema(schema.AdditionalProperties);
94
WriteSchemaDictionaryIfNotNull(_writer, JsonSchemaConstants.PropertiesPropertyName, schema.Properties);
95
WriteSchemaDictionaryIfNotNull(_writer, JsonSchemaConstants.PatternPropertiesPropertyName, schema.PatternProperties);
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)
110
_writer.WritePropertyName(JsonSchemaConstants.EnumPropertyName);
111
_writer.WriteStartArray();
112
foreach (JToken token in schema.Enum)
114
token.WriteTo(_writer);
116
_writer.WriteEndArray();
118
if (schema.Default != null)
120
_writer.WritePropertyName(JsonSchemaConstants.DefaultPropertyName);
121
schema.Default.WriteTo(_writer);
123
if (schema.Options != null)
125
_writer.WritePropertyName(JsonSchemaConstants.OptionsPropertyName);
126
_writer.WriteStartArray();
127
foreach (KeyValuePair<JToken, string> option in schema.Options)
129
_writer.WriteStartObject();
130
_writer.WritePropertyName(JsonSchemaConstants.OptionValuePropertyName);
131
option.Key.WriteTo(_writer);
132
if (option.Value != null)
134
_writer.WritePropertyName(JsonSchemaConstants.OptionLabelPropertyName);
135
_writer.WriteValue(option.Value);
137
_writer.WriteEndObject();
139
_writer.WriteEndArray();
141
if (schema.Disallow != null)
142
WriteType(JsonSchemaConstants.DisallowPropertyName, _writer, schema.Disallow.Value);
143
if (schema.Extends != null)
145
_writer.WritePropertyName(JsonSchemaConstants.ExtendsPropertyName);
146
ReferenceOrWriteSchema(schema.Extends);
148
_writer.WriteEndObject();
151
private void WriteSchemaDictionaryIfNotNull(JsonWriter writer, string propertyName, IDictionary<string, JsonSchema> properties)
153
if (properties != null)
155
writer.WritePropertyName(propertyName);
156
writer.WriteStartObject();
157
foreach (KeyValuePair<string, JsonSchema> property in properties)
159
writer.WritePropertyName(property.Key);
160
ReferenceOrWriteSchema(property.Value);
162
writer.WriteEndObject();
166
private void WriteItems(JsonSchema schema)
168
if (CollectionUtils.IsNullOrEmpty(schema.Items))
171
_writer.WritePropertyName(JsonSchemaConstants.ItemsPropertyName);
173
if (schema.Items.Count == 1)
175
ReferenceOrWriteSchema(schema.Items[0]);
179
_writer.WriteStartArray();
180
foreach (JsonSchema itemSchema in schema.Items)
182
ReferenceOrWriteSchema(itemSchema);
184
_writer.WriteEndArray();
187
private void WriteType(string propertyName, JsonWriter writer, JsonSchemaType type)
189
IList<JsonSchemaType> types;
190
if (System.Enum.IsDefined(typeof(JsonSchemaType), type))
191
types = new List<JsonSchemaType> { type };
193
types = EnumUtils.GetFlagsValues(type).Where(v => v != JsonSchemaType.None).ToList();
195
if (types.Count == 0)
198
writer.WritePropertyName(propertyName);
200
if (types.Count == 1)
202
writer.WriteValue(JsonSchemaBuilder.MapType(types[0]));
206
writer.WriteStartArray();
207
foreach (JsonSchemaType jsonSchemaType in types)
209
writer.WriteValue(JsonSchemaBuilder.MapType(jsonSchemaType));
211
writer.WriteEndArray();
214
private void WritePropertyIfNotNull(JsonWriter writer, string propertyName, object value)
218
writer.WritePropertyName(propertyName);
219
writer.WriteValue(value);