Add support for arrays in luascript topic (#913)

This commit is contained in:
Emil Axelsson
2019-07-05 16:38:43 +02:00
committed by GitHub
parent e9fdd31c56
commit 0293eda1e8

View File

@@ -69,7 +69,7 @@ namespace {
return "[" + formatLuaString(it.key()) + "] = " + formatLua(it);
}
std::string formatLuaTable(const nlohmann::json& json) {
std::string formatObjectAsLuaTable(const nlohmann::json& json) {
std::string output = "{";
auto it = json.begin();
for (size_t i = 0; i < json.size(); ++i, ++it) {
@@ -81,9 +81,24 @@ namespace {
return output + "}";
}
std::string formatArrayAsLuaTable(const nlohmann::json& json) {
std::string output = "{";
auto it = json.begin();
for (size_t i = 0; i < json.size(); ++i, ++it) {
output += formatLua(it);
if (i < json.size() - 1) {
output += ",";
}
}
return output + "}";
}
std::string formatLua(const nlohmann::json::const_iterator& it) {
if (it->is_object()) {
return formatLuaTable(it->get<nlohmann::json>());
return formatObjectAsLuaTable(it->get<nlohmann::json>());
}
if (it->is_array()) {
return formatArrayAsLuaTable(it->get<nlohmann::json>());
}
if (it->is_number()) {
return fmt::format("{:E}", it->get<double>());