ListTreeNodeJsonPointerExtensions.cs
4.44 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
using System;
using System.Collections.Generic;
using System.Linq;
namespace UniJSON
{
public static class ListTreeNodeJsonPointerExtensions
{
public static void SetValue<T>(this ListTreeNode<T> self,
Utf8String jsonPointer, ArraySegment<Byte> bytes)
where T: IListTreeItem, IValue<T>
{
foreach (var node in self.GetNodes(jsonPointer))
{
node.SetValue(default(T).New(
bytes,
ValueNodeType.Boolean,
node.Value.ParentIndex));
}
}
public static void RemoveValue<T>(this ListTreeNode<T> self, Utf8String jsonPointer)
where T : IListTreeItem, IValue<T>
{
foreach (var node in self.GetNodes(new JsonPointer(jsonPointer)))
{
if (node.Parent.IsMap())
{
node.Prev.SetValue(default(T)); // remove key
}
node.SetValue(default(T)); // remove
}
}
public static JsonPointer Pointer<T>(this ListTreeNode<T> self)
where T: IListTreeItem, IValue<T>
{
return JsonPointer.Create(self);
}
public static IEnumerable<ListTreeNode<T>> Path<T>(this ListTreeNode<T> self)
where T : IListTreeItem, IValue<T>
{
if (self.HasParent)
{
foreach (var x in self.Parent.Path())
{
yield return x;
}
}
yield return self;
}
public static IEnumerable<ListTreeNode<T>> GetNodes<T>(this ListTreeNode<T> self,
JsonPointer jsonPointer)
where T : IListTreeItem, IValue<T>
{
if (jsonPointer.Path.Count == 0)
{
yield return self;
yield break;
}
if (self.IsArray())
{
// array
if (jsonPointer[0][0] == '*')
{
// wildcard
foreach (var child in self.ArrayItems())
{
foreach (var childChild in child.GetNodes(jsonPointer.Unshift()))
{
yield return childChild;
}
}
}
else
{
int index = jsonPointer[0].ToInt32();
var child = self.ArrayItems().Skip(index).First();
foreach (var childChild in child.GetNodes(jsonPointer.Unshift()))
{
yield return childChild;
}
}
}
else if (self.IsMap())
{
// object
if (jsonPointer[0][0] == '*')
{
// wildcard
foreach (var kv in self.ObjectItems())
{
foreach (var childChild in kv.Value.GetNodes(jsonPointer.Unshift()))
{
yield return childChild;
}
}
}
else
{
ListTreeNode<T> child;
try
{
child = self.ObjectItems().First(x => x.Key.GetUtf8String() == jsonPointer[0]).Value;
}
catch (Exception)
{
// key
self.AddKey(jsonPointer[0]);
// value
self.AddValue(default(ArraySegment<byte>), ValueNodeType.Object);
child = self.ObjectItems().First(x => x.Key.GetUtf8String() == jsonPointer[0]).Value;
}
foreach (var childChild in child.GetNodes(jsonPointer.Unshift()))
{
yield return childChild;
}
}
}
else
{
throw new NotImplementedException();
}
}
public static IEnumerable<ListTreeNode<T>> GetNodes<T>(this ListTreeNode<T> self,
Utf8String jsonPointer)
where T : IListTreeItem, IValue<T>
{
return self.GetNodes(new JsonPointer(jsonPointer));
}
}
}