Extensions
Contains the LINQ to JSON extension methods.
using Newtonsoft.Json.Utilities;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Runtime.CompilerServices;
namespace Newtonsoft.Json.Linq
{
[System.Runtime.CompilerServices.NullableContext(1)]
[System.Runtime.CompilerServices.Nullable(0)]
public static class Extensions
{
public static IJEnumerable<JToken> Ancestors<[System.Runtime.CompilerServices.Nullable(0)] 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<[System.Runtime.CompilerServices.Nullable(0)] 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<[System.Runtime.CompilerServices.Nullable(0)] 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<[System.Runtime.CompilerServices.Nullable(0)] 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, [System.Runtime.CompilerServices.Nullable(2)] object key)
{
return source.Values<JToken, JToken>(key).AsJEnumerable();
}
public static IJEnumerable<JToken> Values(this IEnumerable<JToken> source)
{
return source.Values(null);
}
[return: System.Runtime.CompilerServices.Nullable(new byte[] {
1,
2
})]
public static IEnumerable<U> Values<[System.Runtime.CompilerServices.Nullable(2)] U>(this IEnumerable<JToken> source, object key)
{
return source.Values<JToken, U>(key);
}
[return: System.Runtime.CompilerServices.Nullable(new byte[] {
1,
2
})]
public static IEnumerable<U> Values<[System.Runtime.CompilerServices.Nullable(2)] U>(this IEnumerable<JToken> source)
{
return source.Values<JToken, U>(null);
}
[System.Runtime.CompilerServices.NullableContext(2)]
public static U Value<U>([System.Runtime.CompilerServices.Nullable(1)] this IEnumerable<JToken> value)
{
return value.Value<JToken, U>();
}
[return: System.Runtime.CompilerServices.Nullable(2)]
public static U Value<[System.Runtime.CompilerServices.Nullable(0)] T, [System.Runtime.CompilerServices.Nullable(2)] 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>();
}
[IteratorStateMachine(typeof(<Values>d__11<, >))]
[return: System.Runtime.CompilerServices.Nullable(new byte[] {
1,
2
})]
internal static IEnumerable<U> Values<[System.Runtime.CompilerServices.Nullable(0)] T, [System.Runtime.CompilerServices.Nullable(2)] U>(this IEnumerable<T> source, [System.Runtime.CompilerServices.Nullable(2)] object key) where T : JToken
{
<Values>d__11<T, U> <Values>d__ = new <Values>d__11<T, U>(-2);
<Values>d__.<>3__source = source;
<Values>d__.<>3__key = key;
return <Values>d__;
}
public static IJEnumerable<JToken> Children<[System.Runtime.CompilerServices.Nullable(0)] T>(this IEnumerable<T> source) where T : JToken
{
return source.Children<T, JToken>().AsJEnumerable();
}
[return: System.Runtime.CompilerServices.Nullable(new byte[] {
1,
2
})]
public static IEnumerable<U> Children<[System.Runtime.CompilerServices.Nullable(0)] T, [System.Runtime.CompilerServices.Nullable(2)] U>(this IEnumerable<T> source) where T : JToken
{
ValidationUtils.ArgumentNotNull(source, "source");
return source.SelectMany((T c) => c.Children()).Convert<JToken, U>();
}
[IteratorStateMachine(typeof(<Convert>d__14<, >))]
[return: System.Runtime.CompilerServices.Nullable(new byte[] {
1,
2
})]
internal static IEnumerable<U> Convert<[System.Runtime.CompilerServices.Nullable(0)] T, [System.Runtime.CompilerServices.Nullable(2)] U>(this IEnumerable<T> source) where T : JToken
{
<Convert>d__14<T, U> <Convert>d__ = new <Convert>d__14<T, U>(-2);
<Convert>d__.<>3__source = source;
return <Convert>d__;
}
[System.Runtime.CompilerServices.NullableContext(2)]
internal static U Convert<[System.Runtime.CompilerServices.Nullable(0)] T, U>([System.Runtime.CompilerServices.Nullable(1)] this T token) where T : JToken
{
if (token == null)
return default(U);
if (token is U) {
U result = token as U;
if ((object)typeof(U) != typeof(IComparable) && (object)typeof(U) != typeof(IFormattable))
return result;
}
JValue jValue = token as JValue;
if (jValue == null)
throw new InvalidCastException("Cannot cast {0} to {1}.".FormatWith(CultureInfo.InvariantCulture, token.GetType(), typeof(T)));
object value = jValue.Value;
if (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<[System.Runtime.CompilerServices.Nullable(0)] T>(this IEnumerable<T> source) where T : JToken
{
if (source == null)
return null;
IJEnumerable<T> iJEnumerable = source as IJEnumerable<T>;
if (iJEnumerable != null)
return iJEnumerable;
return new JEnumerable<T>(source);
}
}
}