Beispiel #1
0
static void cjson_add_null_should_fail_with_null_pointers(void)
{
    cJSON *root = cJSON_CreateObject();

    TEST_ASSERT_NULL(cJSON_AddNullToObject(NULL, "null"));
    TEST_ASSERT_NULL(cJSON_AddNullToObject(root, NULL));

    cJSON_Delete(root);
}
Beispiel #2
0
Eina_Bool
azy_content_serialize_response_json(Azy_Content *content)
{
   cJSON *object, *error;
   char *msg;

   if ((!content) || (content->buffer))
     return EINA_FALSE;

   if (content->error_set)
     {
        object = cJSON_CreateObject();
        cJSON_AddNullToObject(object, "result");

        error = cJSON_CreateObject();
        cJSON_AddNumberToObject(error, "code", content->faultcode);
        cJSON_AddStringToObject(error, "message", azy_content_error_message_get(content));

        cJSON_AddItemToObject(object, "error", error);
        cJSON_AddNumberToObject(object, "id", content->id);
     }
   else if (content->retval)
     {
        object = cJSON_CreateObject();
        cJSON_AddItemToObject(object, "result", azy_value_serialize_json(content->retval));
        cJSON_AddNullToObject(object, "error");
        cJSON_AddNumberToObject(object, "id", content->id);
     }
   else
     {
        DBG("No return found in %s!", __PRETTY_FUNCTION__);
        return EINA_FALSE;
     }

   if (eina_log_domain_level_check(azy_log_dom, EINA_LOG_LEVEL_DBG))
     msg = cJSON_Print(object);
   else
     msg = cJSON_PrintUnformatted(object);

   if(!msg) goto free_object;
   azy_content_buffer_set_(content, (unsigned char *)msg, strlen(msg));

   cJSON_Delete(object);
   return EINA_TRUE;

free_object:
   cJSON_Delete(object);
   return EINA_FALSE;
}
Beispiel #3
0
void JSONItem::append(const JSONItem& element)
{
    if(!_json) { return; }

    switch(element.getType()) {
    case cJSON_False:
        cJSON_AddFalseToObject(_json, element.getName().mb_str(wxConvUTF8).data());
        break;

    case cJSON_True:
        cJSON_AddTrueToObject(_json, element.getName().mb_str(wxConvUTF8).data());
        break;

    case cJSON_NULL:
        cJSON_AddNullToObject(_json, element.getName().mb_str(wxConvUTF8).data());
        break;

    case cJSON_Number:
        cJSON_AddNumberToObject(_json, element.getName().mb_str(wxConvUTF8).data(), element.getValue().GetLong());
        break;

    case cJSON_String:
        cJSON_AddStringToObject(_json, element.getName().mb_str(wxConvUTF8).data(),
                                element.getValue().GetString().mb_str(wxConvUTF8).data());
        break;

    case cJSON_Array:
    case cJSON_Object:
        cJSON_AddItemToObject(_json, element.getName().mb_str(wxConvUTF8).data(), element._json);
        break;
    }
}
Beispiel #4
0
char *createHttpResponseJson(HttpResponse *response) {
    cJSON *root = NULL;
    char *out = NULL;
    cJSON *data = NULL;

    root=cJSON_CreateObject();

    if(!response) {
        return cJSON_PrintUnformatted(root);
    }

    cJSON_AddItemToObject(root, "code", cJSON_CreateNumber(response->code));

    if(response->data) {
        data = cJSON_Parse(response->data);

        if(data) {
            cJSON_AddItemToObject(root, "data", data);
        } else {
            cJSON_AddItemToObject(root, "data", cJSON_CreateString(response->data));
        }
    } else {
            cJSON_AddNullToObject(root, "data");
    }

    out = cJSON_PrintUnformatted(root);

    return out;
}
Beispiel #5
0
static void cjson_add_null_should_fail_on_allocation_failure(void)
{
    cJSON *root = cJSON_CreateObject();

    cJSON_InitHooks(&failing_hooks);

    TEST_ASSERT_NULL(cJSON_AddNullToObject(root, "null"));

    cJSON_InitHooks(NULL);

    cJSON_Delete(root);
}
Beispiel #6
0
static void cjson_add_null_should_add_null(void)
{
    cJSON *root = cJSON_CreateObject();
    cJSON *null = NULL;

    cJSON_AddNullToObject(root, "null");

    TEST_ASSERT_NOT_NULL(null = cJSON_GetObjectItemCaseSensitive(root, "null"));
    TEST_ASSERT_EQUAL_INT(null->type, cJSON_NULL);

    cJSON_Delete(root);
}
Beispiel #7
0
static char *jsonrpc_build_cmd(str *method, str *params, int *id)
{
	char *s;
	cJSON *param_obj;
	cJSON *ret_obj;
	char *params_buf;

	/* first thing - try to parse the parameters - need it NULL terminated */
	params_buf = pkg_malloc(params->len + 1);
	if (!params_buf) {
		LM_ERR("cannot allocate memory for params!\n");
		return NULL;
	}
	memcpy(params_buf, params->s, params->len);
	params_buf[params->len] = 0;

	param_obj = cJSON_Parse(params_buf);
	pkg_free(params_buf);
	if (!param_obj) {
		LM_ERR("cannot parse json param: %.*s\n", params->len, params->s);
		return NULL;
	}

	if (param_obj->type != cJSON_Array && param_obj->type != cJSON_Object) {
		LM_ERR("invalid cJSON type %d - must be array or object!\n", param_obj->type);
		cJSON_Delete(param_obj);
		return NULL;
	}
	
	ret_obj = cJSON_CreateObject();
	if (id)
		cJSON_AddNumberToObject(ret_obj, "id", *id);
	else
		cJSON_AddNullToObject(ret_obj, "id");
	cJSON_AddItemToObject(ret_obj, "jsonrpc",
			cJSON_CreateString(JSONRPC_VERSION));
	cJSON_AddItemToObject(ret_obj, "method",
			cJSON_CreateStr(method->s, method->len));
	cJSON_AddItemToObject(ret_obj, "params", param_obj);

	s = cJSON_PrintUnformatted(ret_obj);
	if (!s)
		LM_ERR("cannot print json object!\n");

	cJSON_Delete(ret_obj);
	return s;
}
Beispiel #8
0
int CRedWoodDataParse::AddSensorDataToJson(cJSON* sensorJson, cJSON* descJson)
{
    if (sensorJson == NULL)
        return -1;

    cJSON* jsonSon        = NULL;
    cJSON* jsonGrandChild = NULL;
    jsonSon = sensorJson->child;

    while (jsonSon) {
        jsonGrandChild = jsonSon->child;

        if (jsonGrandChild) {
            //get grandchild value
            switch (jsonGrandChild->type) {
            case cJSON_Number:
                cJSON_AddNumberToObject(descJson, 
                    jsonSon->string, jsonGrandChild->valuedouble);
                break;

            case cJSON_String:
                cJSON_AddStringToObject(descJson, 
                    jsonSon->string, jsonGrandChild->valuestring);
                break;

            default:
                break;
            }
        }
        else {
            cJSON_AddNullToObject(descJson, jsonSon->string);
        }

        jsonSon = jsonSon->next;
    }

    return 0;
}
Beispiel #9
0
static int login_step2()
{
    curl_data_t data_login2 = empty_curl_data;
    curl_header_t header_login2 = empty_curl_header;
    cJSON* cjson_login2_post = cJSON_CreateObject();
    cJSON *cjson_login2 = NULL, *cjson_result;
    str_t post_data = empty_str, tmp = empty_str;
    str_t cookie_str;
    conf_val_t status_val = conf_lookup(&robot.conf, str_from("STATUS"));
    str_t status;
    int rc = 1;

    if (status_val.type != CONF_VALUE_TYPE_STRING)
    {
        fprintf(stdout, "Warning: Unset STATUS variable, the default value is online!!!!\n");
        fflush(stdout);
        status = str_from("online");
    }
    else status = status_val.string;

    cJSON_AddStringToObject(cjson_login2_post, "status", status.ptr);
    cJSON_AddStringToObject(cjson_login2_post, "ptwebqq", robot.ptwebqq.ptr);
    cJSON_AddStringToObject(cjson_login2_post, "passwd_sig", "");
    cJSON_AddStringToObject(cjson_login2_post, "clientid", CLIENTID);
    cJSON_AddNullToObject(cjson_login2_post, "psessionid");
    post_data.ptr = cJSON_PrintUnformatted(cjson_login2_post);
    post_data.len = strlen(post_data.ptr);
    str_cpy(&tmp, str_from("r="));
    str_ncat(&tmp, post_data.ptr, post_data.len);
    str_cat(&tmp, "&clientid="CLIENTID"&psessionid=null");
    str_free(post_data);
    urlencode(tmp, &post_data);

    cookie_str = cookie_to_str(&robot.cookie);
    rc = post_request_with_cookie("https://d.web2.qq.com/channel/login2", 1, "./pems/d.web2.qq.com.pem", post_data.ptr, cookie_str.ptr, &data_login2, &header_login2);
    if (!rc)
    {
        fprintf(stderr, "Call login2 error!!!!\n");
        goto end;
    }

    merge_cookie_to_robot(&header_login2);

    cjson_login2 = cJSON_Parse(data_login2.data.ptr);
    if (cJSON_GetObjectItem(cjson_login2, "retcode")->valueint != 0)
    {
        rc = 0;
        fprintf(stderr, "login2 faild!!!!\n");
        fprintf(stderr, "%s\n", data_login2.data.ptr);
        goto end;
    }

    cjson_result = cJSON_GetObjectItem(cjson_login2, "result");
    str_free(robot.session);
    robot.session = str_dup(cJSON_GetObjectItem(cjson_result, "psessionid")->valuestring);
    str_free(robot.vfwebqq);
    robot.vfwebqq = str_dup(cJSON_GetObjectItem(cjson_result, "vfwebqq")->valuestring);
end:
    curl_data_free(&data_login2);
    pair_array_free(&header_login2);
    cJSON_Delete(cjson_login2_post);
    cJSON_Delete(cjson_login2);
    str_free(post_data);
    str_free(tmp);
    str_free(cookie_str);
    return rc;
}