From 0293eda1e817567e6d225bbb96e13a02e16fda74 Mon Sep 17 00:00:00 2001 From: Emil Axelsson Date: Fri, 5 Jul 2019 16:38:43 +0200 Subject: [PATCH] Add support for arrays in luascript topic (#913) --- modules/server/src/topics/luascripttopic.cpp | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/modules/server/src/topics/luascripttopic.cpp b/modules/server/src/topics/luascripttopic.cpp index f883410764..018159f0ba 100644 --- a/modules/server/src/topics/luascripttopic.cpp +++ b/modules/server/src/topics/luascripttopic.cpp @@ -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()); + return formatObjectAsLuaTable(it->get()); + } + if (it->is_array()) { + return formatArrayAsLuaTable(it->get()); } if (it->is_number()) { return fmt::format("{:E}", it->get());