mirror of
https://gitlab.com/Syping/luaengineapp.git
synced 2024-12-22 11:55:29 +01:00
add JSON operations
This commit is contained in:
parent
978ea0c075
commit
b019e4aea2
2 changed files with 35 additions and 0 deletions
|
@ -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;
|
||||
|
|
|
@ -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;
|
||||
|
|
Loading…
Reference in a new issue