diff options
Diffstat (limited to 'plugins/MirLua/Modules/m_json/src/main.cpp')
-rw-r--r-- | plugins/MirLua/Modules/m_json/src/main.cpp | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/plugins/MirLua/Modules/m_json/src/main.cpp b/plugins/MirLua/Modules/m_json/src/main.cpp new file mode 100644 index 0000000000..fe987d36f3 --- /dev/null +++ b/plugins/MirLua/Modules/m_json/src/main.cpp @@ -0,0 +1,48 @@ +#include "stdafx.h"
+
+static int lua_Decode(lua_State *L)
+{
+ const char *string = luaL_checkstring(L, 1);
+
+ JSONNode *node = json_parse(string);
+ new (L) JSON(node);
+ luaL_setmetatable(L, MT_JSON);
+
+ return 1;
+}
+
+static int lua_Encode(lua_State *L)
+{
+ luaL_checktype(L, 1, LUA_TTABLE);
+
+ JSONNode *node = json_new(JSON_NODE);
+ lua_pushnil(L);
+ while (lua_next(L, 1) != 0)
+ {
+ lua2json(L, *node);
+ lua_pop(L, 1);
+ }
+ JSON *mt = new (L) JSON(node);
+ luaL_setmetatable(L, MT_JSON);
+
+ return 1;
+}
+
+static const luaL_Reg methods[] =
+{
+ { "Dcode", lua_Decode },
+ { "Encode", lua_Encode },
+
+ { NULL, NULL }
+};
+
+extern "C" LUAMOD_API int luaopen_m_json(lua_State *L)
+{
+ luaL_newlib(L, methods);
+
+ luaL_newmetatable(L, MT_JSON);
+ luaL_setfuncs(L, jsonApi, 0);
+ lua_pop(L, 1);
+
+ return 1;
+}
\ No newline at end of file |