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.

332 lines
15 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 Newtonsoft.Json.Utilities;
  28. using System.Globalization;
  29. #if NET20
  30. using Newtonsoft.Json.Utilities.LinqBridge;
  31. #else
  32. using System.Linq;
  33. #endif
  34. namespace Newtonsoft.Json.Linq
  35. {
  36. /// <summary>
  37. /// Contains the LINQ to JSON extension methods.
  38. /// </summary>
  39. internal static class Extensions
  40. {
  41. /// <summary>
  42. /// Returns a collection of tokens that contains the ancestors of every token in the source collection.
  43. /// </summary>
  44. /// <typeparam name="T">The type of the objects in source, constrained to <see cref="JToken"/>.</typeparam>
  45. /// <param name="source">An <see cref="IEnumerable{T}"/> of <see cref="JToken"/> that contains the source collection.</param>
  46. /// <returns>An <see cref="IEnumerable{T}"/> of <see cref="JToken"/> that contains the ancestors of every token in the source collection.</returns>
  47. public static IJEnumerable<JToken> Ancestors<T>(this IEnumerable<T> source) where T : JToken
  48. {
  49. ValidationUtils.ArgumentNotNull(source, nameof(source));
  50. return source.SelectMany(j => j.Ancestors()).AsJEnumerable();
  51. }
  52. /// <summary>
  53. /// Returns a collection of tokens that contains every token in the source collection, and the ancestors of every token in the source collection.
  54. /// </summary>
  55. /// <typeparam name="T">The type of the objects in source, constrained to <see cref="JToken"/>.</typeparam>
  56. /// <param name="source">An <see cref="IEnumerable{T}"/> of <see cref="JToken"/> that contains the source collection.</param>
  57. /// <returns>An <see cref="IEnumerable{T}"/> of <see cref="JToken"/> that contains every token in the source collection, the ancestors of every token in the source collection.</returns>
  58. public static IJEnumerable<JToken> AncestorsAndSelf<T>(this IEnumerable<T> source) where T : JToken
  59. {
  60. ValidationUtils.ArgumentNotNull(source, nameof(source));
  61. return source.SelectMany(j => j.AncestorsAndSelf()).AsJEnumerable();
  62. }
  63. /// <summary>
  64. /// Returns a collection of tokens that contains the descendants of every token in the source collection.
  65. /// </summary>
  66. /// <typeparam name="T">The type of the objects in source, constrained to <see cref="JContainer"/>.</typeparam>
  67. /// <param name="source">An <see cref="IEnumerable{T}"/> of <see cref="JToken"/> that contains the source collection.</param>
  68. /// <returns>An <see cref="IEnumerable{T}"/> of <see cref="JToken"/> that contains the descendants of every token in the source collection.</returns>
  69. public static IJEnumerable<JToken> Descendants<T>(this IEnumerable<T> source) where T : JContainer
  70. {
  71. ValidationUtils.ArgumentNotNull(source, nameof(source));
  72. return source.SelectMany(j => j.Descendants()).AsJEnumerable();
  73. }
  74. /// <summary>
  75. /// Returns a collection of tokens that contains every token in the source collection, and the descendants of every token in the source collection.
  76. /// </summary>
  77. /// <typeparam name="T">The type of the objects in source, constrained to <see cref="JContainer"/>.</typeparam>
  78. /// <param name="source">An <see cref="IEnumerable{T}"/> of <see cref="JToken"/> that contains the source collection.</param>
  79. /// <returns>An <see cref="IEnumerable{T}"/> of <see cref="JToken"/> that contains every token in the source collection, and the descendants of every token in the source collection.</returns>
  80. public static IJEnumerable<JToken> DescendantsAndSelf<T>(this IEnumerable<T> source) where T : JContainer
  81. {
  82. ValidationUtils.ArgumentNotNull(source, nameof(source));
  83. return source.SelectMany(j => j.DescendantsAndSelf()).AsJEnumerable();
  84. }
  85. /// <summary>
  86. /// Returns a collection of child properties of every object in the source collection.
  87. /// </summary>
  88. /// <param name="source">An <see cref="IEnumerable{T}"/> of <see cref="JObject"/> that contains the source collection.</param>
  89. /// <returns>An <see cref="IEnumerable{T}"/> of <see cref="JProperty"/> that contains the properties of every object in the source collection.</returns>
  90. public static IJEnumerable<JProperty> Properties(this IEnumerable<JObject> source)
  91. {
  92. ValidationUtils.ArgumentNotNull(source, nameof(source));
  93. return source.SelectMany(d => d.Properties()).AsJEnumerable();
  94. }
  95. /// <summary>
  96. /// Returns a collection of child values of every object in the source collection with the given key.
  97. /// </summary>
  98. /// <param name="source">An <see cref="IEnumerable{T}"/> of <see cref="JToken"/> that contains the source collection.</param>
  99. /// <param name="key">The token key.</param>
  100. /// <returns>An <see cref="IEnumerable{T}"/> of <see cref="JToken"/> that contains the values of every token in the source collection with the given key.</returns>
  101. public static IJEnumerable<JToken> Values(this IEnumerable<JToken> source, object key)
  102. {
  103. return Values<JToken, JToken>(source, key).AsJEnumerable();
  104. }
  105. /// <summary>
  106. /// Returns a collection of child values of every object in the source collection.
  107. /// </summary>
  108. /// <param name="source">An <see cref="IEnumerable{T}"/> of <see cref="JToken"/> that contains the source collection.</param>
  109. /// <returns>An <see cref="IEnumerable{T}"/> of <see cref="JToken"/> that contains the values of every token in the source collection.</returns>
  110. public static IJEnumerable<JToken> Values(this IEnumerable<JToken> source)
  111. {
  112. return source.Values(null);
  113. }
  114. /// <summary>
  115. /// Returns a collection of converted child values of every object in the source collection with the given key.
  116. /// </summary>
  117. /// <typeparam name="U">The type to convert the values to.</typeparam>
  118. /// <param name="source">An <see cref="IEnumerable{T}"/> of <see cref="JToken"/> that contains the source collection.</param>
  119. /// <param name="key">The token key.</param>
  120. /// <returns>An <see cref="IEnumerable{T}"/> that contains the converted values of every token in the source collection with the given key.</returns>
  121. public static IEnumerable<U> Values<U>(this IEnumerable<JToken> source, object key)
  122. {
  123. return Values<JToken, U>(source, key);
  124. }
  125. /// <summary>
  126. /// Returns a collection of converted child values of every object in the source collection.
  127. /// </summary>
  128. /// <typeparam name="U">The type to convert the values to.</typeparam>
  129. /// <param name="source">An <see cref="IEnumerable{T}"/> of <see cref="JToken"/> that contains the source collection.</param>
  130. /// <returns>An <see cref="IEnumerable{T}"/> that contains the converted values of every token in the source collection.</returns>
  131. public static IEnumerable<U> Values<U>(this IEnumerable<JToken> source)
  132. {
  133. return Values<JToken, U>(source, null);
  134. }
  135. /// <summary>
  136. /// Converts the value.
  137. /// </summary>
  138. /// <typeparam name="U">The type to convert the value to.</typeparam>
  139. /// <param name="value">A <see cref="JToken"/> cast as a <see cref="IEnumerable{T}"/> of <see cref="JToken"/>.</param>
  140. /// <returns>A converted value.</returns>
  141. public static U Value<U>(this IEnumerable<JToken> value)
  142. {
  143. return value.Value<JToken, U>();
  144. }
  145. /// <summary>
  146. /// Converts the value.
  147. /// </summary>
  148. /// <typeparam name="T">The source collection type.</typeparam>
  149. /// <typeparam name="U">The type to convert the value to.</typeparam>
  150. /// <param name="value">A <see cref="JToken"/> cast as a <see cref="IEnumerable{T}"/> of <see cref="JToken"/>.</param>
  151. /// <returns>A converted value.</returns>
  152. public static U Value<T, U>(this IEnumerable<T> value) where T : JToken
  153. {
  154. ValidationUtils.ArgumentNotNull(value, nameof(value));
  155. if (!(value is JToken token))
  156. {
  157. throw new ArgumentException("Source value must be a JToken.");
  158. }
  159. return token.Convert<JToken, U>();
  160. }
  161. internal static IEnumerable<U> Values<T, U>(this IEnumerable<T> source, object key) where T : JToken
  162. {
  163. ValidationUtils.ArgumentNotNull(source, nameof(source));
  164. if (key == null)
  165. {
  166. foreach (T token in source)
  167. {
  168. JValue value = token as JValue;
  169. if (value != null)
  170. {
  171. yield return Convert<JValue, U>(value);
  172. }
  173. else
  174. {
  175. foreach (JToken t in token.Children())
  176. {
  177. yield return t.Convert<JToken, U>();
  178. }
  179. }
  180. }
  181. }
  182. else
  183. {
  184. foreach (T token in source)
  185. {
  186. JToken value = token[key];
  187. if (value != null)
  188. {
  189. yield return value.Convert<JToken, U>();
  190. }
  191. }
  192. }
  193. }
  194. //TODO
  195. //public static IEnumerable<T> InDocumentOrder<T>(this IEnumerable<T> source) where T : JObject;
  196. /// <summary>
  197. /// Returns a collection of child tokens of every array in the source collection.
  198. /// </summary>
  199. /// <typeparam name="T">The source collection type.</typeparam>
  200. /// <param name="source">An <see cref="IEnumerable{T}"/> of <see cref="JToken"/> that contains the source collection.</param>
  201. /// <returns>An <see cref="IEnumerable{T}"/> of <see cref="JToken"/> that contains the values of every token in the source collection.</returns>
  202. public static IJEnumerable<JToken> Children<T>(this IEnumerable<T> source) where T : JToken
  203. {
  204. return Children<T, JToken>(source).AsJEnumerable();
  205. }
  206. /// <summary>
  207. /// Returns a collection of converted child tokens of every array in the source collection.
  208. /// </summary>
  209. /// <param name="source">An <see cref="IEnumerable{T}"/> of <see cref="JToken"/> that contains the source collection.</param>
  210. /// <typeparam name="U">The type to convert the values to.</typeparam>
  211. /// <typeparam name="T">The source collection type.</typeparam>
  212. /// <returns>An <see cref="IEnumerable{T}"/> that contains the converted values of every token in the source collection.</returns>
  213. public static IEnumerable<U> Children<T, U>(this IEnumerable<T> source) where T : JToken
  214. {
  215. ValidationUtils.ArgumentNotNull(source, nameof(source));
  216. return source.SelectMany(c => c.Children()).Convert<JToken, U>();
  217. }
  218. internal static IEnumerable<U> Convert<T, U>(this IEnumerable<T> source) where T : JToken
  219. {
  220. ValidationUtils.ArgumentNotNull(source, nameof(source));
  221. foreach (T token in source)
  222. {
  223. yield return Convert<JToken, U>(token);
  224. }
  225. }
  226. internal static U Convert<T, U>(this T token) where T : JToken
  227. {
  228. if (token == null)
  229. {
  230. return default(U);
  231. }
  232. if (token is U
  233. // don't want to cast JValue to its interfaces, want to get the internal value
  234. && typeof(U) != typeof(IComparable) && typeof(U) != typeof(IFormattable))
  235. {
  236. // HACK
  237. return (U)(object)token;
  238. }
  239. else
  240. {
  241. JValue value = token as JValue;
  242. if (value == null)
  243. {
  244. throw new InvalidCastException("Cannot cast {0} to {1}.".FormatWith(CultureInfo.InvariantCulture, token.GetType(), typeof(T)));
  245. }
  246. if (value.Value is U u)
  247. {
  248. return u;
  249. }
  250. Type targetType = typeof(U);
  251. if (ReflectionUtils.IsNullableType(targetType))
  252. {
  253. if (value.Value == null)
  254. {
  255. return default(U);
  256. }
  257. targetType = Nullable.GetUnderlyingType(targetType);
  258. }
  259. return (U)System.Convert.ChangeType(value.Value, targetType, CultureInfo.InvariantCulture);
  260. }
  261. }
  262. //TODO
  263. //public static void Remove<T>(this IEnumerable<T> source) where T : JContainer;
  264. /// <summary>
  265. /// Returns the input typed as <see cref="IJEnumerable{T}"/>.
  266. /// </summary>
  267. /// <param name="source">An <see cref="IEnumerable{T}"/> of <see cref="JToken"/> that contains the source collection.</param>
  268. /// <returns>The input typed as <see cref="IJEnumerable{T}"/>.</returns>
  269. public static IJEnumerable<JToken> AsJEnumerable(this IEnumerable<JToken> source)
  270. {
  271. return source.AsJEnumerable<JToken>();
  272. }
  273. /// <summary>
  274. /// Returns the input typed as <see cref="IJEnumerable{T}"/>.
  275. /// </summary>
  276. /// <typeparam name="T">The source collection type.</typeparam>
  277. /// <param name="source">An <see cref="IEnumerable{T}"/> of <see cref="JToken"/> that contains the source collection.</param>
  278. /// <returns>The input typed as <see cref="IJEnumerable{T}"/>.</returns>
  279. public static IJEnumerable<T> AsJEnumerable<T>(this IEnumerable<T> source) where T : JToken
  280. {
  281. if (source == null)
  282. {
  283. return null;
  284. }
  285. else if (source is IJEnumerable<T>)
  286. {
  287. return (IJEnumerable<T>)source;
  288. }
  289. else
  290. {
  291. return new JEnumerable<T>(source);
  292. }
  293. }
  294. }
  295. }