int Parser_string(char *pCommData, char **outputStr, int repCommandID) { char * out = NULL; int outLen = 0; cJSON *pSUSICommDataItem = NULL; if(pCommData == NULL || outputStr == NULL) return outLen; pSUSICommDataItem = cJSON_CreateObject(); switch(repCommandID) { case swm_set_mon_objs_rep: { cJSON_AddStringToObject(pSUSICommDataItem, SWM_SET_MON_OBJS_REP, pCommData); break; } case swm_error_rep: { cJSON_AddStringToObject(pSUSICommDataItem, SWM_ERROR_REP, pCommData); break; } //case swm_kill_prc_rep: // { // break; // } //case swm_restart_prc_rep: //case swm_del_all_mon_objs_rep: //case swm_set_pmi_auto_upload_rep: //case swm_set_pmi_reqp_req: default: { cJSON_AddStringToObject(pSUSICommDataItem, SWM_SET_MON_OBJS_REP, pCommData); break; } } out = cJSON_PrintUnformatted(pSUSICommDataItem); outLen = strlen(out) + 1; *outputStr = (char *)(malloc(outLen)); memset(*outputStr, 0, outLen); strcpy(*outputStr, out); cJSON_Delete(pSUSICommDataItem); printf("%s\n",out); free(out); return outLen; }
void Client::createTurnRoom(std::string name, std::string owner, int max, std::map<std::string,std::string> properties, int time) { if((_state != ConnectionState::connected) || (_socket == NULL)){ if(_zonelistener != NULL) { room _room; _room.result = ResultCode::connection_error; _zonelistener->onCreateRoomDone(_room); } return; } std::map<std::string,std::string>::iterator it; cJSON *propJSON; propJSON = cJSON_CreateObject(); for(it = properties.begin(); it != properties.end(); ++it) { cJSON_AddStringToObject(propJSON, it->first.c_str(),it->second.c_str()); } char *cRet = cJSON_PrintUnformatted(propJSON); std::string prop = cRet; if(prop.length() >= MAX_PROPERTY_SIZE_BYTES) { room _room; _room.result = ResultCode::size_error; if(_zonelistener != NULL) _zonelistener->onCreateRoomDone(_room); } int byteLen; byte *roomReq = buildCreateRoomRequest(name,owner,max, prop, time, byteLen); char *data = new char[byteLen]; for(int i=0; i< byteLen; ++i) { data[i] = roomReq[i]; } _socket->sockSend(data, byteLen); delete[] data; delete[] roomReq; cJSON_Delete(propJSON); free(cRet); }
size_t readfunction( void *ptr, size_t size, size_t nmemb, void *userdata) { int length = -1; char * cipher=NULL, content[CONTENT_LEN]={0}; char *out=NULL; char EpochTime[16]={0}; cJSON *root=NULL; struct NodeStatus * ns = (struct NodeStatus *)userdata; if(transfered) return 0; root=cJSON_CreateObject(); sprintf(EpochTime,"%lx",ns->EpochTime); cJSON_AddStringToObject(root,"EpochTime",EpochTime); cJSON_AddStringToObject(root,"NodeName",ns->NodeName); if(svrversion) { cJSON_AddStringToObject(root,"Version",ns->Version); } if(svrtype) { cJSON_AddNumberToObject(root,"SvrType",ns->SvrType); } strcpy(content, out=cJSON_PrintUnformatted(root)); cJSON_Delete(root); free(out); log4c_cdn(mycat, info, "POST", "%s",content); length = ContentEncode(NODE_3DES_KEY, NODE_3DES_IV, content, &cipher, strlen(content)); if(length<0){ log4c_cdn(mycat, error, "ENDEC", "ContentEncode() failed"); } if(length<0) exit(1); memcpy(ptr,cipher,length); free(cipher); transfered=1; return length; }
string App42Service::getJsonStringFromApp42ACLList(vector<App42ACL> &App42ACLObjectArray) { cJSON *jsonArray = cJSON_CreateArray(); for (vector<App42ACL>::iterator it = App42ACLObjectArray.begin(); it!= App42ACLObjectArray.end(); it++) { cJSON *jsonObject = cJSON_CreateObject(); cJSON_AddStringToObject(jsonObject, it->getUserName().c_str(), it->getPermission().c_str()); cJSON_AddItemToArray(jsonArray, jsonObject); //cJSON_Delete(jsonObject); } char *cptrFormatted = cJSON_PrintUnformatted(jsonArray); string jsonString = cptrFormatted; free(cptrFormatted); cJSON_Delete(jsonArray); return jsonString; }
int main(void) { const char *expected = "{\"foo\":\"bar\"}"; char *str; int retcode = EXIT_SUCCESS; cJSON *obj; obj = cJSON_CreateObject(); cJSON_AddStringToObject(obj, "foo", "bar"); str = cJSON_PrintUnformatted(obj); if (strcmp(str, expected) != 0) { fprintf(stderr, "Expected %s got %s\n", expected, str); retcode = EXIT_FAILURE; } cJSON_Delete(obj); cJSON_Free(str); return retcode; }
int callback(void *response, int argc, char **argv,char **azColName) { cJSON *root,*fmt; int i; root=cJSON_CreateObject(); cJSON_AddItemToObject(root, "car",fmt=cJSON_CreateObject()); for (i = 0; i < argc; i++) { printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL"); cJSON_AddStringToObject(fmt,azColName[i],argv[i]); } strcpy((char *)response,cJSON_PrintUnformatted(root)); //printf("%s",cJSON_PrintUnformatted(root)); cJSON_Delete(root); return 0; }
void App42Response::buildJsonDocument(cJSON *json, JSONDocument *jsonDocumnet) { cJSON *docIdJson = Util::getJSONChild("_id", json); if (docIdJson!=NULL) { jsonDocumnet->setDocId(Util::getJSONString("$oid", docIdJson)); cJSON_DeleteItemFromObject(json, "_id"); } cJSON *ownerJson = Util::getJSONChild("_$owner", json); if (ownerJson!=NULL) { jsonDocumnet->setOwner(Util::getJSONString("owner", ownerJson)); cJSON_DeleteItemFromObject(json,"_$owner"); } string createdAt = Util::getJSONString("_$createdAt", json); if (createdAt.c_str()!=NULL) { jsonDocumnet->setCreatedAt(createdAt); cJSON_DeleteItemFromObject(json, "_$createdAt"); } string updatedAt = Util::getJSONString("_$updatedAt", json); if (updatedAt.c_str()!=NULL) { jsonDocumnet->setUpdatedAt(updatedAt); cJSON_DeleteItemFromObject(json, "_$updatedAt"); } string event = Util::getJSONString("_$event", json); if (event.c_str()!=NULL) { jsonDocumnet->setEvent(event); cJSON_DeleteItemFromObject(json, "_$event"); } char *doc = cJSON_PrintUnformatted(json); jsonDocumnet->setJsonDoc(doc); free(doc); }
void WebInterface::generateScreenJson() { cJSON* jScreens = cJSON_CreateArray(); for (int nScreen=0;nScreen<_screenManager->getScreenCount();nScreen++) { // set current screen _screenManager->setCurrentScreen(nScreen); // create screen object and add it cJSON* jScreen = cJSON_CreateObject(); cJSON_AddItemToArray(jScreens,jScreen); cJSON_AddStringToObject(jScreen,"name",_screenManager->getCurrentScreenName().getCharPointer()); // create pages object and add it to screen cJSON* jPages = cJSON_CreateArray(); cJSON_AddItemToObject(jScreen,"pages",jPages); for (int nPage=0;nPage<_screenManager->getPageCount();nPage++) { // set current page _screenManager->setCurrentPage(nPage); // create the page object and add it to pages cJSON* jPage = cJSON_CreateArray(); cJSON_AddItemToObject(jPages,"pages",jPage); for (int nControl=0;nControl<_screenManager->getControlCount();nControl++) { // create the control object and add it to page cJSON* jControl = cJSON_CreateObject(); cJSON_AddStringToObject(jControl,"name",GrooveControl_getNameString(_screenManager->getControl(nControl))); cJSON_AddNumberToObject(jControl,"id",_screenManager->getControl(nControl)); cJSON_AddItemToArray(jPage,jControl); } } } _screenManager->setCurrentScreen(0); _screenJson = cJSON_PrintUnformatted(jScreens); cJSON_Delete(jScreens); }
/*************************************************************************** Function: construct_msg Description: construct IPC message Input: msgtype text_str Output: pmsg Return: negative:error positive:the length of message data, including null Others: none ***************************************************************************/ static int construct_msg(clientAdminMsgType msgtype, const char *text_str, struct client_admin_msgbuf *pmsg) { int re; char *out; cJSON *msg_json; if ( (msgtype < clientAdmintMsgPassword) || (msgtype > clientAdmintMsgAlias) ) { CA_DEBUG("msgtype error %d\n", msgtype); return -1; } if (text_str == NULL) { CA_DEBUG("text_str NULL\n"); return -1; } pmsg->mtype = msgtype; msg_json = cJSON_CreateObject(); if (!msg_json) { CA_DEBUG("Create msg_json failed\n"); return -2; } switch (msgtype) { case clientAdmintMsgPassword: cJSON_AddStringToObject(msg_json, "password", text_str); break; case clientAdmintMsgAlias: cJSON_AddStringToObject(msg_json, "alias", text_str); break; default: // already check, just fo completed cJSON_Delete(msg_json); CA_DEBUG("msgtype error %d\n", msgtype); return -2; } out = cJSON_PrintUnformatted(msg_json); cJSON_Delete(msg_json); if (!out) { CA_DEBUG("cJSON_PrintUnformatted msg_json failed\n"); return -3; } re = snprintf(pmsg->mtext, CLIENTADMIN_MSG_LEN, "%s", out); free(out); return (re+1); //including null }
ssap_message* generateInsertMessageWithQueryType(const char* sessionKey, const char* ontology, const char* data, SSAPQueryType queryType){ ssap_message* insertMessage = allocateSsapMessage(); cJSON *body; body=cJSON_CreateObject(); switch(queryType){ case NATIVE: cJSON_AddItemToObject(body, "queryType", cJSON_CreateString("NATIVE")); cJSON_AddItemToObject(body, "query", cJSON_CreateNull()); cJSON_AddItemToObject(body, "data", cJSON_CreateString(data)); break; case SQLLIKE: cJSON_AddItemToObject(body, "queryType", cJSON_CreateString("SQLLIKE")); cJSON_AddItemToObject(body, "query", cJSON_CreateString(data)); cJSON_AddItemToObject(body, "data", cJSON_CreateNull()); break; case BDH: cJSON_AddItemToObject(body, "queryType", cJSON_CreateString("BDH")); cJSON_AddItemToObject(body, "query", cJSON_CreateString(data)); cJSON_AddItemToObject(body, "data", cJSON_CreateNull()); break; default: cJSON_AddItemToObject(body, "queryType", cJSON_CreateNull()); cJSON_AddItemToObject(body, "query", cJSON_CreateNull()); cJSON_AddItemToObject(body, "data", cJSON_CreateString(data)); break; } insertMessage->body=cJSON_PrintUnformatted(body); insertMessage->direction = REQUEST; insertMessage->messageId = NULL; insertMessage->messageType=INSERT; insertMessage->ontology=(char*) malloc((strlen(ontology)+1)*sizeof(char)); strcpy(insertMessage->ontology, ontology); insertMessage->sessionKey=(char*) malloc((strlen(sessionKey)+1)*sizeof(char)); strcpy(insertMessage->sessionKey, sessionKey); insertMessage->persistenceType=MONGODB; cJSON_Delete(body); return insertMessage; }
string BuildStorageBody(string json) { cJSON *bodyJSON = cJSON_CreateObject(); cJSON *app42JSON = cJSON_CreateObject(); cJSON *storageJSON = cJSON_CreateObject(); cJSON_AddStringToObject(storageJSON, "jsonDoc", json.c_str()); cJSON_AddItemReferenceToObject(app42JSON, "storage", storageJSON); cJSON_AddItemReferenceToObject(bodyJSON, "app42", app42JSON); char *cptrFormatted = cJSON_PrintUnformatted(bodyJSON); string bodyString = cptrFormatted; cJSON_Delete(storageJSON); cJSON_Delete(app42JSON); cJSON_Delete(bodyJSON); free(cptrFormatted); return bodyString; }
void Client::sendPrivateChat(std::string toUser, std::string message) { if((_state != ConnectionState::connected) || (_socket == NULL)){ if(_chatlistener != NULL) { _chatlistener->onSendPrivateChatDone(ResultCode::connection_error); } return; } if(message.length() >= 512) { if(_chatlistener != NULL) _chatlistener->onSendPrivateChatDone(ResultCode::bad_request); return; } std::string payload; int len; cJSON *payloadJSON; payloadJSON = cJSON_CreateObject(); cJSON_AddStringToObject(payloadJSON, "to" ,toUser.c_str()); cJSON_AddStringToObject(payloadJSON, "chat" ,message.c_str()); char *cRet = cJSON_PrintUnformatted(payloadJSON); payload = cRet; byte * req = buildWarpRequest(RequestType::private_chat, payload, len); char *data = new char[len]; for(int i=0; i< len; ++i) { data[i] = req[i]; } _socket->sockSend(data, len); delete[] data; delete[] req; cJSON_Delete(payloadJSON); free(cRet); }
ssap_message* generateJoinMessage(const char* token, const char* instance){ ssap_message* joinMessage = allocateSsapMessage(); cJSON *body; body=cJSON_CreateObject(); cJSON_AddItemToObject(body, "token", cJSON_CreateString(token)); cJSON_AddItemToObject(body, "instance", cJSON_CreateString(instance)); joinMessage->body=cJSON_PrintUnformatted(body); joinMessage->direction = REQUEST; joinMessage->messageId=NULL; joinMessage->messageType=JOIN; joinMessage->ontology=NULL; joinMessage->sessionKey=NULL; joinMessage->persistenceType=MONGODB; cJSON_Delete(body); return joinMessage; }
// 将URL队列 序列化为 json 文本 char * communication::encoding (queue<url *> & urlQueue) { cJSON * root ; root = cJSON_CreateArray(); while(!urlQueue.empty()) { url * u = urlQueue.front(); cJSON * item = cJSON_CreateObject(); cJSON_AddStringToObject(item,"HOST",u->getHost()); cJSON_AddStringToObject(item,"FILE",u->getFile()); cJSON_AddNumberToObject(item,"PORT",u->getPort()); cJSON_AddNumberToObject(item,"PRIORITY",u->getPriority()); cJSON_AddNumberToObject(item,"DEPTH",u->getDepth()); cJSON_AddItemToArray(root ,item); urlQueue.pop(); } char * out = cJSON_PrintUnformatted(root); cJSON_Delete(root); return out; }
string AlprImpl::toJson(const vector< AlprResult > results) { cJSON *root = cJSON_CreateArray(); for (int i = 0; i < results.size(); i++) { cJSON *resultObj = createJsonObj( &results[i] ); cJSON_AddItemToArray(root, resultObj); } // Print the JSON object to a string and return char *out; out=cJSON_PrintUnformatted(root); cJSON_Delete(root); string response(out); free(out); return response; }
int ont_video_dev_set_channels(void *_dev, int channels) { ont_device_t *dev = _dev; char dsname[32]; int i = 0; cJSON *json = cJSON_CreateNull(); cJSON_AddItemToObject(json, "null", cJSON_CreateString("null")); char *jsonValue = cJSON_PrintUnformatted(json); for (i = 0; i < channels; i++) { ont_platform_snprintf(dsname, sizeof(dsname), "ont_video_%d_mqtttestvideo", i + 1); ont_device_add_dp_object(dev, dsname, jsonValue); } ont_device_send_dp(dev); ont_platform_free(jsonValue); cJSON_Delete(json); return 0; }
/*************************************************************************** Function: package_json_callback Description: 发到5017端口的callback数据格式 Input: Output: 输出封装好的callback数据的地址 Return: Others: ****************************************************************************/ char *package_json_callback(int type,char *pic_name) { cJSON *root,*fmt; char *out; //Our "Video" datatype: root=cJSON_CreateObject(); cJSON_AddNumberToObject(root,"msgtype",0); cJSON_AddNumberToObject(root,"mainid",2); cJSON_AddNumberToObject(root,"subid",6); cJSON_AddNumberToObject(root,"status",type ); if(type == 0) { cJSON_AddStringToObject(root,"ipaddr",pic_name); } out=cJSON_PrintUnformatted(root); cJSON_Delete(root); return out; }
static void make_poll_post_data(str_t* post_data) { cJSON* cjson_poll_post = cJSON_CreateObject(); str_t tmp = empty_str; cJSON_AddStringToObject(cjson_poll_post, "clientid", CLIENTID); cJSON_AddStringToObject(cjson_poll_post, "psessionid", robot.session.ptr); cJSON_AddNumberToObject(cjson_poll_post, "key", 0); cJSON_AddItemToObject(cjson_poll_post, "ids", cJSON_CreateArray()); post_data->ptr = cJSON_PrintUnformatted(cjson_poll_post); post_data->len = strlen(post_data->ptr); cJSON_Delete(cjson_poll_post); str_cpy(&tmp, str_from("r=")); str_ncat(&tmp, post_data->ptr, post_data->len); str_cat(&tmp, "&clientid="CLIENTID"&psessionid="); str_ncat(&tmp, robot.session.ptr, robot.session.len); str_free(*post_data); urlencode(tmp, post_data); str_free(tmp); }
char *rm_item(char *old,char *id) { cJSON *root; char *out; if(old!=NULL) root=cJSON_Parse(old); else root=cJSON_CreateObject(); cJSON *data; data=cJSON_GetObjectItem(root,id); if(data) { cJSON_DeleteItemFromObject(root, id); } out=cJSON_PrintUnformatted(root); cJSON_Delete(root); if(old) free(old); return out; }
ssap_message* generateUpdateMessageWithQueryType(const char* sessionKey, const char* ontology, const char* query, SSAPQueryType queryType){ ssap_message* updateMessage = allocateSsapMessage(); cJSON *body; //Crea la raiz del JSON body=cJSON_CreateObject(); //Añade las propiedades al body cJSON_AddItemToObject(body, "data", cJSON_CreateNull()); cJSON_AddItemToObject(body, "query", cJSON_CreateString(query)); switch(queryType){ case NATIVE: cJSON_AddItemToObject(body, "queryType", cJSON_CreateString("NATIVE")); break; case SQLLIKE: cJSON_AddItemToObject(body, "queryType", cJSON_CreateString("SQLLIKE")); break; case BDH: cJSON_AddItemToObject(body, "queryType", cJSON_CreateString("BDH")); break; default: cJSON_AddItemToObject(body, "queryType", cJSON_CreateNull()); break; } updateMessage->body=cJSON_PrintUnformatted(body); updateMessage->direction = REQUEST; updateMessage->messageId = NULL; updateMessage->messageType=UPDATE; updateMessage->ontology=(char*) malloc((strlen(ontology)+1)*sizeof(char)); strcpy(updateMessage->ontology, ontology); updateMessage->sessionKey=(char*) malloc((strlen(sessionKey)+1)*sizeof(char)); strcpy(updateMessage->sessionKey, sessionKey); updateMessage->persistenceType=MONGODB; cJSON_Delete(body); return updateMessage; }
st NetUtil::login(const char* host, const char* user, const char* pwd) { cJSON* root = cJSON_CreateObject(); cJSON_AddItemToObject(root, "username", cJSON_CreateString(user)); cJSON_AddItemToObject(root, "password", cJSON_CreateString(pwd)); char* str = cJSON_PrintUnformatted(root); cl::StringUtil::copy(g_host, 256, host); cJSON* ret = do_login("/v2/logon", str); cJSON_Delete(root); free(str); if(ret == NULL) return FALSE; cJSON* tokenItem = cJSON_GetObjectItem(ret, "token"); cl::StringUtil::copy(g_token, 64, tokenItem->valuestring); cJSON_Delete(ret); return TRUE; }
// Acoustic Beacon Task Json Object Creator Function char *acousticObject(int buoyColor, float latitude, float longitude){ cJSON *acousticItem, *positions; // JSON Object for Acoustic Task char *outputString; acousticItem = cJSON_CreateObject(); cJSON_AddStringToObject(acousticItem, "course", "courseA"); // MAKE SURE TO REVISE THIS cJSON_AddStringToObject(acousticItem, "team", "VU"); // MAKE SURE TO REVISE THIS switch(buoyColor){ cJSON_AddStringToObject(acousticItem, "buoyColor", color1); break; // Green case 3: cJSON_AddStringToObject(acousticItem, "buoyColor", color3); break; // Blue case 4: cJSON_AddStringToObject(acousticItem, "buoyColor", color4); break; // Yellow case 5: cJSON_AddStringToObject(acousticItem, "buoyColor", color5); break; // Black case 6: cJSON_AddStringToObject(acousticItem, "buoyColor", color6); break; // Guess Blue default: cJSON_AddStringToObject(acousticItem, "buoyColor", color4); break; } cJSON_AddItemToObject(acousticItem, "buoyPosition", positions=cJSON_CreateObject()); cJSON_AddStringToObject(positions, "datum", "WGS84"); cJSON_AddNumberToObject(positions, "latitude", latitude); cJSON_AddNumberToObject(positions, "longitude", longitude); outputString = cJSON_PrintUnformatted(acousticItem); cJSON_Delete(acousticItem); printf("acousticObjectCreated:%s\n",outputString); return outputString; }
Eina_Bool azy_content_serialize_request_json(Azy_Content *content) { Eina_List *l; Eina_Value *v; cJSON *object, *params; char *msg; if ((!content) || (content->buffer)) return EINA_FALSE; object = cJSON_CreateObject(); cJSON_AddStringToObject(object, "method", content->method); if (content->params) { params = cJSON_CreateArray(); EINA_LIST_FOREACH(content->params, l, v) cJSON_AddItemToArray(params, azy_value_serialize_json(v)); cJSON_AddItemToObject(object, "params", params); } cJSON_AddNumberToObject(object, "id", content->id); 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; }
// Lua -> JSON static int _encode(lua_State *L) { //app_t *app = lua_touserdata(L, lua_upvalueindex(1)); if(lua_istable(L, 1) && !lua_objlen(L, 1)) { cJSON *root = _encode_object(L, 1); char *json = cJSON_PrintUnformatted(root); lua_pushnil(L); // no error lua_pushstring(L, json); free(json); cJSON_Delete(root); } else { lua_pushstring(L, "not an object table"); lua_pushnil(L); } return 2; }
int Parser_swm_get_smi_rep(char *pCommData, char **outputStr) { sys_mon_info_t * pSysMonInfo = (sys_mon_info_t *)pCommData; char * out = NULL; int outLen = 0; cJSON *pSUSICommDataItem = NULL; if(pCommData == NULL || outputStr == NULL) return outLen; pSUSICommDataItem = cJSON_CreateObject(); cJSON_AddSysMonInfoToObject(pSUSICommDataItem, SWM_SYS_MON_INFO, pSysMonInfo); out = cJSON_PrintUnformatted(pSUSICommDataItem); outLen = strlen(out) + 1; *outputStr = (char *)(malloc(outLen)); memset(*outputStr, 0, outLen); strcpy(*outputStr, out); cJSON_Delete(pSUSICommDataItem); printf("%s\n",out); free(out); return outLen; }
int sg_json_doc_to_file(sg_json_doc_t *doc, const char *filename) { cJSON *json = cast_to_cJSON(doc); if (json == NULL) return -1; FILE *f = fopen(filename, "wb"); if (f == NULL) return -1; char *str = cJSON_PrintUnformatted(json); if (str == NULL) { fclose(f); return -1; } const int len = strlen(str); int j = fwrite(str, 1, len, f); free(str); fclose(f); return j == len ? 0 : -1; }
// Success Checker Json Object Parsing Function // 0 = False (Fail), 1 = True (Success), -1 = ERROR int validParse(char *str){ if(str == NULL){ printf("Error: Passed NULL as Input Argument\n"); return -1; } else{ cJSON *tmp = cJSON_Parse(str); char *outputString; int answer = 0; if(!tmp){ printf("Error before: [%s]\n",cJSON_GetErrorPtr()); return -1; } else{ outputString = cJSON_PrintUnformatted(tmp); answer = cJSON_GetObjectItem(tmp,"success")->valueint; //printf("validParse return: %d\n",answer); cJSON_Delete(tmp); return answer; } } }
const char* App42Service::getJsonStringFromVector(vector<string>array) { cJSON *userJSON = cJSON_CreateArray(); std::vector<string>::iterator it; for(it=array.begin(); it!=array.end(); ++it) { cJSON *jsonString = cJSON_CreateString(it->c_str()); cJSON_AddItemToArray(userJSON, jsonString); //cJSON_Delete(jsonString); } char *cptrFormatted = cJSON_PrintUnformatted(userJSON); string bodyString = cptrFormatted; cJSON_Delete(userJSON); free(cptrFormatted); return bodyString.c_str(); }
string App42Service::getJsonStringFromAclList(string key,vector<ACL> &aclObjectArray) { cJSON *bodyJSON = cJSON_CreateObject(); cJSON *jsonArray = cJSON_CreateArray(); for (vector<ACL>::iterator it = aclObjectArray.begin(); it!= aclObjectArray.end(); it++) { cJSON *jsonObject = cJSON_CreateObject(); cJSON_AddStringToObject(jsonObject, "user", it->getUserName().c_str()); cJSON_AddStringToObject(jsonObject, "permission", it->getPermission().c_str()); cJSON_AddItemToArray(jsonArray, jsonObject); //cJSON_Delete(jsonObject); } cJSON_AddItemReferenceToObject(bodyJSON, key.c_str(), jsonArray); char *cptrFormatted = cJSON_PrintUnformatted(bodyJSON); string jsonString = cptrFormatted; free(cptrFormatted); cJSON_Delete(jsonArray); cJSON_Delete(bodyJSON); return jsonString; }
void Client::joinRoomInUserRange(int minJoinedUsers, int maxJoinedUsers, bool maxPreferred) { if((_state != ConnectionState::connected) || (_socket == NULL)){ if(_roomlistener != NULL) { room _room; _room.result = ResultCode::connection_error; _roomlistener->onJoinRoomDone(_room); } return; } int byteLen; byte *req; std::string payload; cJSON *payloadJSON; payloadJSON = cJSON_CreateObject(); cJSON_AddNumberToObject(payloadJSON, "minUsers", minJoinedUsers); cJSON_AddNumberToObject(payloadJSON, "maxUsers", maxJoinedUsers); maxPreferred ? cJSON_AddTrueToObject(payloadJSON, "maxPreferred") : cJSON_AddFalseToObject(payloadJSON, "maxPreferred"); char *cRet = cJSON_PrintUnformatted(payloadJSON); payload = cRet; req = buildWarpRequest(RequestType::join_room_range, payload, byteLen); char *data = new char[byteLen]; for(int i=0; i< byteLen; ++i) { data[i] = req[i]; } _socket->sockSend(data, byteLen); delete[] data; delete[] req; cJSON_Delete(payloadJSON); free(cRet); }