summaryrefslogtreecommitdiff
path: root/plugins/MirLua/Modules/m_json/src/main.cpp
blob: fe987d36f31d87ec8fe24d2cbb731ec3039c584f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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;
}