From 1856dd2002f032bbfc4ce63ef8e87e4c2a88222b Mon Sep 17 00:00:00 2001 From: Lion Kortlepel Date: Sun, 6 Nov 2022 01:00:20 +0100 Subject: [PATCH] fix various little things in the json encode and decode --- src/LuaAPI.cpp | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/LuaAPI.cpp b/src/LuaAPI.cpp index 8b907e3..2b8155d 100644 --- a/src/LuaAPI.cpp +++ b/src/LuaAPI.cpp @@ -551,6 +551,8 @@ static void JsonEncodeRecursive(nlohmann::json& json, const sol::object& left, c nlohmann::json value; switch (right.get_type()) { case sol::type::lua_nil: + value = nullptr; + return; case sol::type::none: return; case sol::type::poly: @@ -572,7 +574,11 @@ static void JsonEncodeRecursive(nlohmann::json& json, const sol::object& left, c value = right.as(); break; case sol::type::number: - value = right.as(); + if (right.is()) { + value = right.as(); + } else { + value = right.as(); + } break; case sol::type::function: beammp_lua_warn("unsure what to do with function in JsonEncode, ignoring"); @@ -582,6 +588,7 @@ static void JsonEncodeRecursive(nlohmann::json& json, const sol::object& left, c for (const auto& pair : right.as()) { if (pair.first.get_type() != sol::type::number) { local_is_array = false; + break; } } for (const auto& pair : right.as()) { @@ -606,10 +613,11 @@ std::string LuaAPI::MP::JsonEncode(const sol::table& object) { for (const auto& pair : object.as()) { if (pair.first.get_type() != sol::type::number) { is_array = false; + break; } } - for (const auto& entry : object) { - JsonEncodeRecursive(json, entry.first, entry.second, is_array); + for (const auto& [key, value] : object) { + JsonEncodeRecursive(json, key, value, is_array); } return json.dump(); }