Example #1
0
void lua2json(lua_State *L, JSONNode &node)
{
	const char *name = lua_tostring(L, -2);

	switch (lua_type(L, -1))
	{
	case LUA_TNIL:
		node.nullify();
		break;
	case LUA_TSTRING:
		node = lua_tostring(L, -1);
		break;
	case LUA_TBOOLEAN:
		node = lua_toboolean(L, -1) != 0;
		break;
	case LUA_TNUMBER:
		node = lua_tonumber(L, -1);
		break;
	case LUA_TTABLE:
	{
		node.cast(JSON_NODE);
		if (name)
			node.set_name(name);

		lua_pushnil(L);
		while (lua_next(L, -2) != 0)
		{
			JSONNode child;
			lua2json(L, child);
			node << child;

			lua_pop(L, 1);
		}
	}
	}
}