PageRenderTime 124ms CodeModel.GetById 60ms RepoModel.GetById 1ms app.codeStats 0ms

/Net.SuddenElfilio.RilSharp/JSON/Linq/Extensions.cs

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