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());
|
callback(error.errorString());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (doc.isObject()) {
|
|
||||||
callback(LuaEngine::toTable(thisState, doc.object()));
|
callback(LuaEngine::toTable(thisState, doc));
|
||||||
} else if (doc.isArray()) {
|
|
||||||
callback(LuaEngine::toTable(thisState, doc.array()));
|
|
||||||
} else {
|
|
||||||
sol::state_view lua(thisState);
|
|
||||||
callback(lua.create_table());
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
@@ -11,18 +11,7 @@ void addJsonModule()
|
|||||||
{
|
{
|
||||||
LuaEngine::registerProvider("Json", [](sol::state_view lua) -> sol::object {
|
LuaEngine::registerProvider("Json", [](sol::state_view lua) -> sol::object {
|
||||||
sol::table json = lua.create_table();
|
sol::table json = lua.create_table();
|
||||||
json["encode"] = [](const sol::table &tbl) -> QString {
|
json["encode"] = &LuaEngine::toJsonString;
|
||||||
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 {
|
json["decode"] = [](sol::this_state l, const QString &str) -> sol::table {
|
||||||
QJsonParseError error;
|
QJsonParseError error;
|
||||||
@@ -30,12 +19,7 @@ void addJsonModule()
|
|||||||
if (error.error != QJsonParseError::NoError)
|
if (error.error != QJsonParseError::NoError)
|
||||||
throw sol::error(error.errorString().toStdString());
|
throw sol::error(error.errorString().toStdString());
|
||||||
|
|
||||||
if (doc.isObject())
|
return LuaEngine::toTable(l.lua_state(), doc);
|
||||||
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;
|
return json;
|
||||||
|
@@ -15,6 +15,7 @@
|
|||||||
#include <utils/theme/theme.h>
|
#include <utils/theme/theme.h>
|
||||||
|
|
||||||
#include <QJsonArray>
|
#include <QJsonArray>
|
||||||
|
#include <QJsonDocument>
|
||||||
#include <QJsonObject>
|
#include <QJsonObject>
|
||||||
#include <QStandardPaths>
|
#include <QStandardPaths>
|
||||||
|
|
||||||
@@ -348,6 +349,15 @@ sol::table LuaEngine::toTable(const sol::state_view &lua, const QJsonValue &v)
|
|||||||
return table;
|
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::object &object);
|
||||||
|
|
||||||
QJsonValue toJsonValue(const sol::table &table)
|
QJsonValue toJsonValue(const sol::table &table)
|
||||||
@@ -397,6 +407,16 @@ QJsonValue LuaEngine::toJson(const sol::table &table)
|
|||||||
return toJsonValue(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 LuaEngine::variadicToStringList(const sol::variadic_args &vargs)
|
||||||
{
|
{
|
||||||
QStringList strings;
|
QStringList strings;
|
||||||
|
@@ -66,7 +66,10 @@ public:
|
|||||||
static bool isCoroutine(lua_State *state);
|
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 QJsonValue &v);
|
||||||
|
static sol::table toTable(const sol::state_view &lua, const QJsonDocument &doc);
|
||||||
|
|
||||||
static QJsonValue toJson(const sol::table &t);
|
static QJsonValue toJson(const sol::table &t);
|
||||||
|
static QString toJsonString(const sol::table &t);
|
||||||
|
|
||||||
template<class T>
|
template<class T>
|
||||||
static void checkKey(const sol::table &table, const QString &key)
|
static void checkKey(const sol::table &table, const QString &key)
|
||||||
|
Reference in New Issue
Block a user