Search code examples
c++nlohmann-json

How to create file hierarchy representation in JSON


I am working with https://github.com/nlohmann/json first time and i have to create file hierarchy like this:

{
"Files": [
  {
    "Name": "Test.txt",
    "Size": "27 B",
    "Path": "D:\\Projects\\Test.txt"
  },
  ...
],
"Children": [
               {
        "Name": "SubProjects",
        "Files": [
            {
              "Name": "SubTest.txt",
              "Size": "2 B",
              "Path": "D:\\Projects\\SubProjects\\SubTest.txt"
            },
            ...
        ],
        "Children": [ 
                          ....
                      ]
    },
    {
        "Name": "SubProjects3",
        "Files": [],
        "Children": []
    },
    ...
]}

Now i have a problem with adding information in nested nodes. I tried to solve this problem by searching by new key "level" and tried to find where this key equals to level which i need but it still doesn't work. My code:

#include <iostream>
#include  <conio.h>
#include <nlohmann/json.hpp>
#include <fstream>
#include <iomanip>
#include <string>  
#include <sstream>  
#include <experimental/filesystem>  
using namespace std;
using json = nlohmann::json;
namespace fs = std::experimental::filesystem;

void DisplayFileInfo(const fs::v1::directory_entry& entry, fs::v1::path& filename , json &j_main,int level)
{
    json j_file;
    j_file["Name"] = filename.string();
    j_file["Size"] = fs::file_size(entry);
    j_file["Path"] = fs::absolute(filename).string();
    for ( auto& obj : j_main) {
        if (obj["Level"] == level) {
            obj["Files"].push_back(j_file);
        }
    }
}
void  DisplayFolderInfo(const fs::v1::directory_entry& entry,  fs::v1::path& filename, json &j_main, int level)
{
    json j_folder;
    j_folder["Level"] = level+1;
    j_folder["Name"] = filename.string();
    j_folder["Files"] = json::array({});
    j_folder["Children"] = json::array({});
    for (auto& obj : j_main)
    {
        if (obj["Level"] == level) {
        obj["Children"].push_back(j_folder);
        }
    }
void DisplayDirectoryTree(const fs::path& pathToShow, int level, json &j_main)
{

  if (fs::exists(pathToShow) && fs::is_directory(pathToShow))
  {
      for (const auto& entry : fs::directory_iterator(pathToShow))
      {
        auto filename = entry.path().filename();
        if (fs::is_directory(entry.status()))
        {
            DisplayFolderInfo(entry,  filename,j_main,level);
            level++;
            DisplayDirectoryTree(entry,level,j_main);
        }
        else if (fs::is_regular_file(entry.status()))
            DisplayFileInfo(entry, filename,j_main,level);

      }
  }

}

int main()
{
  char folder_path[255];
  cout << "Please input name of folder with full path: " << endl;
  cin >> folder_path;
  json j_main;
  j_main["Level"] = 0;
  j_main["Children"] = json::array({});
  j_main["Files"] = json::array({});
  const fs::path pathToShow=folder_path;
  DisplayDirectoryTree(pathToShow, 0,j_main);
  ofstream o("file.json");
  o << setw(4) << j_main << endl;
  _getch();
  return 0;
}

Solution

  • You don't need to search for anything, you can simply pass the subtree instead of j_main.

    Or not pass anything, the logic is fairly simple to fit in a single function:

    void DisplayDirectoryTree(const fs::path & pathToShow, json& root, int level = 0)
    {
        std::vector<json> files, children;
        root["Level"] = level;
        for (const auto& entry : fs::directory_iterator(pathToShow))
        {
            json j_entry;
            j_entry["Name"] = entry.path().filename().u8string();
            if (fs::is_directory(entry.status()))
            {
                DisplayDirectoryTree(entry.path(), j_entry, level + 1);
                children.emplace_back(std::move(j_entry));
            }
            else
            {
                j_entry["Size"] = fs::file_size(entry);
                j_entry["Path"] = fs::absolute(entry.path()).u8string();
                files.emplace_back(std::move(j_entry));
            }
        }
        root["Files"] = files;
        root["Children"] = children;
    }
    
    int main()
    {
        std::string folder_path;
        cout << "Please input name of folder with full path: " << endl;
        cin >> folder_path;
        json j_main;
        DisplayDirectoryTree(fs::path(folder_path), j_main);
        ofstream o("file.json");
        o << j_main.dump(4) << endl;
        o.close();
        return 0;
    }
    

    Also note that your level++ logic is wrong - you need not increment it in a loop, but pass level + 1 into the recursive call.