using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using DPumpHydr.WinFrmUI.WenSkin.Json.Utilities;
namespace DPumpHydr.WinFrmUI.WenSkin.Json.Linq
{
///
/// Contains the LINQ to JSON extension methods.
///
public static class Extensions
{
///
/// Returns a collection of tokens that contains the ancestors of every token in the source collection.
///
/// The type of the objects in source, constrained to .
/// An of that contains the source collection.
/// An of that contains the ancestors of every token in the source collection.
public static IJEnumerable Ancestors(this IEnumerable source) where T : JToken
{
ValidationUtils.ArgumentNotNull(source, "source");
return source.SelectMany((T j) => j.Ancestors()).AsJEnumerable();
}
///
/// Returns a collection of tokens that contains every token in the source collection, and the ancestors of every token in the source collection.
///
/// The type of the objects in source, constrained to .
/// An of that contains the source collection.
/// An of that contains every token in the source collection, the ancestors of every token in the source collection.
public static IJEnumerable AncestorsAndSelf(this IEnumerable source) where T : JToken
{
ValidationUtils.ArgumentNotNull(source, "source");
return source.SelectMany((T j) => j.AncestorsAndSelf()).AsJEnumerable();
}
///
/// Returns a collection of tokens that contains the descendants of every token in the source collection.
///
/// The type of the objects in source, constrained to .
/// An of that contains the source collection.
/// An of that contains the descendants of every token in the source collection.
public static IJEnumerable Descendants(this IEnumerable source) where T : JContainer
{
ValidationUtils.ArgumentNotNull(source, "source");
return source.SelectMany((T j) => j.Descendants()).AsJEnumerable();
}
///
/// Returns a collection of tokens that contains every token in the source collection, and the descendants of every token in the source collection.
///
/// The type of the objects in source, constrained to .
/// An of that contains the source collection.
/// An of that contains every token in the source collection, and the descendants of every token in the source collection.
public static IJEnumerable DescendantsAndSelf(this IEnumerable source) where T : JContainer
{
ValidationUtils.ArgumentNotNull(source, "source");
return source.SelectMany((T j) => j.DescendantsAndSelf()).AsJEnumerable();
}
///
/// Returns a collection of child properties of every object in the source collection.
///
/// An of that contains the source collection.
/// An of that contains the properties of every object in the source collection.
public static IJEnumerable Properties(this IEnumerable source)
{
ValidationUtils.ArgumentNotNull(source, "source");
return source.SelectMany((JObject d) => d.Properties()).AsJEnumerable();
}
///
/// Returns a collection of child values of every object in the source collection with the given key.
///
/// An of that contains the source collection.
/// The token key.
/// An of that contains the values of every token in the source collection with the given key.
public static IJEnumerable Values(this IEnumerable source, object key)
{
return source.Values(key).AsJEnumerable();
}
///
/// Returns a collection of child values of every object in the source collection.
///
/// An of that contains the source collection.
/// An of that contains the values of every token in the source collection.
public static IJEnumerable Values(this IEnumerable source)
{
return source.Values(null);
}
///
/// Returns a collection of converted child values of every object in the source collection with the given key.
///
/// The type to convert the values to.
/// An of that contains the source collection.
/// The token key.
/// An that contains the converted values of every token in the source collection with the given key.
public static IEnumerable Values(this IEnumerable source, object key)
{
return source.Values(key);
}
///
/// Returns a collection of converted child values of every object in the source collection.
///
/// The type to convert the values to.
/// An of that contains the source collection.
/// An that contains the converted values of every token in the source collection.
public static IEnumerable Values(this IEnumerable source)
{
return source.Values(null);
}
///
/// Converts the value.
///
/// The type to convert the value to.
/// A cast as a of .
/// A converted value.
public static U Value(this IEnumerable value)
{
return value.Value();
}
///
/// Converts the value.
///
/// The source collection type.
/// The type to convert the value to.
/// A cast as a of .
/// A converted value.
public static U Value(this IEnumerable value) where T : JToken
{
ValidationUtils.ArgumentNotNull(value, "value");
return ((value as JToken) ?? throw new ArgumentException("Source value must be a JToken.")).Convert();
}
internal static IEnumerable Values(this IEnumerable source, object key) where T : JToken
{
ValidationUtils.ArgumentNotNull(source, "source");
foreach (T token in source)
{
if (key == null)
{
if (token is JValue)
{
yield return ((JValue)(object)token).Convert();
continue;
}
foreach (JToken item in token.Children())
{
yield return item.Convert();
}
}
else
{
JToken jToken = token[key];
if (jToken != null)
{
yield return jToken.Convert();
}
}
}
}
///
/// Returns a collection of child tokens of every array in the source collection.
///
/// The source collection type.
/// An of that contains the source collection.
/// An of that contains the values of every token in the source collection.
public static IJEnumerable Children(this IEnumerable source) where T : JToken
{
return source.Children().AsJEnumerable();
}
///
/// Returns a collection of converted child tokens of every array in the source collection.
///
/// An of that contains the source collection.
/// The type to convert the values to.
/// The source collection type.
/// An that contains the converted values of every token in the source collection.
public static IEnumerable Children(this IEnumerable source) where T : JToken
{
ValidationUtils.ArgumentNotNull(source, "source");
return source.SelectMany((T c) => c.Children()).Convert();
}
internal static IEnumerable Convert(this IEnumerable source) where T : JToken
{
ValidationUtils.ArgumentNotNull(source, "source");
foreach (T item in source)
{
yield return item.Convert();
}
}
internal static U Convert(this T token) where T : JToken
{
if (token == null)
{
return default(U);
}
if (token is U && typeof(U) != typeof(IComparable) && typeof(U) != typeof(IFormattable))
{
return (U)(object)token;
}
JValue jValue = token as JValue;
if (jValue == null)
{
throw new InvalidCastException("Cannot cast {0} to {1}.".FormatWith(CultureInfo.InvariantCulture, token.GetType(), typeof(T)));
}
if (jValue.Value is U)
{
return (U)jValue.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);
}
///
/// Returns the input typed as .
///
/// An of that contains the source collection.
/// The input typed as .
public static IJEnumerable AsJEnumerable(this IEnumerable source)
{
return source.AsJEnumerable();
}
///
/// Returns the input typed as .
///
/// The source collection type.
/// An of that contains the source collection.
/// The input typed as .
public static IJEnumerable AsJEnumerable(this IEnumerable source) where T : JToken
{
if (source == null)
{
return null;
}
if (source is IJEnumerable)
{
return (IJEnumerable)source;
}
return new JEnumerable(source);
}
}
}