Reputation: 1800
I have a sample output of the file paths, this is just an example the for the question
New Text Document.txt
New folder/
New folder/README.txt
which I would like to convert to the following JSON
{
"Data":"/",
"Nodes":[
{
"Data":"New Folder",
"Nodes":[
{
"Data":"New Text Document.txt"
}
]
},
{
"Data":"New Text Document.txt",
"Nodes":[
""
]
}
]
}
My Node Class is the following
public class Node
{
public Node(string fileName)
{
Nodes = new List<Node>();
Data = fileName;
}
public List<Node> Nodes { get; set; }
public string Data { get; set; }
}
I'm trying to figure out the algorithm, how to represent file paths in the as a Node class which I will serialize later to get the JSON. If there is any other way to represent file paths as Directory Tree structured JSON please suggest
Upvotes: 3
Views: 4215
Reputation: 2982
I finally got around to making a sample for you. This should be a nicely scalable recursive solution. :)
static void Main(string[] args)
{
Node root = new Node("/");
AddNode("New Text Document.txt", root);
AddNode("New folder/", root);
AddNode("New folder/README.txt", root);
Console.ReadKey();
}
public class Node
{
public Node() { Nodes = new List<Node>(); }
public Node(string fileName)
{
Nodes = new List<Node>();
Data = fileName;
}
public Node FindNode(string data)
{
if (this.Nodes == null || !this.Nodes.Any()) { return null; }
// check Node list to see if there are any that already exist
return this.Nodes
.FirstOrDefault(n => String.Equals(n.Data, data, StringComparison.CurrentCultureIgnoreCase));
}
public string Data { get; set; }
public List<Node> Nodes { get; set; }
}
public static Node AddNode(string filePath, Node rootNode)
{
// convenience method. this creates the queue that we need for recursion from the filepath for you
var tokens = filePath.Split('/').ToList();
// if you split a folder ending with / it leaves an empty string at the end and we want to remove that
if (String.IsNullOrWhiteSpace(tokens.Last())) { tokens.Remove(""); }
return AddNode(new Queue<string>(tokens), rootNode);
}
private static Node AddNode(Queue<string> tokens, Node rootNode)
{
// base case -> node wasnt found and tokens are gone :(
if (tokens == null || !tokens.Any())
{
return null;
}
// get current token, leaving only unsearched ones in the tokens object
string current = tokens.Dequeue();
// create node if not already exists
Node foundNode = rootNode.FindNode(current);
if (foundNode != null)
{
// node exists! recurse
return AddNode(tokens, foundNode);
}
else
{
// node doesnt exist! add it manually and recurse
Node newNode = new Node() { Data = current };
rootNode.Nodes.Add(newNode);
return AddNode(tokens, newNode);
}
}
Upvotes: 5
Reputation: 1346
using System.Web.Script.Serialization; ///name space to use
JavaScriptSerializer js = new JavaScriptSerializer();
string json = js.Serialize(pass node class object here);
Below code should give the desired directory structure.
static void Main(string[] args)
{
string path = @"path where to check";
Node n = new Node();
n.Nodes = new List<Node>();
GetNodes(path, n);
JavaScriptSerializer js = new JavaScriptSerializer();
string json = js.Serialize(n);
}
public static void GetNodes(string path, Node node)
{
if (File.Exists(path))
{
node = new Node(path);
}
else if (Directory.Exists(path))
{
node.Data = "\\";
GetFiles(path, node);
foreach ( string item in Directory.GetDirectories(path))
{
Node n = new Node();
n.Nodes = new List<Node>();
n.Data = item;
GetFiles(path, n);
node.Nodes.Add(n);
}
}
}
public static void GetFiles(string path, Node node)
{
foreach (string item in Directory.GetFiles(path))
{
node.Nodes.Add(new Node(item));
}
}
public class Node
{
public Node()
{ }
public Node(string fileName)
{
Nodes = new List<Node>();
Data = fileName;
}
public List<Node> Nodes { get; set; }
public string Data { get; set; }
}
Upvotes: 1