/
JsonObjectModel.cs
217 lines (201 loc) · 9.5 KB
/
JsonObjectModel.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
//-----------------------------------------------------------------------
// <copyright file="JsonObjectModel.cs" company="Visual JSON Editor">
// Copyright (c) Rico Suter. All rights reserved.
// </copyright>
// <license>http://visualjsoneditor.codeplex.com/license</license>
// <author>Rico Suter, mail@rsuter.com</author>
//-----------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using NJsonSchema;
using NJsonSchema.Validation;
namespace VisualJsonEditor.Models
{
/// <summary>Represents a JSON object. </summary>
public class JsonObjectModel : JsonTokenModel
{
/// <summary>Creates a default <see cref="JsonObjectModel"/> from the given schema. </summary>
/// <param name="schema">The <see cref="JsonSchema4"/>. </param>
/// <returns>The <see cref="JsonObjectModel"/>. </returns>
public static JsonObjectModel FromSchema(JsonSchema4 schema)
{
schema = schema.ActualSchema;
var obj = new JsonObjectModel();
foreach (var property in schema.Properties)
{
var propertySchema = property.Value.ActualSchema;
if (propertySchema.Type.HasFlag(JsonObjectType.Object))
{
if (property.Value.IsRequired)
obj[property.Key] = FromSchema(propertySchema);
else
obj[property.Key] = null;
}
else if (propertySchema.Type.HasFlag(JsonObjectType.Array))
{
if (property.Value.IsRequired)
obj[property.Key] = new ObservableCollection<JsonTokenModel>();
else
obj[property.Key] = null;
}
else
obj[property.Key] = GetDefaultValue(property);
}
obj.Schema = schema;
return obj;
}
/// <summary>Creates a <see cref="JsonObjectModel"/> from the given JSON data and <see cref="JsonSchema4"/>. </summary>
/// <param name="jsonData">The JSON data. </param>
/// <param name="schema">The <see cref="JsonSchema4"/>. </param>
/// <returns>The <see cref="JsonObjectModel"/>. </returns>
public static JsonObjectModel FromJson(string jsonData, JsonSchema4 schema)
{
var json = JsonConvert.DeserializeObject(jsonData);
return FromJson((JObject)json, schema);
}
/// <summary>Creates a <see cref="JsonObjectModel"/> from the given <see cref="JObject"/> and <see cref="JsonSchema4"/>. </summary>
/// <param name="obj">The <see cref="JObject"/>. </param>
/// <param name="schema">The <see cref="JsonSchema4"/>. </param>
/// <returns>The <see cref="JsonObjectModel"/>. </returns>
public static JsonObjectModel FromJson(JObject obj, JsonSchema4 schema)
{
schema = schema.ActualSchema;
var result = new JsonObjectModel();
foreach (var property in schema.Properties)
{
var propertySchema = property.Value.ActualSchema;
if (propertySchema.Type.HasFlag(JsonObjectType.Array))
{
var propertyItemSchema = propertySchema.Item != null ? propertySchema.Item.ActualSchema : null;
if (obj[property.Key] != null)
{
var objects = obj[property.Key].Select(o => o is JObject ?
(JsonTokenModel)FromJson((JObject)o, propertyItemSchema) :
JsonValueModel.FromJson((JValue)o, propertyItemSchema));
var list = new ObservableCollection<JsonTokenModel>(objects);
foreach (var item in list)
item.ParentList = list;
result[property.Key] = list;
}
else
result[property.Key] = null;
}
else if (propertySchema.Type.HasFlag(JsonObjectType.Object) || propertySchema.Type == JsonObjectType.None)
{
var token = obj[property.Key];
if (token is JObject)
result[property.Key] = FromJson((JObject)token, propertySchema);
else
result[property.Key] = null;
}
else
{
JToken value;
if (obj.TryGetValue(property.Key, out value))
result[property.Key] = ((JValue)value).Value;
else
result[property.Key] = GetDefaultValue(property);
}
}
result.Schema = schema;
return result;
}
private static object GetDefaultValue(KeyValuePair<string, JsonProperty> property)
{
var propertySchema = property.Value.ActualSchema;
if (propertySchema.Default != null)
return propertySchema.Default;
if (propertySchema.Type.HasFlag(JsonObjectType.Boolean))
return false;
if (propertySchema.Type.HasFlag(JsonObjectType.String) && propertySchema.Format == JsonFormatStrings.DateTime)
return new DateTime();
if (propertySchema.Type.HasFlag(JsonObjectType.String) && propertySchema.Format == "date") // TODO: What to do with date/time?
return new DateTime();
if (propertySchema.Type.HasFlag(JsonObjectType.String) && propertySchema.Format == "time")
return new TimeSpan();
if (propertySchema.Type.HasFlag(JsonObjectType.String))
return string.Empty;
return null;
}
/// <summary>Gets the object's properties. </summary>
public IEnumerable<JsonPropertyModel> Properties
{
get
{
var properties = new List<JsonPropertyModel>();
if (Schema.Properties != null)
{
foreach (var propertyInfo in Schema.Properties)
{
var property = new JsonPropertyModel(propertyInfo.Key, this, propertyInfo.Value);
if (property.Value is ObservableCollection<JsonTokenModel>)
{
foreach (var obj in (ObservableCollection<JsonTokenModel>)property.Value)
obj.Schema = propertyInfo.Value.Item != null ? propertyInfo.Value.Item.ActualSchema : null;
}
properties.Add(property);
}
}
return properties;
}
}
/// <summary>Converts the <see cref="JsonTokenModel"/> to a <see cref="JToken"/>. </summary>
/// <returns>The <see cref="JToken"/>. </returns>
public override JToken ToJToken()
{
var obj = new JObject();
foreach (var pair in this)
{
if (pair.Value is ObservableCollection<JsonTokenModel>)
{
var array = (ObservableCollection<JsonTokenModel>)pair.Value;
var jArray = new JArray();
foreach (var item in array)
jArray.Add(item.ToJToken());
obj[pair.Key] = jArray;
}
else if (pair.Value is JsonTokenModel)
obj[pair.Key] = ((JsonTokenModel)pair.Value).ToJToken();
else if (pair.Value != null || Schema.Properties[pair.Key].IsRequired)
obj[pair.Key] = new JValue(pair.Value);
}
return obj;
}
/// <summary>Validates the data of the <see cref="JsonObjectModel"/>. </summary>
/// <returns>The list of validation errors. </returns>
public Task<string> ValidateAsync()
{
return Task.Run(() =>
{
var json = ToJson();
var errors = Schema.Validate(json);
return string.Join("\n", ConvertErrors(errors, string.Empty));
});
}
private static string[] ConvertErrors(IEnumerable<ValidationError> errors, string padding)
{
return errors.Select(error =>
{
var output = new StringBuilder(string.Format("{0}{1}: {2}", padding, error.Path, error.Kind));
if (error is ChildSchemaValidationError)
{
foreach (var childError in ((ChildSchemaValidationError)error).Errors)
{
var schemaTitle = (!string.IsNullOrEmpty(childError.Key.Title) ? childError.Key.Title : "Schema");
output.Append(string.Format("\n{0} {1}:", padding, schemaTitle));
foreach (var x in ConvertErrors(childError.Value, padding + " "))
output.Append(string.Format("\n{0}", x));
}
}
return output.ToString();
}).ToArray();
}
}
}