|
| 1 | +namespace net.openstack.Core.Domain |
| 2 | +{ |
| 3 | + using System; |
| 4 | + using System.Collections.Generic; |
| 5 | + using System.Linq; |
| 6 | + using net.openstack.Core.Collections; |
| 7 | + using Newtonsoft.Json; |
| 8 | + using Newtonsoft.Json.Linq; |
| 9 | + |
| 10 | + /// <summary> |
| 11 | + /// This is the abstract base class for types modeling the JSON representation of a resource |
| 12 | + /// which may be extended by specific providers or updated in future releases of a core |
| 13 | + /// service. |
| 14 | + /// </summary> |
| 15 | + /// <threadsafety static="true" instance="false"/> |
| 16 | + /// <preliminary/> |
| 17 | + [JsonObject(MemberSerialization.OptIn)] |
| 18 | + public abstract class ExtensibleJsonObject |
| 19 | + { |
| 20 | + /// <summary> |
| 21 | + /// An empty, and thus immutable, value which is the default return value |
| 22 | + /// for <see cref="ExtensionData"/> when the backing field is <see langword="null"/>. |
| 23 | + /// </summary> |
| 24 | + protected static readonly ReadOnlyDictionary<string, JToken> EmptyExtensionData = |
| 25 | + new ReadOnlyDictionary<string, JToken>(new Dictionary<string, JToken>()); |
| 26 | + |
| 27 | + /// <summary> |
| 28 | + /// This is the backing field for the <see cref="ExtensionData"/> property. |
| 29 | + /// </summary> |
| 30 | + [JsonExtensionData] |
| 31 | + private Dictionary<string, JToken> _extensionData; |
| 32 | + |
| 33 | + /// <summary> |
| 34 | + /// Initializes a new instance of the <see cref="ExtensibleJsonObject"/> class |
| 35 | + /// during JSON deserialization. |
| 36 | + /// </summary> |
| 37 | + [JsonConstructor] |
| 38 | + protected ExtensibleJsonObject() |
| 39 | + { |
| 40 | + } |
| 41 | + |
| 42 | + /// <summary> |
| 43 | + /// Initializes a new instance of the <see cref="ExtensibleJsonObject"/> class |
| 44 | + /// with the specified extension data. |
| 45 | + /// </summary> |
| 46 | + /// <param name="extensionData">The extension data.</param> |
| 47 | + /// <exception cref="ArgumentNullException">If <paramref name="extensionData"/> is <see langword="null"/>.</exception> |
| 48 | + protected ExtensibleJsonObject(IDictionary<string, JToken> extensionData) |
| 49 | + { |
| 50 | + if (extensionData == null) |
| 51 | + throw new ArgumentNullException("extensionData"); |
| 52 | + |
| 53 | + if (extensionData.Count > 0) |
| 54 | + _extensionData = new Dictionary<string, JToken>(extensionData); |
| 55 | + } |
| 56 | + |
| 57 | + /// <summary> |
| 58 | + /// Initializes a new instance of the <see cref="ExtensibleJsonObject"/> class |
| 59 | + /// with the specified extension data. |
| 60 | + /// </summary> |
| 61 | + /// <param name="extensionData">The extension data.</param> |
| 62 | + /// <exception cref="ArgumentNullException">If <paramref name="extensionData"/> is <see langword="null"/>.</exception> |
| 63 | + /// <exception cref="ArgumentException">If <paramref name="extensionData"/> contains any <see langword="null"/> values.</exception> |
| 64 | + protected ExtensibleJsonObject(IEnumerable<JProperty> extensionData) |
| 65 | + : this(extensionData.ToArray()) |
| 66 | + { |
| 67 | + } |
| 68 | + |
| 69 | + /// <summary> |
| 70 | + /// Initializes a new instance of the <see cref="ExtensibleJsonObject"/> class |
| 71 | + /// with the specified extension data. |
| 72 | + /// </summary> |
| 73 | + /// <param name="extensionData">The extension data.</param> |
| 74 | + /// <exception cref="ArgumentNullException">If <paramref name="extensionData"/> is <see langword="null"/>.</exception> |
| 75 | + /// <exception cref="ArgumentException">If <paramref name="extensionData"/> contains any <see langword="null"/> values.</exception> |
| 76 | + protected ExtensibleJsonObject(params JProperty[] extensionData) |
| 77 | + { |
| 78 | + if (extensionData == null) |
| 79 | + throw new ArgumentNullException("extensionData"); |
| 80 | + |
| 81 | + if (extensionData.Length > 0) |
| 82 | + { |
| 83 | + _extensionData = new Dictionary<string, JToken>(); |
| 84 | + foreach (JProperty property in extensionData) |
| 85 | + { |
| 86 | + if (property == null) |
| 87 | + throw new ArgumentException("extensionData cannot contain any null values"); |
| 88 | + |
| 89 | + _extensionData[property.Name] = property.Value; |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + /// <summary> |
| 95 | + /// Gets a map of object properties which did not map to another field or property |
| 96 | + /// during JSON deserialization. The keys of the map represent the property names, |
| 97 | + /// and the values are <see cref="JToken"/> instances containing the parsed JSON |
| 98 | + /// values. |
| 99 | + /// </summary> |
| 100 | + /// <value> |
| 101 | + /// A collection of object properties which did not map to another field or property |
| 102 | + /// during JSON deserialization. |
| 103 | + /// <para>-or-</para> |
| 104 | + /// <para>An empty dictionary if the object did not contain any unmapped properties.</para> |
| 105 | + /// </value> |
| 106 | + public ReadOnlyDictionary<string, JToken> ExtensionData |
| 107 | + { |
| 108 | + get |
| 109 | + { |
| 110 | + if (_extensionData == null) |
| 111 | + return EmptyExtensionData; |
| 112 | + |
| 113 | + return new ReadOnlyDictionary<string, JToken>(_extensionData); |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | +} |
0 commit comments