#region License
// Copyright (c) 2007 James Newton-King
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
#endregion
using System;
using System.Collections.Generic;
using LC.Newtonsoft.Json.Utilities;
using System.Globalization;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
#if !HAVE_LINQ
using LC.Newtonsoft.Json.Utilities.LinqBridge;
#else
using System.Linq;
#endif
namespace LC.Newtonsoft.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, nameof(source));
return source.SelectMany(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, nameof(source));
return source.SelectMany(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, nameof(source));
return source.SelectMany(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, nameof(source));
return source.SelectMany(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, nameof(source));
return source.SelectMany(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 Values(source, 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 Values(source, 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 Values(source, 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, nameof(value));
if (!(value is JToken token))
{
throw new ArgumentException("Source value must be a JToken.");
}
return token.Convert();
}
internal static IEnumerable Values(this IEnumerable source, object? key) where T : JToken
{
ValidationUtils.ArgumentNotNull(source, nameof(source));
if (key == null)
{
foreach (T token in source)
{
if (token is JValue value)
{
yield return Convert(value);
}
else
{
foreach (JToken t in token.Children())
{
yield return t.Convert();
}
}
}
}
else
{
foreach (T token in source)
{
JToken? value = token[key];
if (value != null)
{
yield return value.Convert();
}
}
}
}
//TODO
//public static IEnumerable InDocumentOrder(this IEnumerable source) where T : JObject;
///
/// 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 Children(source).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, nameof(source));
return source.SelectMany(c => c.Children()).Convert();
}
internal static IEnumerable Convert(this IEnumerable source) where T : JToken
{
ValidationUtils.ArgumentNotNull(source, nameof(source));
foreach (T token in source)
{
yield return Convert(token);
}
}
[return: MaybeNull]
internal static U Convert(this T token) where T : JToken?
{
if (token == null)
{
#pragma warning disable CS8653 // A default expression introduces a null value for a type parameter.
return default;
#pragma warning restore CS8653 // A default expression introduces a null value for a type parameter.
}
if (token is U castValue
// don't want to cast JValue to its interfaces, want to get the internal value
&& typeof(U) != typeof(IComparable) && typeof(U) != typeof(IFormattable))
{
return castValue;
}
else
{
if (!(token is JValue value))
{
throw new InvalidCastException("Cannot cast {0} to {1}.".FormatWith(CultureInfo.InvariantCulture, token.GetType(), typeof(T)));
}
if (value.Value is U u)
{
return u;
}
Type targetType = typeof(U);
if (ReflectionUtils.IsNullableType(targetType))
{
if (value.Value == null)
{
#pragma warning disable CS8653 // A default expression introduces a null value for a type parameter.
return default;
#pragma warning restore CS8653 // A default expression introduces a null value for a type parameter.
}
targetType = Nullable.GetUnderlyingType(targetType);
}
return (U)System.Convert.ChangeType(value.Value, targetType, CultureInfo.InvariantCulture);
}
}
//TODO
//public static void Remove(this IEnumerable source) where T : JContainer;
///
/// 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!;
}
else if (source is IJEnumerable customEnumerable)
{
return customEnumerable;
}
else
{
return new JEnumerable(source);
}
}
}
}