add JsonDiffApply, JsonMinify, JsonPrettify, JsonFlatten, JsonUnflatten

This commit is contained in:
Lion Kortlepel 2022-03-03 12:25:06 +01:00
parent 7d97c3b560
commit ca3314b416
No known key found for this signature in database
GPG Key ID: 4322FF2B4C71259B
3 changed files with 56 additions and 0 deletions

View File

@ -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 {

View File

@ -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);
}

View File

@ -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) {