diff --git a/include/LuaAPI.h b/include/LuaAPI.h index 23d40c0..74286fd 100644 --- a/include/LuaAPI.h +++ b/include/LuaAPI.h @@ -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 { diff --git a/src/LuaAPI.cpp b/src/LuaAPI.cpp index a7ced6f..faa0739 100644 --- a/src/LuaAPI.cpp +++ b/src/LuaAPI.cpp @@ -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); +} diff --git a/src/TLuaEngine.cpp b/src/TLuaEngine.cpp index 5ae4b0d..c71a9c5 100644 --- a/src/TLuaEngine.cpp +++ b/src/TLuaEngine.cpp @@ -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) {