-
Notifications
You must be signed in to change notification settings - Fork 97
Expand file tree
/
Copy pathExtensibleJsonObject.cs
More file actions
390 lines (337 loc) · 14 KB
/
Copy pathExtensibleJsonObject.cs
File metadata and controls
390 lines (337 loc) · 14 KB
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
using System.Collections.ObjectModel;
namespace net.openstack.Core.Domain
{
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using net.openstack.Core.Collections;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using IEnumerable = System.Collections.IEnumerable;
using IEnumerator = System.Collections.IEnumerator;
/// <summary>
/// This is the abstract base class for types modeling the JSON representation of a resource
/// which may be extended by specific providers or updated in future releases of a core
/// service.
/// </summary>
/// <threadsafety static="true" instance="false"/>
/// <preliminary/>
[JsonObject(MemberSerialization.OptIn)]
public abstract class ExtensibleJsonObject
{
/// <summary>
/// An empty, and thus immutable, value which is the default return value
/// for <see cref="ExtensionData"/> when the backing field is <see langword="null"/>.
/// </summary>
protected static readonly ReadOnlyDictionary<string, JToken> EmptyExtensionData =
new ReadOnlyDictionary<string, JToken>(new Dictionary<string, JToken>());
/// <summary>
/// This is the backing field for the <see cref="ExtensionData"/> property.
/// </summary>
private Dictionary<string, JToken> _extensionData;
/// <summary>
/// Initializes a new instance of the <see cref="ExtensibleJsonObject"/> class
/// during JSON deserialization.
/// </summary>
[JsonConstructor]
protected ExtensibleJsonObject()
{
}
/// <summary>
/// Initializes a new instance of the <see cref="ExtensibleJsonObject"/> class
/// with the specified extension data.
/// </summary>
/// <param name="extensionData">The extension data.</param>
/// <exception cref="ArgumentNullException">If <paramref name="extensionData"/> is <see langword="null"/>.</exception>
protected ExtensibleJsonObject(IDictionary<string, JToken> extensionData)
{
if (extensionData == null)
throw new ArgumentNullException("extensionData");
if (extensionData.Count > 0)
_extensionData = new Dictionary<string, JToken>(extensionData);
}
/// <summary>
/// Initializes a new instance of the <see cref="ExtensibleJsonObject"/> class
/// with the specified extension data.
/// </summary>
/// <param name="extensionData">The extension data.</param>
/// <exception cref="ArgumentNullException">If <paramref name="extensionData"/> is <see langword="null"/>.</exception>
/// <exception cref="ArgumentException">If <paramref name="extensionData"/> contains any <see langword="null"/> values.</exception>
protected ExtensibleJsonObject(IEnumerable<JProperty> extensionData)
: this(extensionData.ToArray())
{
}
/// <summary>
/// Initializes a new instance of the <see cref="ExtensibleJsonObject"/> class
/// with the specified extension data.
/// </summary>
/// <param name="extensionData">The extension data.</param>
/// <exception cref="ArgumentNullException">If <paramref name="extensionData"/> is <see langword="null"/>.</exception>
/// <exception cref="ArgumentException">If <paramref name="extensionData"/> contains any <see langword="null"/> values.</exception>
protected ExtensibleJsonObject(params JProperty[] extensionData)
{
if (extensionData == null)
throw new ArgumentNullException("extensionData");
if (extensionData.Length > 0)
{
_extensionData = new Dictionary<string, JToken>();
foreach (JProperty property in extensionData)
{
if (property == null)
throw new ArgumentException("extensionData cannot contain any null values");
_extensionData[property.Name] = property.Value;
}
}
}
/// <summary>
/// Gets a map of object properties which did not map to another field or property
/// during JSON deserialization. The keys of the map represent the property names,
/// and the values are <see cref="JToken"/> instances containing the parsed JSON
/// values.
/// </summary>
/// <value>
/// A collection of object properties which did not map to another field or property
/// during JSON deserialization.
/// <para>-or-</para>
/// <para>An empty dictionary if the object did not contain any unmapped properties.</para>
/// </value>
public ReadOnlyDictionary<string, JToken> ExtensionData
{
get
{
if (_extensionData == null)
return EmptyExtensionData;
return new ReadOnlyDictionary<string, JToken>(_extensionData);
}
}
/// <summary>
/// This property exposes the <see cref="_extensionData"/> field to Json.NET as a dictionary with
/// <see cref="object"/> values instead of <see cref="JToken"/> values, which works around a known bug in the
/// way Json.NET 5.x handled <see langword="null"/> values in the extension data.
/// </summary>
[JsonExtensionData]
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private ExtensionDataDictionary ExtensionDataWrapper
{
get
{
// This can never return null or Json.NET will attempt to set the value.
return new ExtensionDataDictionary(this);
}
set
{
// This setter must exist or Json.NET will not recognize the extension data. It cannot be used because
// Json.NET will bypass the getter, resulting in a lost update.
throw new NotSupportedException("Attempted to set the extension data wrapper. See issue openstacknetsdk/openstack.net#419.");
}
}
/// <summary>
/// Converts an object to a <see cref="JToken"/>.
/// </summary>
/// <remarks>
/// <para>
/// Unlike <see cref="JToken.FromObject(object)"/>, this method supports <see langword="null"/> values.
/// </para>
/// </remarks>
/// <param name="obj">The object.</param>
/// <returns>
/// <para>The result of calling <see cref="JToken.FromObject(object)"/> on the input object.</para>
/// <para>-or-</para>
/// <para><see langword="null"/> if <paramref name="obj"/> is <see langword="null"/>.</para>
/// </returns>
private static JToken ToJToken(object obj)
{
if (obj == null)
return null;
return JToken.FromObject(obj);
}
/// <summary>
/// This class works around a known bug in Json.NET's handling of JSON extension data.
/// </summary>
/// <remarks>
/// <para>Adding values to the underlying dictionary requires converting the value to a <see cref="JToken"/> by
/// calling <see cref="ToJToken(object)"/>. Reading values does not require the inverse because the serializer
/// in Json.NET has no trouble handling <see cref="JToken"/> values as input.</para>
/// </remarks>
/// <seealso cref="ExtensionDataWrapper"/>
private sealed class ExtensionDataDictionary : IDictionary<string, object>
{
private readonly ExtensibleJsonObject _underlying;
[JsonConstructor]
private ExtensionDataDictionary()
{
// This constructor must exist or Json.NET will not be able to set the extension data. It cannot be used
// because Json.NET will not set the required _underlying field.
throw new NotSupportedException("Attempted to create the extension data wrapper with its underlying object. See issue openstacknetsdk/openstack.net#419.");
}
public ExtensionDataDictionary(ExtensibleJsonObject extensibleJsonObject)
{
if (extensibleJsonObject == null)
throw new ArgumentNullException("extensibleJsonObject");
_underlying = extensibleJsonObject;
}
public object this[string key]
{
get
{
return _underlying.ExtensionData[key];
}
set
{
GetOrCreateExtensionData()[key] = ToJToken(value);
}
}
public int Count
{
get
{
return _underlying.ExtensionData.Count;
}
}
public bool IsReadOnly
{
get
{
return false;
}
}
public ICollection<string> Keys
{
get
{
return _underlying.ExtensionData.Keys;
}
}
public ICollection<object> Values
{
get
{
return new ExtensionDataValues(_underlying.ExtensionData.Values);
}
}
public void Add(KeyValuePair<string, object> item)
{
IDictionary<string, JToken> extensionData = GetOrCreateExtensionData();
extensionData.Add(new KeyValuePair<string, JToken>(item.Key, ToJToken(item.Value)));
}
public void Add(string key, object value)
{
GetOrCreateExtensionData().Add(key, ToJToken(value));
}
public void Clear()
{
GetOrCreateExtensionData().Clear();
}
public bool Contains(KeyValuePair<string, object> item)
{
return _underlying.ExtensionData.Contains(new KeyValuePair<string, JToken>(item.Key, ToJToken(item.Value)));
}
public bool ContainsKey(string key)
{
return _underlying.ExtensionData.ContainsKey(key);
}
public void CopyTo(KeyValuePair<string, object>[] array, int arrayIndex)
{
IDictionary<string, object> intermediate = new Dictionary<string, object>(_underlying.ExtensionData.ToDictionary(i => i.Key, i => (object)i.Value));
intermediate.CopyTo(array, arrayIndex);
}
public IEnumerator<KeyValuePair<string, object>> GetEnumerator()
{
return _underlying.ExtensionData.Select(i => new KeyValuePair<string, object>(i.Key, i.Value)).GetEnumerator();
}
public bool Remove(KeyValuePair<string, object> item)
{
IDictionary<string, JToken> extensionData = _underlying._extensionData;
if (extensionData == null)
return false;
return extensionData.Remove(new KeyValuePair<string, JToken>(item.Key, ToJToken(item.Value)));
}
public bool Remove(string key)
{
var extensionData = _underlying._extensionData;
if (extensionData == null)
return false;
return extensionData.Remove(key);
}
public bool TryGetValue(string key, out object value)
{
JToken intermediate;
bool result = _underlying.ExtensionData.TryGetValue(key, out intermediate);
value = intermediate;
return result;
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
private Dictionary<string, JToken> GetOrCreateExtensionData()
{
var result = _underlying._extensionData;
if (result == null)
{
result = new Dictionary<string, JToken>();
_underlying._extensionData = result;
}
return result;
}
}
/// <summary>
/// This class works around a known bug in Json.NET's handling of JSON extension data.
/// </summary>
/// <seealso cref="ExtensionDataWrapper"/>
private class ExtensionDataValues : ICollection<object>
{
private readonly ICollection<JToken> _values;
public ExtensionDataValues(ICollection<JToken> values)
{
if (values == null)
throw new ArgumentNullException("values");
_values = values;
}
public int Count
{
get
{
return _values.Count;
}
}
public bool IsReadOnly
{
get
{
return _values.IsReadOnly;
}
}
public void Add(object item)
{
_values.Add(ToJToken(item));
}
public void Clear()
{
_values.Clear();
}
public bool Contains(object item)
{
return _values.Contains(ToJToken(item));
}
public void CopyTo(object[] array, int arrayIndex)
{
ICollection<object> intermediate = _values.ToArray();
intermediate.CopyTo(array, arrayIndex);
}
public IEnumerator<object> GetEnumerator()
{
return _values.Cast<object>().GetEnumerator();
}
public bool Remove(object item)
{
return _values.Remove(ToJToken(item));
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
}
}