using System; using System.Collections.Generic; using System.Linq; namespace UniJSON { public static class ListTreeNodeArrayExtensions { public static IEnumerable> ArrayItems(this ListTreeNode self) where T : IListTreeItem, IValue { if (!self.IsArray()) throw new DeserializationException("is not array"); return self.Children; } [Obsolete("Use GetArrayItem(index)")] public static ListTreeNode GetArrrayItem(this ListTreeNode self, int index) where T : IListTreeItem, IValue { return GetArrayItem(self, index); } public static ListTreeNode GetArrayItem(this ListTreeNode self, int index) where T : IListTreeItem, IValue { int i = 0; foreach (var v in self.ArrayItems()) { if (i++ == index) { return v; } } throw new KeyNotFoundException(); } public static int GetArrayCount(this ListTreeNode self) where T : IListTreeItem, IValue { if (!self.IsArray()) throw new DeserializationException("is not array"); return self.Children.Count(); } public static int IndexOf(this ListTreeNode self, ListTreeNode child) where T : IListTreeItem, IValue { int i = 0; foreach (var v in self.ArrayItems()) { if (v.ValueIndex == child.ValueIndex) { return i; } ++i; } throw new KeyNotFoundException(); } } }