
// sudo yum -y install json-devel
// g++ test-json.cpp -o test-json -std=gnu++11

#include <iomanip>
#include <iostream>
#include <fstream>
#include <nlohmann/json.hpp>

using json = nlohmann::json;

int main() {
    
    // Read JSON configuration file
    std::ifstream input("config.json");
    json config;
    input >> config;

    // Print original values from the configuration
    std::cout << "Encoder position: " << config["CAL_FILTER_DEP"]["enc"] << std::endl;
    std::cout << "Named position: " << config["CAL_FILTER_DEP"]["named"] << std::endl;

    // Modify some values in the configuration
    config["CAL_FILTER_DEP"]["enc"] = 0;
    config["CAL_FILTER_DEP"]["named"] = "IN";

    // Print edited values from the configuration
    std::cout << "Encoder position: " << config["CAL_FILTER_DEP"]["enc"] << std::endl;
    std::cout << "Named position: " << config["CAL_FILTER_DEP"]["named"] << std::endl;

    // Modify AGAIN some values in the configuration
    config["CAL_FILTER_DEP"]["enc"] = 12345;
    config["CAL_FILTER_DEP"]["named"] = "OUT";

    // Print AGAIN edited values from the configuration
    std::cout << "Encoder position: " << config["CAL_FILTER_DEP"]["enc"] << std::endl;
    std::cout << "Named position: " << config["CAL_FILTER_DEP"]["named"] << std::endl;

    // Write updated configuration back to file
    std::ofstream output("config.json");
    output << std::setw(4) << config << std::endl;
    
    return 0;
    
}
