add JSON operations

This commit is contained in:
Syping 2019-11-03 17:21:22 +01:00
parent 978ea0c075
commit b019e4aea2
2 changed files with 35 additions and 0 deletions

View file

@ -16,8 +16,11 @@
*****************************************************************************/
#include "LuaEngineIO.h"
#include <QJsonDocument>
#include <QDirIterator>
#include <QJsonObject>
#include <QTextStream>
#include <QJsonArray>
#include <QFileInfo>
#include <QString>
#include <QObject>
@ -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;

View file

@ -20,6 +20,8 @@
#include "LuaEngineIO_global.h"
#include "LuaEngine.h"
#include <QJsonValueRef>
#include <QJsonValue>
#include <QString>
#include <QObject>
@ -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;