예제 #1
0
static void _json_to_lua(lua_State *l, const Json &data)
{
	LUA_DEBUG_START(l);

	switch (data.type()) {
		case Json::nullValue:
			lua_pushnil(l);
			break;

		case Json::intValue:
		case Json::uintValue:
		case Json::realValue:
			lua_pushnumber(l, data.asDouble());
			break;

		case Json::stringValue: {
			const std::string &str(data.asString());
			lua_pushlstring(l, str.c_str(), str.size());
			break;
		}

		case Json::booleanValue:
			lua_pushboolean(l, data.asBool());
			break;

		case Json::arrayValue: {
			lua_newtable(l);
			for (int i = 0; i < int(data.size()); i++) {
				lua_pushinteger(l, i+1);
				_json_to_lua(l, data[i]);
				lua_rawset(l, -3);
			}
			break;
		}

		case Json::objectValue: {
			lua_newtable(l);
			for (Json::const_iterator i = data.begin(); i != data.end(); ++i) {
				const std::string &key(i.key().asString());
				lua_pushlstring(l, key.c_str(), key.size());
				_json_to_lua(l, *i);
				lua_rawset(l, -3);
			}
			break;
		}
	}

	LUA_DEBUG_END(l, 1);
}
예제 #2
0
파일: jsonc.c 프로젝트: arychen/GlobalCall
static int json_parse(lua_State *L)
{
    size_t len;
    const char *json = luaL_checklstring(L, 1, &len);
    struct json_state s = {
        .tok = json_tokener_new()
    };

    if (!s.tok)
        return 0;

    s.obj = json_tokener_parse_ex(s.tok, json, len);
    s.err = json_tokener_get_error(s.tok);

    if (s.obj)
    {
        _json_to_lua(L, s.obj);
        json_object_put(s.obj);
    }
    else
    {
        lua_pushnil(L);
    }

    if (s.err == json_tokener_continue)
        s.err = json_tokener_error_parse_eof;

    if (s.err)
        lua_pushstring(L, json_tokener_error_desc(s.err));

    json_tokener_free(s.tok);
    return (1 + !!s.err);
}
예제 #3
0
파일: jsonc.c 프로젝트: arychen/GlobalCall
static void _json_to_lua(lua_State *L, struct json_object *obj)
{
    int n;

    switch (json_object_get_type(obj))
    {
    case json_type_object:
        lua_newtable(L);
        json_object_object_foreach(obj, key, val)
        {
            _json_to_lua(L, val);
            lua_setfield(L, -2, key);
        }
        break;

    case json_type_array:
        lua_newtable(L);
        for (n = 0; n < json_object_array_length(obj); n++)
        {
            _json_to_lua(L, json_object_array_get_idx(obj, n));
            lua_rawseti(L, -2, n + 1);
        }
        break;

    case json_type_boolean:
        lua_pushboolean(L, json_object_get_boolean(obj));
        break;

    case json_type_int:
        lua_pushinteger(L, json_object_get_int(obj));
        break;

    case json_type_double:
        lua_pushnumber(L, json_object_get_double(obj));
        break;

    case json_type_string:
        lua_pushstring(L, json_object_get_string(obj));
        break;

    case json_type_null:
        lua_pushnil(L);
        break;
    }
예제 #4
0
static void _success_callback(const Json &data, void *userdata)
{
	CallbackPair *cp = reinterpret_cast<CallbackPair*>(userdata);
	if (!cp->successCallback.IsValid()) return;

	cp->successCallback.PushCopyToStack();

	_json_to_lua(cp->lua, data);
	pi_lua_protected_call(cp->lua, 1, 0);

	delete cp;
}