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