Extensions
Contains the LINQ to JSON extension methods.
using Newtonsoft.Json.Utilities;
using Newtonsoft.Json.Utilities.LinqBridge;
using System;
using System.Collections.Generic;
using System.Globalization;
namespace Newtonsoft.Json.Linq
{
public static class Extensions
{
public static IJEnumerable<JToken> Ancestors<T>(this IEnumerable<T> source) where T : JToken
{
ValidationUtils.ArgumentNotNull(source, "source");
return source.SelectMany((T j) => j.Ancestors()).AsJEnumerable();
}
public static IJEnumerable<JToken> AncestorsAndSelf<T>(this IEnumerable<T> source) where T : JToken
{
ValidationUtils.ArgumentNotNull(source, "source");
return source.SelectMany((T j) => j.AncestorsAndSelf()).AsJEnumerable();
}
public static IJEnumerable<JToken> Descendants<T>(this IEnumerable<T> source) where T : JContainer
{
ValidationUtils.ArgumentNotNull(source, "source");
return source.SelectMany((T j) => j.Descendants()).AsJEnumerable();
}
public static IJEnumerable<JToken> DescendantsAndSelf<T>(this IEnumerable<T> source) where T : JContainer
{
ValidationUtils.ArgumentNotNull(source, "source");
return source.SelectMany((T j) => j.DescendantsAndSelf()).AsJEnumerable();
}
public static IJEnumerable<JProperty> Properties(this IEnumerable<JObject> source)
{
ValidationUtils.ArgumentNotNull(source, "source");
return source.SelectMany((JObject d) => d.Properties()).AsJEnumerable();
}
public static IJEnumerable<JToken> Values(this IEnumerable<JToken> source, object key)
{
return source.Values<JToken, JToken>(key).AsJEnumerable();
}
public static IJEnumerable<JToken> Values(this IEnumerable<JToken> source)
{
return source.Values(null);
}
public static IEnumerable<U> Values<U>(this IEnumerable<JToken> source, object key)
{
return source.Values<JToken, U>(key);
}
public static IEnumerable<U> Values<U>(this IEnumerable<JToken> source)
{
return source.Values<JToken, U>(null);
}
public static U Value<U>(this IEnumerable<JToken> value)
{
return value.Value<JToken, U>();
}
public static U Value<T, U>(this IEnumerable<T> value) where T : JToken
{
ValidationUtils.ArgumentNotNull(value, "value");
JToken obj = value as JToken;
if (obj == null)
throw new ArgumentException("Source value must be a JToken.");
return obj.Convert<JToken, U>();
}
internal static IEnumerable<U> Values<T, U>(this IEnumerable<T> source, object key) where T : JToken
{
ValidationUtils.ArgumentNotNull(source, "source");
if (key == null) {
foreach (T item in source) {
JValue token;
if ((token = (item as JValue)) != null)
yield return Extensions.Convert<JValue, U>(token);
else {
foreach (JToken item2 in item.Children()) {
yield return Extensions.Convert<JToken, U>(item2);
}
}
}
} else {
foreach (T item3 in source) {
JToken jToken = item3[key];
if (jToken != null)
yield return Extensions.Convert<JToken, U>(jToken);
}
}
}
public static IJEnumerable<JToken> Children<T>(this IEnumerable<T> source) where T : JToken
{
return source.Children<T, JToken>().AsJEnumerable();
}
public static IEnumerable<U> Children<T, U>(this IEnumerable<T> source) where T : JToken
{
ValidationUtils.ArgumentNotNull(source, "source");
return source.SelectMany((T c) => c.Children()).Convert<JToken, U>();
}
internal static IEnumerable<U> Convert<T, U>(this IEnumerable<T> source) where T : JToken
{
ValidationUtils.ArgumentNotNull(source, "source");
foreach (T item in source) {
yield return Extensions.Convert<JToken, U>((JToken)item);
}
}
internal static U Convert<T, U>(this T token) where T : JToken
{
if (token == null)
return default(U);
T val;
if ((val = token) is U) {
U result = (U)(object)val;
if ((object)typeof(U) != typeof(IComparable) && (object)typeof(U) != typeof(IFormattable))
return result;
}
JValue jValue;
if ((jValue = (token as JValue)) == null)
throw new InvalidCastException("Cannot cast {0} to {1}.".FormatWith(CultureInfo.InvariantCulture, token.GetType(), typeof(T)));
object value;
if ((value = jValue.Value) is U)
return (U)value;
Type type = typeof(U);
if (ReflectionUtils.IsNullableType(type)) {
if (jValue.Value == null)
return default(U);
type = Nullable.GetUnderlyingType(type);
}
return (U)System.Convert.ChangeType(jValue.Value, type, CultureInfo.InvariantCulture);
}
public static IJEnumerable<JToken> AsJEnumerable(this IEnumerable<JToken> source)
{
return source.AsJEnumerable<JToken>();
}
public static IJEnumerable<T> AsJEnumerable<T>(this IEnumerable<T> source) where T : JToken
{
if (source == null)
return null;
IJEnumerable<T> result;
if ((result = (source as IJEnumerable<T>)) != null)
return result;
return new JEnumerable<T>(source);
}
}
}