Use type switch in JsonEncode
lua_isnumber and lua_isstring try to coerce the values to their respective type, so a type switch is more reliable. Fixes https://github.com/Simply-Love/Simply-Love-SM5/issues/406
This commit is contained in:
+8
-13
@@ -2532,16 +2532,13 @@ int LuaFunc_JsonEncode(lua_State* L)
|
|||||||
|
|
||||||
std::function<Json::Value(int)> convert = [&L, &convert](int index) -> Json::Value
|
std::function<Json::Value(int)> convert = [&L, &convert](int index) -> Json::Value
|
||||||
{
|
{
|
||||||
if (lua_isboolean(L, index))
|
switch (lua_type(L, index))
|
||||||
{
|
|
||||||
return Json::Value(static_cast<bool>(lua_toboolean(L, index)));
|
|
||||||
}
|
|
||||||
else if (lua_isnil(L, index))
|
|
||||||
{
|
{
|
||||||
|
case LUA_TNIL:
|
||||||
return Json::Value(Json::nullValue);
|
return Json::Value(Json::nullValue);
|
||||||
}
|
case LUA_TBOOLEAN:
|
||||||
else if (lua_isnumber(L, index))
|
return Json::Value(static_cast<bool>(lua_toboolean(L, index)));
|
||||||
{
|
case LUA_TNUMBER: {
|
||||||
double val = lua_tonumber(L, index);
|
double val = lua_tonumber(L, index);
|
||||||
|
|
||||||
if (val == static_cast<Json::UInt>(val))
|
if (val == static_cast<Json::UInt>(val))
|
||||||
@@ -2554,15 +2551,13 @@ int LuaFunc_JsonEncode(lua_State* L)
|
|||||||
}
|
}
|
||||||
return Json::Value(val);
|
return Json::Value(val);
|
||||||
}
|
}
|
||||||
else if (lua_isstring(L, index))
|
case LUA_TSTRING: {
|
||||||
{
|
|
||||||
size_t len;
|
size_t len;
|
||||||
const char *s = lua_tolstring(L, index, &len);
|
const char *s = lua_tolstring(L, index, &len);
|
||||||
|
|
||||||
return Json::Value(std::string(s, len));
|
return Json::Value(std::string(s, len));
|
||||||
}
|
}
|
||||||
else if (lua_istable(L, index))
|
case LUA_TTABLE: {
|
||||||
{
|
|
||||||
// if the index is relative to the top of the stack,
|
// if the index is relative to the top of the stack,
|
||||||
// then calculate the absolute index, so we have a
|
// then calculate the absolute index, so we have a
|
||||||
// stable reference
|
// stable reference
|
||||||
@@ -2613,7 +2608,7 @@ int LuaFunc_JsonEncode(lua_State* L)
|
|||||||
}
|
}
|
||||||
return obj;
|
return obj;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int tp = lua_type(L, index);
|
int tp = lua_type(L, index);
|
||||||
|
|||||||
Reference in New Issue
Block a user