From 9a0cdc6517940f3950862c5d26e6ddba4afecb97 Mon Sep 17 00:00:00 2001 From: Lion Kortlepel Date: Wed, 2 Mar 2022 13:26:19 +0100 Subject: [PATCH] rename JsonSerialize to JsonEncode, JsonDeserialize to JsonDecode --- Changelog.md | 2 +- include/LuaAPI.h | 2 +- include/TLuaEngine.h | 2 +- src/LuaAPI.cpp | 20 ++++++++++---------- src/TLuaEngine.cpp | 24 ++++++++++++------------ 5 files changed, 25 insertions(+), 25 deletions(-) diff --git a/Changelog.md b/Changelog.md index 4ce45d7..00f0b48 100644 --- a/Changelog.md +++ b/Changelog.md @@ -1,7 +1,7 @@ # v3.0.1 - ADDED Backup URLs to UpdateCheck (will fail less often now) -- ADDED MP.JsonSerialize() and MP.JsonDeserialize(), which turn lua tables into json and vice-versa +- ADDED MP.JsonEncode() and MP.JsonDecode(), which turn lua tables into json and vice-versa - FIXED a bug where, when run with --working-directory, the Server.log would still be in the original directory - FIXED a bug which could cause the plugin reload thread to spin at 100% if the reloaded plugin's didn't terminate - FIXED an issue which would cause servers to crash on mod download via SIGPIPE on POSIX diff --git a/include/LuaAPI.h b/include/LuaAPI.h index b25c740..e317a23 100644 --- a/include/LuaAPI.h +++ b/include/LuaAPI.h @@ -22,7 +22,7 @@ namespace MP { bool IsPlayerConnected(int ID); void Sleep(size_t Ms); void PrintRaw(sol::variadic_args); - std::string JsonSerialize(const sol::table& object); + std::string JsonEncode(const sol::table& object); } namespace FS { std::pair CreateDirectory(const std::string& Path); diff --git a/include/TLuaEngine.h b/include/TLuaEngine.h index fb1353e..973841d 100644 --- a/include/TLuaEngine.h +++ b/include/TLuaEngine.h @@ -190,7 +190,7 @@ private: std::string Lua_GetPlayerName(int ID); sol::table Lua_GetPlayerVehicles(int ID); sol::table Lua_HttpCreateConnection(const std::string& host, uint16_t port); - sol::table Lua_JsonDeserialize(const std::string& str); + sol::table Lua_JsonDecode(const std::string& str); int Lua_GetPlayerIDByName(const std::string& Name); std::string mName; diff --git a/src/LuaAPI.cpp b/src/LuaAPI.cpp index 53f28ad..1baa467 100644 --- a/src/LuaAPI.cpp +++ b/src/LuaAPI.cpp @@ -354,7 +354,7 @@ std::string LuaAPI::FS::ConcatPaths(sol::variadic_args Args) { return Result; } -static void JsonSerializeRecursive(nlohmann::json& json, const sol::object& left, const sol::object& right, size_t depth = 0) { +static void JsonEncodeRecursive(nlohmann::json& json, const sol::object& left, const sol::object& right, size_t depth = 0) { if (depth > 100) { beammp_lua_error("json serialize will not go deeper than 100 nested tables, internal references assumed, aborted this path"); return; @@ -371,7 +371,7 @@ static void JsonSerializeRecursive(nlohmann::json& json, const sol::object& left case sol::type::thread: case sol::type::function: case sol::type::table: - beammp_lua_error("JsonSerialize: left side of table field is unexpected type"); + beammp_lua_error("JsonEncode: left side of table field is unexpected type"); return; case sol::type::string: key = left.as(); @@ -387,19 +387,19 @@ static void JsonSerializeRecursive(nlohmann::json& json, const sol::object& left case sol::type::none: return; case sol::type::poly: - beammp_lua_warn("unsure what to do with poly type in JsonSerialize, ignoring"); + beammp_lua_warn("unsure what to do with poly type in JsonEncode, ignoring"); return; case sol::type::boolean: value = right.as(); break; case sol::type::lightuserdata: - beammp_lua_warn("unsure what to do with lightuserdata in JsonSerialize, ignoring"); + beammp_lua_warn("unsure what to do with lightuserdata in JsonEncode, ignoring"); return; case sol::type::userdata: - beammp_lua_warn("unsure what to do with userdata in JsonSerialize, ignoring"); + beammp_lua_warn("unsure what to do with userdata in JsonEncode, ignoring"); return; case sol::type::thread: - beammp_lua_warn("unsure what to do with thread in JsonSerialize, ignoring"); + beammp_lua_warn("unsure what to do with thread in JsonEncode, ignoring"); return; case sol::type::string: value = right.as(); @@ -408,11 +408,11 @@ static void JsonSerializeRecursive(nlohmann::json& json, const sol::object& left value = right.as(); break; case sol::type::function: - beammp_lua_warn("unsure what to do with function in JsonSerialize, ignoring"); + beammp_lua_warn("unsure what to do with function in JsonEncode, ignoring"); return; case sol::type::table: for (const auto& pair : right.as()) { - JsonSerializeRecursive(value, pair.first, pair.second, depth + 1); + JsonEncodeRecursive(value, pair.first, pair.second, depth + 1); } break; } @@ -423,11 +423,11 @@ static void JsonSerializeRecursive(nlohmann::json& json, const sol::object& left } } -std::string LuaAPI::MP::JsonSerialize(const sol::table& object) { +std::string LuaAPI::MP::JsonEncode(const sol::table& object) { nlohmann::json json; // table for (const auto& entry : object) { - JsonSerializeRecursive(json, entry.first, entry.second); + JsonEncodeRecursive(json, entry.first, entry.second); } return json.dump(); } diff --git a/src/TLuaEngine.cpp b/src/TLuaEngine.cpp index 69b3c11..21adcc2 100644 --- a/src/TLuaEngine.cpp +++ b/src/TLuaEngine.cpp @@ -483,7 +483,7 @@ static void AddToTable(sol::table& table, const std::string& left, const T& valu } } -static void JsonDeserializeRecursive(sol::state_view& StateView, sol::table& table, const std::string& left, const nlohmann::json& right) { +static void JsonDecodeRecursive(sol::state_view& StateView, sol::table& table, const std::string& left, const nlohmann::json& right) { switch (right.type()) { case nlohmann::detail::value_t::null: return; @@ -491,7 +491,7 @@ static void JsonDeserializeRecursive(sol::state_view& StateView, sol::table& tab auto value = table.create(); value.clear(); for (const auto& entry : right.items()) { - JsonDeserializeRecursive(StateView, value, entry.key(), entry.value()); + JsonDecodeRecursive(StateView, value, entry.key(), entry.value()); } AddToTable(table, left, value); break; @@ -500,7 +500,7 @@ static void JsonDeserializeRecursive(sol::state_view& StateView, sol::table& tab auto value = table.create(); value.clear(); for (const auto& entry : right.items()) { - JsonDeserializeRecursive(StateView, value, "", entry.value()); + JsonDecodeRecursive(StateView, value, "", entry.value()); } AddToTable(table, left, value); break; @@ -521,31 +521,31 @@ static void JsonDeserializeRecursive(sol::state_view& StateView, sol::table& tab AddToTable(table, left, right.get()); break; case nlohmann::detail::value_t::binary: - beammp_lua_error("JsonDeserialize can't handle binary blob in json, ignoring"); + beammp_lua_error("JsonDecode can't handle binary blob in json, ignoring"); return; case nlohmann::detail::value_t::discarded: return; } } -sol::table TLuaEngine::StateThreadData::Lua_JsonDeserialize(const std::string& str) { +sol::table TLuaEngine::StateThreadData::Lua_JsonDecode(const std::string& str) { sol::state_view StateView(mState); auto table = StateView.create_table(); if (!nlohmann::json::accept(str)) { - beammp_lua_error("string given to JsonDeserialize is not valid json."); + beammp_lua_error("string given to JsonDecode is not valid json."); return sol::lua_nil; } nlohmann::json json = nlohmann::json::parse(str); if (json.is_object()) { for (const auto& entry : json.items()) { - JsonDeserializeRecursive(StateView, table, entry.key(), entry.value()); + JsonDecodeRecursive(StateView, table, entry.key(), entry.value()); } } else if (json.is_array()) { for (const auto& entry : json) { - JsonDeserializeRecursive(StateView, table, "", entry); + JsonDecodeRecursive(StateView, table, "", entry); } } else { - beammp_lua_error("JsonDeserialize expected array or object json, instead got " + std::string(json.type_name())); + beammp_lua_error("JsonDecode expected array or object json, instead got " + std::string(json.type_name())); return sol::lua_nil; } return table; @@ -634,9 +634,9 @@ TLuaEngine::StateThreadData::StateThreadData(const std::string& Name, std::atomi mEngine->CancelEventTimers(EventName, mStateId); }); MPTable.set_function("Set", &LuaAPI::MP::Set); - MPTable.set_function("JsonSerialize", &LuaAPI::MP::JsonSerialize); - MPTable.set_function("JsonDeserialize", [this](const std::string& str) { - return Lua_JsonDeserialize(str); + MPTable.set_function("JsonEncode", &LuaAPI::MP::JsonEncode); + MPTable.set_function("JsonDecode", [this](const std::string& str) { + return Lua_JsonDecode(str); }); auto HttpTable = StateView.create_named_table("Http");