Skip to content

Instantly share code, notes, and snippets.

@pboettch
Last active January 8, 2019 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pboettch/67e6006e96584655bc3018c1612224ce to your computer and use it in GitHub Desktop.
Save pboettch/67e6006e96584655bc3018c1612224ce to your computer and use it in GitHub Desktop.
Convert Settings from Libconfig++ to NLohmann's JSON
#include <config/config.h>
#include <nlohmann/json.hpp>
namespace libconfig
{
void to_json(nlohmann::json &j, const Setting &setting)
{
json *data;
try {
auto n = setting.getName();
data = &j[n];
} catch (const std::exception &) {
data = &j;
}
switch (setting.getType()) {
case Setting::TypeInt:
*data = (int) setting;
break;
case Setting::TypeInt64:
*data = (long long) setting;
break;
case Setting::TypeFloat:
*data = (double) setting;
break;
case Setting::TypeString:
*data = (const char *) setting;
break;
case Setting::TypeBoolean:
*data = (bool) setting;
break;
case Setting::TypeGroup:
*data = json::object();
for (auto &sub : setting)
data->update(sub);
break;
case Setting::TypeArray:
case Setting::TypeList:
*data = json::array();
for (auto &sub : setting)
data->push_back(sub);
break;
case Setting::TypeNone:
default:
throw std::invalid_argument("unknown setting type");
break;
}
}
} // namespace libconfig
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment