-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDirectoryTreeStructure.linq
66 lines (58 loc) · 2.25 KB
/
DirectoryTreeStructure.linq
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
<Query Kind="Program">
<NuGetReference>Newtonsoft.Json</NuGetReference>
<Namespace>Newtonsoft.Json</Namespace>
<Namespace>Newtonsoft.Json.Linq</Namespace>
</Query>
private static string _existingData = "[{ \"Path\": \"/\", \"Data\": null, \"Nodes\": [{ \"Path\": \"rock\", \"Data\": null, \"Nodes\": [{ \"Path\": \"you\", \"Data\": null, \"Nodes\": [{ \"Path\": \"like\", \"Data\": null, \"Nodes\": [{ \"Path\": \"hurricane\", \"Data\": { \"hello\": \"world\", \"first\": \"last\", \"myData\": \"123456\" }, \"Nodes\": [] } ] } ] } ] } ] }]";
private List<DirectoryTree> _treeData;
void Main()
{
_treeData = JsonConvert.DeserializeObject<List<DirectoryTree>>(_existingData);
_treeData.Dump();
var data = "{ \"hello\":\"world\", \"first\":\"tony\", \"last\":\"hernandez\"}";
var routeValues = "/rock/you/more/like/hurricane";
var listData = BuildList(routeValues, data);
var stringData = JsonConvert.SerializeObject(listData);
var jTokenData = JsonConvert.DeserializeObject<JToken>(stringData);
jTokenData.Dump();
}
private DirectoryTree BuildList(string routeValues, JToken data)
{
var myTree = new DirectoryTree() { Path = "/", Nodes = new List<DirectoryTree>() };
var parts = routeValues.Split('/');
EnsureExists(myTree, parts, data);
return myTree;
}
private void EnsureExists(DirectoryTree myTree, IEnumerable<string> routeValues, JToken data)
{
if (routeValues.Any())
{
var path = routeValues.First();
var node = myTree.Nodes.SingleOrDefault(x => x.Path == path);
if (node == null)
{
node = new DirectoryTree() { Path = path, Nodes = new List<DirectoryTree>() };
myTree.Nodes.Add(node);
}
EnsureExists(node, routeValues.Skip(1), data);
}
else
{
AddDataToLastNode(myTree, data);
}
}
private void AddDataToLastNode(DirectoryTree myTree, JToken data)
{
myTree.Data = data;
}
public class DirectoryTree
{
public string Path { get; set; }
public object Data { get; set; }
public List<DirectoryTree> Nodes { get; set; }
public override string ToString()
{
var fullPath = Path == "/" ? string.Empty : $"{Path}";
return Nodes.Aggregate(fullPath, (current, node) => current + $"/{node.Path}");
}
}