You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

189 lines
7.3 KiB

  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. using System;
  26. using System.Collections.Generic;
  27. using System.Text;
  28. using System.Collections.ObjectModel;
  29. using Newtonsoft.Json.Utilities;
  30. using System.Globalization;
  31. namespace Newtonsoft.Json.Serialization
  32. {
  33. /// <summary>
  34. /// A collection of <see cref="JsonProperty"/> objects.
  35. /// </summary>
  36. internal class JsonPropertyCollection : KeyedCollection<string, JsonProperty>
  37. {
  38. private readonly Type _type;
  39. private readonly List<JsonProperty> _list;
  40. /// <summary>
  41. /// Initializes a new instance of the <see cref="JsonPropertyCollection"/> class.
  42. /// </summary>
  43. /// <param name="type">The type.</param>
  44. public JsonPropertyCollection(Type type)
  45. : base(StringComparer.Ordinal)
  46. {
  47. ValidationUtils.ArgumentNotNull(type, "type");
  48. _type = type;
  49. // foreach over List<T> to avoid boxing the Enumerator
  50. _list = (List<JsonProperty>)Items;
  51. }
  52. /// <summary>
  53. /// When implemented in a derived class, extracts the key from the specified element.
  54. /// </summary>
  55. /// <param name="item">The element from which to extract the key.</param>
  56. /// <returns>The key for the specified element.</returns>
  57. protected override string GetKeyForItem(JsonProperty item)
  58. {
  59. return item.PropertyName;
  60. }
  61. /// <summary>
  62. /// Adds a <see cref="JsonProperty"/> object.
  63. /// </summary>
  64. /// <param name="property">The property to add to the collection.</param>
  65. public void AddProperty(JsonProperty property)
  66. {
  67. if (Contains(property.PropertyName))
  68. {
  69. // don't overwrite existing property with ignored property
  70. if (property.Ignored)
  71. {
  72. return;
  73. }
  74. JsonProperty existingProperty = this[property.PropertyName];
  75. bool duplicateProperty = true;
  76. if (existingProperty.Ignored)
  77. {
  78. // remove ignored property so it can be replaced in collection
  79. Remove(existingProperty);
  80. duplicateProperty = false;
  81. }
  82. else
  83. {
  84. if (property.DeclaringType != null && existingProperty.DeclaringType != null)
  85. {
  86. if (property.DeclaringType.IsSubclassOf(existingProperty.DeclaringType)
  87. || (existingProperty.DeclaringType.IsInterface() && property.DeclaringType.ImplementInterface(existingProperty.DeclaringType)))
  88. {
  89. // current property is on a derived class and hides the existing
  90. Remove(existingProperty);
  91. duplicateProperty = false;
  92. }
  93. if (existingProperty.DeclaringType.IsSubclassOf(property.DeclaringType)
  94. || (property.DeclaringType.IsInterface() && existingProperty.DeclaringType.ImplementInterface(property.DeclaringType)))
  95. {
  96. // current property is hidden by the existing so don't add it
  97. return;
  98. }
  99. if (_type.ImplementInterface(existingProperty.DeclaringType) && _type.ImplementInterface(property.DeclaringType))
  100. {
  101. // current property was already defined on another interface
  102. return;
  103. }
  104. }
  105. }
  106. if (duplicateProperty)
  107. {
  108. throw new JsonSerializationException("A member with the name '{0}' already exists on '{1}'. Use the JsonPropertyAttribute to specify another name.".FormatWith(CultureInfo.InvariantCulture, property.PropertyName, _type));
  109. }
  110. }
  111. Add(property);
  112. }
  113. /// <summary>
  114. /// Gets the closest matching <see cref="JsonProperty"/> object.
  115. /// First attempts to get an exact case match of <paramref name="propertyName"/> and then
  116. /// a case insensitive match.
  117. /// </summary>
  118. /// <param name="propertyName">Name of the property.</param>
  119. /// <returns>A matching property if found.</returns>
  120. public JsonProperty GetClosestMatchProperty(string propertyName)
  121. {
  122. JsonProperty property = GetProperty(propertyName, StringComparison.Ordinal);
  123. if (property == null)
  124. {
  125. property = GetProperty(propertyName, StringComparison.OrdinalIgnoreCase);
  126. }
  127. return property;
  128. }
  129. #if NETFX || NETSTANDARD2_0
  130. private bool TryGetValue(string key, out JsonProperty item)
  131. #else
  132. private new bool TryGetValue(string key, out JsonProperty item)
  133. #endif
  134. {
  135. if (Dictionary == null)
  136. {
  137. item = default(JsonProperty);
  138. return false;
  139. }
  140. return Dictionary.TryGetValue(key, out item);
  141. }
  142. /// <summary>
  143. /// Gets a property by property name.
  144. /// </summary>
  145. /// <param name="propertyName">The name of the property to get.</param>
  146. /// <param name="comparisonType">Type property name string comparison.</param>
  147. /// <returns>A matching property if found.</returns>
  148. public JsonProperty GetProperty(string propertyName, StringComparison comparisonType)
  149. {
  150. // KeyedCollection has an ordinal comparer
  151. if (comparisonType == StringComparison.Ordinal)
  152. {
  153. if (TryGetValue(propertyName, out JsonProperty property))
  154. {
  155. return property;
  156. }
  157. return null;
  158. }
  159. for (int i = 0; i < _list.Count; i++)
  160. {
  161. JsonProperty property = _list[i];
  162. if (string.Equals(propertyName, property.PropertyName, comparisonType))
  163. {
  164. return property;
  165. }
  166. }
  167. return null;
  168. }
  169. }
  170. }