mirror of
https://github.com/BeamMP/BeamMP-Server.git
synced 2025-07-01 23:35:41 +00:00
add JsonDiffApply, JsonMinify, JsonPrettify, JsonFlatten, JsonUnflatten
This commit is contained in:
parent
7d97c3b560
commit
ca3314b416
@ -24,6 +24,11 @@ namespace MP {
|
||||
void PrintRaw(sol::variadic_args);
|
||||
std::string JsonEncode(const sol::table& object);
|
||||
std::string JsonDiff(const std::string& a, const std::string& b);
|
||||
std::string JsonDiffApply(const std::string& data, const std::string& patch);
|
||||
std::string JsonPrettify(const std::string& json);
|
||||
std::string JsonMinify(const std::string& json);
|
||||
std::string JsonFlatten(const std::string& json);
|
||||
std::string JsonUnflatten(const std::string& json);
|
||||
}
|
||||
|
||||
namespace FS {
|
||||
|
@ -445,3 +445,50 @@ std::string LuaAPI::MP::JsonDiff(const std::string& a, const std::string& b) {
|
||||
auto b_json = nlohmann::json::parse(b);
|
||||
return nlohmann::json::diff(a_json, b_json).dump();
|
||||
}
|
||||
|
||||
std::string LuaAPI::MP::JsonDiffApply(const std::string& data, const std::string& patch) {
|
||||
if (!nlohmann::json::accept(data)) {
|
||||
beammp_lua_error("JsonDiffApply first argument is not valid json: `" + data + "`");
|
||||
return "";
|
||||
}
|
||||
if (!nlohmann::json::accept(patch)) {
|
||||
beammp_lua_error("JsonDiffApply second argument is not valid json: `" + patch + "`");
|
||||
return "";
|
||||
}
|
||||
auto a_json = nlohmann::json::parse(data);
|
||||
auto b_json = nlohmann::json::parse(patch);
|
||||
a_json.patch(b_json);
|
||||
return a_json.dump();
|
||||
}
|
||||
|
||||
std::string LuaAPI::MP::JsonPrettify(const std::string& json) {
|
||||
if (!nlohmann::json::accept(json)) {
|
||||
beammp_lua_error("JsonPrettify argument is not valid json: `" + json + "`");
|
||||
return "";
|
||||
}
|
||||
return nlohmann::json::parse(json).dump(4);
|
||||
}
|
||||
|
||||
std::string LuaAPI::MP::JsonMinify(const std::string& json) {
|
||||
if (!nlohmann::json::accept(json)) {
|
||||
beammp_lua_error("JsonMinify argument is not valid json: `" + json + "`");
|
||||
return "";
|
||||
}
|
||||
return nlohmann::json::parse(json).dump(-1);
|
||||
}
|
||||
|
||||
std::string LuaAPI::MP::JsonFlatten(const std::string& json) {
|
||||
if (!nlohmann::json::accept(json)) {
|
||||
beammp_lua_error("JsonFlatten argument is not valid json: `" + json + "`");
|
||||
return "";
|
||||
}
|
||||
return nlohmann::json::parse(json).flatten().dump(-1);
|
||||
}
|
||||
|
||||
std::string LuaAPI::MP::JsonUnflatten(const std::string& json) {
|
||||
if (!nlohmann::json::accept(json)) {
|
||||
beammp_lua_error("JsonUnflatten argument is not valid json: `" + json + "`");
|
||||
return "";
|
||||
}
|
||||
return nlohmann::json::parse(json).unflatten().dump(-1);
|
||||
}
|
||||
|
@ -665,6 +665,10 @@ TLuaEngine::StateThreadData::StateThreadData(const std::string& Name, std::atomi
|
||||
return Lua_JsonDecode(str);
|
||||
});
|
||||
MPTable.set_function("JsonDiff", &LuaAPI::MP::JsonDiff);
|
||||
MPTable.set_function("JsonFlatten", &LuaAPI::MP::JsonFlatten);
|
||||
MPTable.set_function("JsonUnflatten", &LuaAPI::MP::JsonUnflatten);
|
||||
MPTable.set_function("JsonPrettify", &LuaAPI::MP::JsonPrettify);
|
||||
MPTable.set_function("JsonMinify", &LuaAPI::MP::JsonMinify);
|
||||
|
||||
auto HttpTable = StateView.create_named_table("Http");
|
||||
HttpTable.set_function("CreateConnection", [this](const std::string& host, uint16_t port) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user