forked from qt-creator/qt-creator
Lua: Add to/from json convenience functions
Change-Id: Ibf69c8021d676bd6efbbdb5331f1925808fcfe38 Reviewed-by: Christian Stenger <christian.stenger@qt.io>
This commit is contained in:
@@ -288,14 +288,8 @@ void addFetchModule()
|
||||
callback(error.errorString());
|
||||
return;
|
||||
}
|
||||
if (doc.isObject()) {
|
||||
callback(LuaEngine::toTable(thisState, doc.object()));
|
||||
} else if (doc.isArray()) {
|
||||
callback(LuaEngine::toTable(thisState, doc.array()));
|
||||
} else {
|
||||
sol::state_view lua(thisState);
|
||||
callback(lua.create_table());
|
||||
}
|
||||
|
||||
callback(LuaEngine::toTable(thisState, doc));
|
||||
});
|
||||
|
||||
} else {
|
||||
|
@@ -11,18 +11,7 @@ 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["encode"] = &LuaEngine::toJsonString;
|
||||
|
||||
json["decode"] = [](sol::this_state l, const QString &str) -> sol::table {
|
||||
QJsonParseError error;
|
||||
@@ -30,12 +19,7 @@ void addJsonModule()
|
||||
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 LuaEngine::toTable(l.lua_state(), doc);
|
||||
};
|
||||
|
||||
return json;
|
||||
|
@@ -15,6 +15,7 @@
|
||||
#include <utils/theme/theme.h>
|
||||
|
||||
#include <QJsonArray>
|
||||
#include <QJsonDocument>
|
||||
#include <QJsonObject>
|
||||
#include <QStandardPaths>
|
||||
|
||||
@@ -348,6 +349,15 @@ sol::table LuaEngine::toTable(const sol::state_view &lua, const QJsonValue &v)
|
||||
return table;
|
||||
}
|
||||
|
||||
sol::table LuaEngine::toTable(const sol::state_view &lua, const QJsonDocument &doc)
|
||||
{
|
||||
if (doc.isArray())
|
||||
return toTable(lua, doc.array());
|
||||
else if (doc.isObject())
|
||||
return toTable(lua, doc.object());
|
||||
return sol::table();
|
||||
}
|
||||
|
||||
QJsonValue toJsonValue(const sol::object &object);
|
||||
|
||||
QJsonValue toJsonValue(const sol::table &table)
|
||||
@@ -397,6 +407,16 @@ QJsonValue LuaEngine::toJson(const sol::table &table)
|
||||
return toJsonValue(table);
|
||||
}
|
||||
|
||||
QString LuaEngine::toJsonString(const sol::table &t)
|
||||
{
|
||||
QJsonValue v = toJson(t);
|
||||
if (v.isArray())
|
||||
return QString::fromUtf8(QJsonDocument(v.toArray()).toJson(QJsonDocument::Compact));
|
||||
else if (v.isObject())
|
||||
return QString::fromUtf8(QJsonDocument(v.toObject()).toJson(QJsonDocument::Compact));
|
||||
return {};
|
||||
}
|
||||
|
||||
QStringList LuaEngine::variadicToStringList(const sol::variadic_args &vargs)
|
||||
{
|
||||
QStringList strings;
|
||||
|
@@ -66,7 +66,10 @@ public:
|
||||
static bool isCoroutine(lua_State *state);
|
||||
|
||||
static sol::table toTable(const sol::state_view &lua, const QJsonValue &v);
|
||||
static sol::table toTable(const sol::state_view &lua, const QJsonDocument &doc);
|
||||
|
||||
static QJsonValue toJson(const sol::table &t);
|
||||
static QString toJsonString(const sol::table &t);
|
||||
|
||||
template<class T>
|
||||
static void checkKey(const sol::table &table, const QString &key)
|
||||
|
Reference in New Issue
Block a user