Ejemplo n.º 1
0
static int json_stringify(lua_State *L)
{
    struct json_object *obj = _lua_to_json(L, 1);
    bool pretty = lua_toboolean(L, 2);
    int flags = 0;

    if (pretty)
        flags |= JSON_C_TO_STRING_PRETTY | JSON_C_TO_STRING_SPACED;

    lua_pushstring(L, json_object_to_json_string_ext(obj, flags));
    return 1;
}
Ejemplo n.º 2
0
static Json _lua_to_json(lua_State *l, int idx)
{
	int data = lua_absindex(l, idx);

	switch (lua_type(l, data)) {
		case LUA_TNIL:
			return Json();

		case LUA_TNUMBER:
			return Json(lua_tonumber(l, data));

		case LUA_TBOOLEAN:
			return Json(lua_toboolean(l, data));

		case LUA_TSTRING:
			return Json(lua_tostring(l, data));

		case LUA_TTABLE: {

			// XXX handle arrays

			Json object(Json::objectValue);

			lua_pushnil(l);
			while (lua_next(l, data)) {
				const std::string key(luaL_checkstring(l, -2));
				Json value(_lua_to_json(l, -1));
				object[key] = value;
				lua_pop(l, 1);
			}

			return object;
		}

		default:
			luaL_error(l, "can't convert Lua type %s to JSON", lua_typename(l, lua_type(l, idx)));
			return Json();
	}

	// shouldn't get here
	assert(0);

	return Json();
}
Ejemplo n.º 3
0
/*
 * Function: Call
 *
 * Call a remote function, optionally sending or receiving data.
 *
 * > ServerAgent.Call(method, data, onSuccess, onFailure)
 *
 * Parameters:
 *
 *   method - name of the remote function/method to call.
 *
 *   data - optional. an arbitrary data item to be sent to the server along
 *          with the call.
 *
 *   onSuccess - optional. a function to call when the remote call completes
 *               successfully. If the call returned data, it will be passed as
 *               the first argument to the function.
 *
 *   onFailure - optional. a function to call if the remote call fails for any
 *               reason. The text of the error will be passed as the first
 *               argument to the function.
 *
 * Availability:
 *
 *   alpha 29
 *
 * Status:
 *
 *   experimental
 */
static int l_serveragent_call(lua_State *l)
{
	const std::string method(luaL_checkstring(l, 1));
	const Json data(_lua_to_json(l, 2));

	int successIndex = LUA_NOREF, failIndex = LUA_NOREF;

	if (lua_gettop(l) > 2) {
		luaL_checktype(l, 3, LUA_TFUNCTION);
		successIndex = 3;
	}
	if (lua_gettop(l) > 3) {
		luaL_checktype(l, 4, LUA_TFUNCTION);
		failIndex = 4;
	}

	CallbackPair *cp = new CallbackPair(l, successIndex, failIndex);

	Pi::serverAgent->Call(method, data, sigc::ptr_fun(_success_callback), sigc::ptr_fun(_fail_callback), cp);

	return 0;
}