Last active
January 8, 2019 14:03
-
-
Save pboettch/67e6006e96584655bc3018c1612224ce to your computer and use it in GitHub Desktop.
Convert Settings from Libconfig++ to NLohmann's JSON
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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