summaryrefslogtreecommitdiff
path: root/plugins/MirLua/Modules/m_json/src/main.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/MirLua/Modules/m_json/src/main.cpp')
-rw-r--r--plugins/MirLua/Modules/m_json/src/main.cpp60
1 files changed, 51 insertions, 9 deletions
diff --git a/plugins/MirLua/Modules/m_json/src/main.cpp b/plugins/MirLua/Modules/m_json/src/main.cpp
index fd7c40b1ff..3774442d7f 100644
--- a/plugins/MirLua/Modules/m_json/src/main.cpp
+++ b/plugins/MirLua/Modules/m_json/src/main.cpp
@@ -13,15 +13,57 @@ static int lua_Decode(lua_State *L)
static int lua_Encode(lua_State *L)
{
- luaL_checktype(L, 1, LUA_TTABLE);
-
- JSONNode *node = json_new(JSON_NODE);
- lua_pushnil(L);
- lua_pushvalue(L, 1);
- lua2json(L, *node);
- lua_pop(L, 2);
- JSON *mt = new (L) JSON(node);
- luaL_setmetatable(L, MT_JSON);
+ switch (lua_type(L, 1))
+ {
+ case LUA_TNIL:
+ lua_pushliteral(L, "null");
+ break;
+ case LUA_TBOOLEAN:
+ lua_pushstring(L, lua_toboolean(L, 1) ? "true" : "false");
+ break;
+ case LUA_TNUMBER:
+ {
+ if (lua_isinteger(L, 1)) {
+ lua_pushfstring(L, "%I", lua_tointeger(L, 1));
+ break;
+ }
+ char decpoint = lua_getlocaledecpoint();
+ if (decpoint != '.') {
+ char p[2] = { decpoint };
+ luaL_gsub(L, lua_tostring(L, 1), p, ".");
+ }
+ else
+ lua_pushfstring(L, "%f", lua_tonumber(L, 1));
+ break;
+ }
+ case LUA_TSTRING:
+ lua_pushfstring(L, "\"%s\"", lua_tostring(L, 1));
+ break;
+ case LUA_TTABLE:
+ {
+ JSONNode node;
+ lua_pushnil(L);
+ lua_pushvalue(L, 1);
+ lua2json(L, node);
+ lua_pop(L, 2);
+ lua_pushstring(L, node.write().c_str());
+ break;
+ }
+ case LUA_TUSERDATA:
+ {
+ JSONNode &node = *((JSON*)luaL_checkudata(L, 1, MT_JSON))->node;
+ lua_pushstring(L, node.write().c_str());
+ break;
+ }
+ case LUA_TLIGHTUSERDATA:
+ if (lua_touserdata(L, 1) == NULL)
+ {
+ lua_pushliteral(L, "null");
+ break;
+ }
+ default:
+ luaL_argerror(L, 1, luaL_typename(L, 1));
+ }
return 1;
}