ListTreeNodeObjectExtensions.cs
2.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
using System;
using System.Collections.Generic;
using System.Linq;
namespace UniJSON
{
public static class IValueNodeObjectExtensions
{
public static IEnumerable<KeyValuePair<ListTreeNode<T>, ListTreeNode<T>>> ObjectItems<T>(this ListTreeNode<T> self)
where T : IListTreeItem, IValue<T>
{
if (!self.IsMap()) throw new DeserializationException("is not object");
var it = self.Children.GetEnumerator();
while (it.MoveNext())
{
var key = it.Current;
it.MoveNext();
yield return new KeyValuePair<ListTreeNode<T>, ListTreeNode<T>>(key, it.Current);
}
}
public static int GetObjectCount<T>(this ListTreeNode<T> self)
where T : IListTreeItem, IValue<T>
{
if (!self.IsMap()) throw new DeserializationException("is not object");
return self.Children.Count() / 2;
}
public static ListTreeNode<T> GetObjectItem<T>(this ListTreeNode<T> self, String key)
where T : IListTreeItem, IValue<T>
{
return self.GetObjectItem(Utf8String.From(key));
}
public static ListTreeNode<T> GetObjectItem<T>(this ListTreeNode<T> self, Utf8String key)
where T : IListTreeItem, IValue<T>
{
foreach (var kv in self.ObjectItems())
{
if (kv.Key.GetUtf8String() == key)
{
return kv.Value;
}
}
throw new KeyNotFoundException();
}
public static bool ContainsKey<T>(this ListTreeNode<T> self, Utf8String key)
where T : IListTreeItem, IValue<T>
{
return self.ObjectItems().Any(x => x.Key.GetUtf8String() == key);
}
public static bool ContainsKey<T>(this ListTreeNode<T> self, String key)
where T : IListTreeItem, IValue<T>
{
var ukey = Utf8String.From(key);
return self.ContainsKey(ukey);
}
public static Utf8String KeyOf<T>(this ListTreeNode<T> self, ListTreeNode<T> node)
where T : IListTreeItem, IValue<T>
{
foreach (var kv in self.ObjectItems())
{
if (node.ValueIndex == kv.Value.ValueIndex)
{
return kv.Key.GetUtf8String();
}
}
throw new KeyNotFoundException();
}
}
}