forked from qt-creator/qt-creator
Lua: Add Json module
Change-Id: I533dc63b5147f00ac377e80a338cc4e2e3d0e8d9 Reviewed-by: hjk <hjk@qt.io>
This commit is contained in:
@@ -14,6 +14,7 @@ add_qtc_plugin(Lua
|
||||
bindings/hook.cpp
|
||||
bindings/inheritance.h
|
||||
bindings/install.cpp
|
||||
bindings/json.cpp
|
||||
bindings/messagemanager.cpp
|
||||
bindings/qtcprocess.cpp
|
||||
bindings/settings.cpp
|
||||
@@ -32,6 +33,7 @@ add_qtc_plugin(Lua
|
||||
meta/documents.lua
|
||||
meta/fetch.lua
|
||||
meta/gui.lua
|
||||
meta/json.lua
|
||||
meta/install.lua
|
||||
meta/lsp.lua
|
||||
meta/messagemanager.lua
|
||||
@@ -73,6 +75,8 @@ set(META_FILES
|
||||
meta/documents.lua
|
||||
meta/fetch.lua
|
||||
meta/gui.lua
|
||||
meta/json.lua
|
||||
meta/localsocket.lua
|
||||
meta/lsp.lua
|
||||
meta/messagemanager.lua
|
||||
meta/process.lua
|
||||
|
45
src/plugins/lua/bindings/json.cpp
Normal file
45
src/plugins/lua/bindings/json.cpp
Normal file
@@ -0,0 +1,45 @@
|
||||
// Copyright (C) 2024 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
|
||||
#include "../luaengine.h"
|
||||
|
||||
#include <QJsonArray>
|
||||
#include <QJsonDocument>
|
||||
|
||||
namespace Lua::Internal {
|
||||
|
||||
void addJsonModule()
|
||||
{
|
||||
LuaEngine::registerProvider("Json", [](sol::state_view lua) -> sol::object {
|
||||
sol::table json = lua.create_table();
|
||||
json["encode"] = [](const sol::table &tbl) -> QString {
|
||||
QJsonValue value = LuaEngine::toJson(tbl);
|
||||
QJsonDocument doc;
|
||||
if (value.isObject())
|
||||
doc.setObject(value.toObject());
|
||||
else if (value.isArray())
|
||||
doc.setArray(value.toArray());
|
||||
else
|
||||
return QString();
|
||||
|
||||
return QString::fromUtf8(doc.toJson());
|
||||
};
|
||||
|
||||
json["decode"] = [](sol::this_state l, const QString &str) -> sol::table {
|
||||
QJsonParseError error;
|
||||
auto doc = QJsonDocument::fromJson(str.toUtf8(), &error);
|
||||
if (error.error != QJsonParseError::NoError)
|
||||
throw sol::error(error.errorString().toStdString());
|
||||
|
||||
if (doc.isObject())
|
||||
return LuaEngine::toTable(l.lua_state(), doc.object());
|
||||
else if (doc.isArray())
|
||||
return LuaEngine::toTable(l.lua_state(), doc.array());
|
||||
|
||||
return sol::table();
|
||||
};
|
||||
|
||||
return json;
|
||||
});
|
||||
}
|
||||
|
||||
} // namespace Lua::Internal
|
@@ -40,6 +40,7 @@ QtcPlugin {
|
||||
"hook.cpp",
|
||||
"inheritance.h",
|
||||
"install.cpp",
|
||||
"json.cpp"
|
||||
"messagemanager.cpp",
|
||||
"qtcprocess.cpp",
|
||||
"settings.cpp",
|
||||
@@ -69,6 +70,7 @@ QtcPlugin {
|
||||
"fetch.lua",
|
||||
"gui.lua",
|
||||
"install.lua",
|
||||
"json.lua",
|
||||
"lsp.lua",
|
||||
"messagemanager.lua",
|
||||
"process.lua",
|
||||
|
@@ -40,6 +40,7 @@ void addFetchModule();
|
||||
void addGuiModule();
|
||||
void addHookModule();
|
||||
void addInstallModule();
|
||||
void addJsonModule();
|
||||
void addMessageManagerModule();
|
||||
void addProcessModule();
|
||||
void addQtModule();
|
||||
@@ -254,6 +255,7 @@ public:
|
||||
addGuiModule();
|
||||
addHookModule();
|
||||
addInstallModule();
|
||||
addJsonModule();
|
||||
addMessageManagerModule();
|
||||
addProcessModule();
|
||||
addQtModule();
|
||||
|
15
src/plugins/lua/meta/json.lua
Normal file
15
src/plugins/lua/meta/json.lua
Normal file
@@ -0,0 +1,15 @@
|
||||
---@meta Json
|
||||
|
||||
local Json = {}
|
||||
|
||||
---Create a json string representing the table.
|
||||
---@param table table The table to encode.
|
||||
---@return string json The JSON string.
|
||||
function Json.encode(table) end
|
||||
|
||||
---Decode a json string into a table.
|
||||
---@param json string The JSON string.
|
||||
---@return table table The decoded table.
|
||||
function Json.decode(json) end
|
||||
|
||||
return Json
|
Reference in New Issue
Block a user