#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 System.Linq;
using System.Text;
using Newtonsoft.Json.Utilities;
using System.Collections;
using System.Globalization;
namespace 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 node in the source collection.
public static IJEnumerable Ancestors(this IEnumerable source) where T : JToken
{
ValidationUtils.ArgumentNotNull(source, "source");
return source.SelectMany(j => j.Ancestors()).AsJEnumerable();
}
//TODO
//public static IEnumerable AncestorsAndSelf(this IEnumerable source) where T : JObject
//{
// ValidationUtils.ArgumentNotNull(source, "source");
// return source.SelectMany(j => j.AncestorsAndSelf());
//}
///
/// 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 node in the source collection.
public static IJEnumerable Descendants(this IEnumerable source) where T : JContainer
{
ValidationUtils.ArgumentNotNull(source, "source");
return source.SelectMany(j => j.Descendants()).AsJEnumerable();
}
//TODO
//public static IEnumerable DescendantsAndSelf(this IEnumerable source) where T : JContainer
//{
// ValidationUtils.ArgumentNotNull(source, "source");
// return source.SelectMany(j => j.DescendantsAndSelf());
//}
///
/// 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(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 node 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 node 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 node 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 node 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, "source");
JToken token = value as JToken;
if (token == null)
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, "source");
foreach (JToken token in source)
{
if (key == null)
{
if (token is JValue)
{
yield return Convert((JValue)token);
}
else
{
foreach (JToken t in token.Children())
{
yield return t.Convert(); ;
}
}
}
else
{
JToken value = token[key];
if (value != null)
yield return value.Convert();
}
}
yield break;
}
//TODO
//public static IEnumerable InDocumentOrder(this IEnumerable source) where T : JObject;
//public static IEnumerable Children(this IEnumerable source) where T : JToken
//{
// ValidationUtils.ArgumentNotNull(source, "source");
// return source.SelectMany(c => c.Children());
//}
///
/// 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 node 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 node in the source collection.
public static IEnumerable Children(this IEnumerable source) where T : JToken
{
ValidationUtils.ArgumentNotNull(source, "source");
return source.SelectMany(c => c.Children()).Convert();
}
internal static IEnumerable Convert(this IEnumerable source) where T : JToken
{
ValidationUtils.ArgumentNotNull(source, "source");
bool cast = typeof(JToken).IsAssignableFrom(typeof(U));
foreach (JToken token in source)
{
yield return Convert(token, cast);
}
}
internal static U Convert(this T token) where T : JToken
{
bool cast = typeof(JToken).IsAssignableFrom(typeof(U));
return Convert(token, cast);
}
internal static U Convert(this T token, bool cast) where T : JToken
{
if (cast)
{
// HACK
return (U)(object)token;
}
else
{
if (token == null)
return default(U);
JValue value = token as JValue;
if (value == null)
throw new InvalidCastException("Cannot cast {0} to {1}.".FormatWith(CultureInfo.InvariantCulture, token.GetType(), typeof(T)));
Type targetType = typeof(U);
if (ReflectionUtils.IsNullableType(targetType))
{
if (value.Value == null)
return default(U);
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)
return (IJEnumerable)source;
else
return new JEnumerable(source);
}
}
}