示例#1
0
文件: json.c 项目: takaaptech/lua333
static cJSON* json_encode_tree(lua_State *L) {
    int type = lua_type(L, -1);
    if(type == LUA_TNIL) {
        cJSON *json = cJSON_CreateNull();
        return json;
    } else if(type == LUA_TBOOLEAN) {
        int b = lua_toboolean(L, -1);
        cJSON* json = cJSON_CreateBool(b);
        return json;
    } else if(type == LUA_TNUMBER) {
        double num = lua_tonumber(L, -1);
        cJSON* json = cJSON_CreateNumber(num);
        return json;
    } else if(type == LUA_TSTRING) {
        size_t str_len;
        const char *string = lua_tolstring(L, -1, &str_len);
        cJSON* json = cJSON_CreateString(string);
        return json;
    } else if(type == LUA_TTABLE) {
        cJSON *json = cJSON_CreateObject();
        if(json == NULL) {
            LOG_ERROR("null");
            return NULL;
        }
        lua_pushnil(L);
        while(lua_next(L, -2) != 0) {
            if(lua_type(L, -2) == LUA_TSTRING) {
                size_t str_len = 0;
                const char *k = lua_tolstring(L, -2, &str_len);
                cJSON *cjson = json_encode_tree(L);
                if(cjson == NULL) {
                    LOG_ERROR("null");
                    break;
                }
                cJSON_AddItemToObject(json, k, cjson);
            } else if(lua_type(L, -2) == LUA_TNUMBER) {
                int k = (int)lua_tonumber(L, -2);
                cJSON *cjson = json_encode_tree(L);
                if(cjson == NULL) {
                    LOG_ERROR("null");
                    break;
                }
                char kname[32];
                sprintf(kname, "%d", k);
                cJSON_AddItemToObject(json, kname, cjson);
            }
            lua_pop(L, 1);
        }
        return json;
    }
    LOG_ERROR("unspport type");
    return NULL;
}
示例#2
0
int write_json_result(struct mg_connection *conn, int code, char * msg)
{
    char * buff;
    int ret;
    cJSON *croot;
    croot = cJSON_CreateObject();
    cJSON_AddItemToObject(croot,"Code",cJSON_CreateBool(code));
    cJSON_AddItemToObject(croot,"Msg",cJSON_CreateString(msg));
    buff =  cJSON_PrintUnformatted(croot);
    ret = mg_printf_data(conn,"Result=%s",buff);
    cJSON_Delete(croot);
    return ret;
}
bool handleTurnSignalCommand(const char* name, cJSON* value, cJSON* event,
        CanSignal* signals, int signalCount) {
    const char* direction = value->valuestring;
    CanSignal* signal = NULL;
    if(!strcmp("left", direction)) {
        signal = lookupSignal("turn_signal_left", signals, signalCount);
    } else if(!strcmp("right", direction)) {
        signal = lookupSignal("turn_signal_right", signals, signalCount);
    }

    if(signal != NULL) {
        return sendCanSignal(signal, cJSON_CreateBool(true), booleanWriter,
                signals, signalCount, true);
    } else {
        debug("Unable to find signal for %s turn signal", direction);
    }
    return false;
}
示例#4
0
void update_soda_status(unsigned char sold_out_mask)
	{
	struct cJSON *json, *item, *prev, *result, *data = cJSON_CreateObject();
	unsigned char i;
	unsigned char *out;
	char rand[RAND_SIZE];

	json = cJSON_CreateArray();
	for (i = 0; i < 8; i++)
		{
		item = cJSON_CreateObject();
		cJSON_AddItemToObjectCS(item, "slot", cJSON_CreateNumber(i + 1));
		cJSON_AddItemToObjectCS(item, "sold_out", cJSON_CreateBool(sold_out_mask & (1 << i)));
		if (!i)
			json->child = item;
		else
			{
			prev->next = item;
			item->prev = prev;
			}
		prev = item;
		}
	
	cJSON_AddItemToObjectCS(data, "nonce",           cJSON_CreateString(nonce));
	cJSON_AddItemToObjectCS(data, "operation",       cJSON_CreateString("soda_status"));
	add_random_response(data, rand);
	cJSON_AddItemToObjectCS(data, "soda_status",     json);
	cJSON_AddItemToObjectCS(data, "version",         cJSON_CreateNumber(2));
	
	i = http_request(data, &result, rand);

	if (i == RESPONSE_GOOD)
		{
		json = cJSON_GetObjectItem(result, "error");
		out = json ? (unsigned char *)json->valuestring : NULL;
		log_msg("Sold out updated: %s", out);
		cJSON_Delete(result);
		}
	}
示例#5
0
static cJSON *
_encode_item(lua_State *L, int idx)
{
	switch(lua_type(L, idx))
	{
		case LUA_TNIL:
			return cJSON_CreateNull();
		case LUA_TBOOLEAN:
			return cJSON_CreateBool(lua_toboolean(L, idx));
		case LUA_TNUMBER:
			return cJSON_CreateNumber(luaL_checknumber(L, idx));
		case LUA_TSTRING:
			return cJSON_CreateString(luaL_checkstring(L, idx));
		case LUA_TTABLE:
			if(!lua_objlen(L, idx))
				return _encode_object(L, idx);
			else
				return _encode_array(L, idx);
		default:
			fprintf(stderr, "cannot encode %s", lua_typename(L, idx));
			return NULL;
	}
}
示例#6
0
/**
 * @brief Add a boolean value to the array.
 * @param [in] value The boolean value to add to the array.
 */
void JsonArray::addBoolean(bool value) {
	cJSON_AddItemToArray(m_node, cJSON_CreateBool(value));
} // addBoolean
示例#7
0
文件: json.c 项目: canmor-lam/libsg
int sg_json_set_bool(sg_json_doc_t *doc, const char *json_pointer, bool value)
{
	cJSON *json = cast_to_cJSON(doc);
	return set_value(json, json_pointer, cJSON_CreateBool(value));
}
示例#8
0
void sendEventedBooleanMessage(const char* name, const char* value, bool event,
                               Listener* listener) {
    sendJSONMessage(name, cJSON_CreateString(value), cJSON_CreateBool(event),
                    listener);
}
示例#9
0
void sendBooleanMessage(const char* name, bool value, Listener* listener) {
    sendJSONMessage(name, cJSON_CreateBool(value), NULL, listener);
}