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.

281 lines
7.4 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.Collections.ObjectModel;
  28. using Newtonsoft.Json.Utilities;
  29. namespace Newtonsoft.Json.Linq
  30. {
  31. internal class JPropertyKeyedCollection : Collection<JToken>
  32. {
  33. private static readonly IEqualityComparer<string> Comparer = StringComparer.Ordinal;
  34. private Dictionary<string, JToken> _dictionary;
  35. public JPropertyKeyedCollection() : base(new List<JToken>())
  36. {
  37. }
  38. private void AddKey(string key, JToken item)
  39. {
  40. EnsureDictionary();
  41. _dictionary[key] = item;
  42. }
  43. protected void ChangeItemKey(JToken item, string newKey)
  44. {
  45. if (!ContainsItem(item))
  46. {
  47. throw new ArgumentException("The specified item does not exist in this KeyedCollection.");
  48. }
  49. string keyForItem = GetKeyForItem(item);
  50. if (!Comparer.Equals(keyForItem, newKey))
  51. {
  52. if (newKey != null)
  53. {
  54. AddKey(newKey, item);
  55. }
  56. if (keyForItem != null)
  57. {
  58. RemoveKey(keyForItem);
  59. }
  60. }
  61. }
  62. protected override void ClearItems()
  63. {
  64. base.ClearItems();
  65. _dictionary?.Clear();
  66. }
  67. public bool Contains(string key)
  68. {
  69. if (key == null)
  70. {
  71. throw new ArgumentNullException(nameof(key));
  72. }
  73. if (_dictionary != null)
  74. {
  75. return _dictionary.ContainsKey(key);
  76. }
  77. return false;
  78. }
  79. private bool ContainsItem(JToken item)
  80. {
  81. if (_dictionary == null)
  82. {
  83. return false;
  84. }
  85. string key = GetKeyForItem(item);
  86. return _dictionary.TryGetValue(key, out _);
  87. }
  88. private void EnsureDictionary()
  89. {
  90. if (_dictionary == null)
  91. {
  92. _dictionary = new Dictionary<string, JToken>(Comparer);
  93. }
  94. }
  95. private string GetKeyForItem(JToken item)
  96. {
  97. return ((JProperty)item).Name;
  98. }
  99. protected override void InsertItem(int index, JToken item)
  100. {
  101. AddKey(GetKeyForItem(item), item);
  102. base.InsertItem(index, item);
  103. }
  104. public bool Remove(string key)
  105. {
  106. if (key == null)
  107. {
  108. throw new ArgumentNullException(nameof(key));
  109. }
  110. if (_dictionary != null)
  111. {
  112. return _dictionary.ContainsKey(key) && Remove(_dictionary[key]);
  113. }
  114. return false;
  115. }
  116. protected override void RemoveItem(int index)
  117. {
  118. string keyForItem = GetKeyForItem(Items[index]);
  119. RemoveKey(keyForItem);
  120. base.RemoveItem(index);
  121. }
  122. private void RemoveKey(string key)
  123. {
  124. _dictionary?.Remove(key);
  125. }
  126. protected override void SetItem(int index, JToken item)
  127. {
  128. string keyForItem = GetKeyForItem(item);
  129. string keyAtIndex = GetKeyForItem(Items[index]);
  130. if (Comparer.Equals(keyAtIndex, keyForItem))
  131. {
  132. if (_dictionary != null)
  133. {
  134. _dictionary[keyForItem] = item;
  135. }
  136. }
  137. else
  138. {
  139. AddKey(keyForItem, item);
  140. if (keyAtIndex != null)
  141. {
  142. RemoveKey(keyAtIndex);
  143. }
  144. }
  145. base.SetItem(index, item);
  146. }
  147. public JToken this[string key]
  148. {
  149. get
  150. {
  151. if (key == null)
  152. {
  153. throw new ArgumentNullException(nameof(key));
  154. }
  155. if (_dictionary != null)
  156. {
  157. return _dictionary[key];
  158. }
  159. throw new KeyNotFoundException();
  160. }
  161. }
  162. public bool TryGetValue(string key, out JToken value)
  163. {
  164. if (_dictionary == null)
  165. {
  166. value = null;
  167. return false;
  168. }
  169. return _dictionary.TryGetValue(key, out value);
  170. }
  171. public ICollection<string> Keys
  172. {
  173. get
  174. {
  175. EnsureDictionary();
  176. return _dictionary.Keys;
  177. }
  178. }
  179. public ICollection<JToken> Values
  180. {
  181. get
  182. {
  183. EnsureDictionary();
  184. return _dictionary.Values;
  185. }
  186. }
  187. public int IndexOfReference(JToken t)
  188. {
  189. return ((List<JToken>)Items).IndexOfReference(t);
  190. }
  191. public bool Compare(JPropertyKeyedCollection other)
  192. {
  193. if (this == other)
  194. {
  195. return true;
  196. }
  197. // dictionaries in JavaScript aren't ordered
  198. // ignore order when comparing properties
  199. Dictionary<string, JToken> d1 = _dictionary;
  200. Dictionary<string, JToken> d2 = other._dictionary;
  201. if (d1 == null && d2 == null)
  202. {
  203. return true;
  204. }
  205. if (d1 == null)
  206. {
  207. return (d2.Count == 0);
  208. }
  209. if (d2 == null)
  210. {
  211. return (d1.Count == 0);
  212. }
  213. if (d1.Count != d2.Count)
  214. {
  215. return false;
  216. }
  217. foreach (KeyValuePair<string, JToken> keyAndProperty in d1)
  218. {
  219. if (!d2.TryGetValue(keyAndProperty.Key, out JToken secondValue))
  220. {
  221. return false;
  222. }
  223. JProperty p1 = (JProperty)keyAndProperty.Value;
  224. JProperty p2 = (JProperty)secondValue;
  225. if (p1.Value == null)
  226. {
  227. return (p2.Value == null);
  228. }
  229. if (!p1.Value.DeepEquals(p2.Value))
  230. {
  231. return false;
  232. }
  233. }
  234. return true;
  235. }
  236. }
  237. }