diff --git a/src/luaengineio/luaengine/LuaEngineIO.cpp b/src/luaengineio/luaengine/LuaEngineIO.cpp index ba378c6..9e9187a 100644 --- a/src/luaengineio/luaengine/LuaEngineIO.cpp +++ b/src/luaengineio/luaengine/LuaEngineIO.cpp @@ -16,8 +16,11 @@ *****************************************************************************/ #include "LuaEngineIO.h" +#include #include +#include #include +#include #include #include #include @@ -48,6 +51,12 @@ void LuaEngineIO::pushClass(lua_State *L_p) pushFunction(L_p, "linkFile", linkFile); pushVariant(L_p, "Symlink", (int)0); pushVariant(L_p, "Hardlink", (int)1); + + // Json + pushFunction(L_p, "jsonToTable", jsonToTable); + pushFunction(L_p, "tableToJson", tableToJson); + pushVariant(L_p, "JsonCompact", (int)QJsonDocument::Compact); + pushVariant(L_p, "JsonIndented", (int)QJsonDocument::Indented); } void LuaEngineIO::pushClass(LuaEngine *luaEngine) @@ -162,6 +171,28 @@ int LuaEngineIO::linkFile(lua_State *L_p) return 1; } +int LuaEngineIO::jsonToTable(lua_State *L_p) +{ + if (getArgumentCount(L_p) >= 1) { + pushVariant(L_p, QJsonDocument::fromJson(getVariant(L_p, 1).toString().toUtf8()).object().toVariantMap()); + return 1; + } + return 0; +} + +int LuaEngineIO::tableToJson(lua_State *L_p) +{ + if (getArgumentCount(L_p) >= 1) { + QJsonDocument::JsonFormat jsonFormat = QJsonDocument::Compact; + if (getArgumentCount(L_p) >= 2) { + jsonFormat = (QJsonDocument::JsonFormat)getVariant(L_p, 2).toInt(); + } + pushVariant(L_p, QString::fromUtf8(QJsonDocument(QJsonObject::fromVariantMap(getVariant(L_p, 1).toMap())).toJson(jsonFormat))); + return 1; + } + return 0; +} + QString LuaEngineIO::nameForPointer(void *pointer) { QString nameStorage; diff --git a/src/luaengineio/luaengine/LuaEngineIO.h b/src/luaengineio/luaengine/LuaEngineIO.h index 7048b21..4e0bf91 100644 --- a/src/luaengineio/luaengine/LuaEngineIO.h +++ b/src/luaengineio/luaengine/LuaEngineIO.h @@ -20,6 +20,8 @@ #include "LuaEngineIO_global.h" #include "LuaEngine.h" +#include +#include #include #include @@ -33,6 +35,8 @@ public: static int getDirectoryContent(lua_State *L_p); static int getDirectoryPath(lua_State *L_p); static int linkFile(lua_State *L_p); + static int jsonToTable(lua_State *L_p); + static int tableToJson(lua_State *L_p); private: lua_State *L;