static const char* processor_setup_page(processor_state_t *self, json_object *request) { json_object *page_obj = NULL; if (json_object_object_get_ex(request, "action", &page_obj)) { json_object_get(page_obj); json_object_object_del(request, "action"); } else if (json_object_object_get_ex(request, "logjam_action", &page_obj)) { json_object_get(page_obj); json_object_object_del(request, "logjam_action"); } else { page_obj = json_object_new_string("Unknown#unknown_method"); } const char *page_str = json_object_get_string(page_obj); if (!strchr(page_str, '#')) page_str = append_to_json_string(&page_obj, page_str, "#unknown_method"); else if (page_str[strlen(page_str)-1] == '#') page_str = append_to_json_string(&page_obj, page_str, "unknown_method"); json_object_object_add(request, "page", page_obj); return page_str; }
void SetValByKey(json_object * jobj, char *sname, char *key) { json_object *pval = jobj; char *p, *strs; strs = (char *) malloc(strlen(sname) + 1); strcpy(strs, sname); int couts = calu_append_num(sname, "->"); char *bak; bak = sname; if (couts != 0) { pval = json_object_object_get(jobj, strtok(strs, "->")); couts--; while ((p = strtok(NULL, "->"))) { free(bak); if (couts == 0) { bak = (char *) malloc(strlen(p) + 1); strcpy(bak, p); bak[strlen(p)] = '\0'; couts--; } else { pval = json_object_object_get(pval, p); } } } json_object_object_del(pval, bak); json_object_object_add(pval, bak, json_object_new_string(key)); free(strs); }
static int http_response_process(void * arg) { struct json_object *resp; if(ghttp_process(request)==ghttp_done) { resp=json_tokener_parse(ghttp_get_body(request)); resp_ret.type=json_object_get_int(json_object_object_get(resp,RESP_TYPE)); resp_ret.result=json_object_get_int(json_object_object_get(resp,RESP_RESULT)); if(resp_ret.type==REQ_REGISTER) { json_object_object_del(resp, RESP_TYPE); json_object_to_file((char *)arg,resp); } else { if((resp_ret.type==REQ_SUBMIT)&&(!resp_ret.result)) { sprintf(qrcode_buf,format_qrcode,machine_id,*p_order_id,info_version); pid_t pid=vfork(); if(!pid) execlp("qrencode","qrencode","-s 6","-m 2","-lH","-oqrcode.png",qrcode_buf,0); else wait(NULL); } } json_object_put(resp); return 0; } else return -1; }
int main(int argc, char **argv) { MC_SET_DEBUG(1); /* * Check that replacing an existing object keeps the key valid, * and that it keeps the order the same. */ json_object *my_object = json_object_new_object(); json_object_object_add(my_object, "foo1", json_object_new_string("bar1")); json_object_object_add(my_object, "foo2", json_object_new_string("bar2")); json_object_object_add(my_object, "deleteme", json_object_new_string("bar2")); json_object_object_add(my_object, "foo3", json_object_new_string("bar3")); printf("==== delete-in-loop test starting ====\n"); int orig_count = 0; json_object_object_foreach(my_object, key0, val0) { printf("Key at index %d is [%s]", orig_count, key0); if (strcmp(key0, "deleteme") == 0) { json_object_object_del(my_object, key0); printf(" (deleted)\n"); } else printf(" (kept)\n"); orig_count++; }
int test_jsonc() { json_tokener *tok; json_object *my_string, *my_int, *my_object, *my_array; json_object *new_obj; int i; MC_SET_DEBUG(1); my_string = json_object_new_string("\t"); printf("my_string=%s\n", json_object_get_string(my_string)); printf("my_string.to_string()=%s\n", json_object_to_json_string(my_string)); json_object_put(my_string); my_string = json_object_new_string("\\"); printf("my_string=%s\n", json_object_get_string(my_string)); printf("my_string.to_string()=%s\n", json_object_to_json_string(my_string)); json_object_put(my_string); my_string = json_object_new_string("foo"); printf("my_string=%s\n", json_object_get_string(my_string)); printf("my_string.to_string()=%s\n", json_object_to_json_string(my_string)); my_int = json_object_new_int(9); printf("my_int=%d\n", json_object_get_int(my_int)); printf("my_int.to_string()=%s\n", json_object_to_json_string(my_int)); my_array = json_object_new_array(); json_object_array_add(my_array, json_object_new_int(1)); json_object_array_add(my_array, json_object_new_int(2)); json_object_array_add(my_array, json_object_new_int(3)); json_object_array_put_idx(my_array, 4, json_object_new_int(5)); printf("my_array=\n"); for(i=0; i < json_object_array_length(my_array); i++) { json_object *obj = json_object_array_get_idx(my_array, i); printf("\t[%d]=%s\n", i, json_object_to_json_string(obj)); } printf("my_array.to_string()=%s\n", json_object_to_json_string(my_array)); my_object = json_object_new_object(); json_object_object_add(my_object, "abc", json_object_new_int(12)); json_object_object_add(my_object, "foo", json_object_new_string("bar")); json_object_object_add(my_object, "bool0", json_object_new_boolean(0)); json_object_object_add(my_object, "bool1", json_object_new_boolean(1)); json_object_object_add(my_object, "baz", json_object_new_string("bang")); json_object_object_add(my_object, "baz", json_object_new_string("fark")); json_object_object_del(my_object, "baz"); /*json_object_object_add(my_object, "arr", my_array);*/ printf("my_object=\n"); json_object_object_foreach(my_object, key, val) { printf("\t%s: %s\n", key, json_object_to_json_string(val)); }
int bridge_request_send_response(bridge_request_t *self, struct json_object *error, struct json_object *result) { json_object_object_add(self->response, "id", json_object_new_int(self->id)); if(error) json_object_object_add(self->response, "error", json_object_get(error)); if(result) json_object_object_add(self->response, "result", json_object_get(result)); bridge_request_transmit(self, self->response); if(error) json_object_object_del(self->response, "error"); if(result) json_object_object_del(self->response, "result"); return 0; }
/** * Delete json object by key. * @param osName Key name. * * @since GDAL 2.3 */ void CPLJSONObject::Delete(const std::string &osName) { std::string objectName; CPLJSONObject object = GetObjectByPath( osName, objectName ); if( object.IsValid() ) { json_object_object_del( TO_JSONOBJ(object.GetInternalHandle()), objectName.c_str() ); } }
static int json_check_accum(noit_check_t *check, void *closure) { struct json_object *cobj, *doc = closure; char id_str[UUID_STR_LEN+1]; uuid_unparse_lower(check->checkid, id_str); cobj = noit_check_state_as_json(check, 0); json_object_object_del(cobj, "id"); json_object_object_add(doc, id_str, cobj); return 1; }
/* * delete fields from a json message */ void delete_fields(char **fields, int len, struct json_object *msg) { const char *field_key; int i; for (i = 0; i < len; i++) { field_key = fields[i]; json_object_object_del(msg, field_key); } }
void ipc_get_workspaces_callback(swayc_t *workspace, void *data) { if (workspace->type == C_WORKSPACE) { json_object *workspace_json = ipc_json_describe_container(workspace); // override the default focused indicator because // it's set differently for the get_workspaces reply bool focused = root_container.focused == workspace->parent && workspace->parent->focused == workspace; json_object_object_del(workspace_json, "focused"); json_object_object_add(workspace_json, "focused", json_object_new_boolean(focused)); json_object_array_add((json_object *)data, workspace_json); } }
json_object* PackageDescription::toJSON() const { json_object* json = NULL; if (m_isOldStyle) { json = json_object_new_object(); json_object_object_add(json, (char*) "id", json_object_new_string((char*) m_id.c_str())); json_object_object_add(json, (char*) "version", json_object_new_string(m_version.c_str())); json_object_object_add(json, (char*) "size", json_object_new_int((int)m_packageSize)); json_object* apps = json_object_new_array(); std::vector<std::string>::const_iterator appIdIt, appIdItEnd; for (appIdIt = m_appIds.begin(), appIdItEnd = m_appIds.end(); appIdIt != appIdItEnd; ++appIdIt) { json_object_array_add(apps, json_object_new_string((*appIdIt).c_str())); } json_object_object_add(json, (char*) "apps", apps); } else { json = json_tokener_parse(m_jsonString.c_str()); if (!json || is_error(json)) { g_warning("%s: Failed to parse '%s' into a JSON string", __PRETTY_FUNCTION__, m_jsonString.c_str()); return NULL; } json_object_object_add(json, (char*) "size", json_object_new_int((int)m_packageSize)); json_object* label = JsonGetObject(json, "icon"); if (label) { std::string icon = json_object_get_string(label); json_object_object_del(json, (char*) "icon"); json_object_object_add(json, (char*) "icon", json_object_new_string((char*) (m_folderPath + "/" + icon).c_str())); } label = JsonGetObject(json, "miniicon"); if (label) { std::string miniicon = json_object_get_string(label); json_object_object_del(json, (char*) "miniicon"); json_object_object_add(json, (char*) "miniicon", json_object_new_string((char*) (m_folderPath + "/" + miniicon).c_str())); } } return json; }
static json_object* processor_setup_exceptions(processor_state_t *self, json_object *request) { json_object* exceptions; if (json_object_object_get_ex(request, "exceptions", &exceptions)) { int num_ex = json_object_array_length(exceptions); if (num_ex == 0) { json_object_object_del(request, "exceptions"); return NULL; } } return exceptions; }
static int processor_setup_response_code(processor_state_t *self, json_object *request) { json_object *code_obj = NULL; int response_code = 500; if (json_object_object_get_ex(request, "code", &code_obj)) { response_code = json_object_get_int(code_obj); json_object_object_del(request, "code"); } json_object_object_add(request, "response_code", json_object_new_int(response_code)); // printf("[D] response_code: %d\n", response_code); return response_code; }
/* deleting an object DOES decrement reference count */ int cli_json_delowner(json_object *owner, const char *key, int idx) { json_type objty; json_object *obj; if (NULL == owner) { cli_dbgmsg("json: no owner object specified to cli_json_delowner\n"); return CL_ENULLARG; } objty = json_object_get_type(owner); if (objty == json_type_object) { if (NULL == key) { cli_dbgmsg("json: null string specified as key to cli_delowner\n"); return CL_ENULLARG; } if (!json_object_object_get_ex(owner, key, &obj)) { cli_dbgmsg("json: owner array does not have content with key %s\n", key); return CL_EARG; } json_object_object_del(owner, key); } else if (objty == json_type_array) { json_object *empty; if (NULL == json_object_array_get_idx(owner, idx)) { cli_dbgmsg("json: owner array does not have content at idx %d\n", idx); return CL_EARG; } /* allocate the empty object to replace target object */ empty = cli_jsonobj(NULL, NULL); if (NULL == empty) return CL_EMEM; if (0 != json_object_array_put_idx(owner, idx, empty)) { /* this shouldn't be possible */ cli_dbgmsg("json: cannot delete idx %d of owner array\n", idx); return CL_BREAK; } } else { cli_dbgmsg("json: no owner object cannot hold ownership\n"); return CL_EARG; } return CL_SUCCESS; }
int main(int argc, char **argv) { struct json_object *my_string, *my_int, *my_object, *my_array; struct json_object *new_obj; int i; my_string = json_object_new_string("\t"); printf("my_string=%s\n", json_object_get_string(my_string)); printf("my_string.to_string()=%s\n", json_object_to_json_string(my_string)); json_object_put(my_string); my_string = json_object_new_string("foo"); printf("my_string=%s\n", json_object_get_string(my_string)); printf("my_string.to_string()=%s\n", json_object_to_json_string(my_string)); my_int = json_object_new_int(9); printf("my_int=%d\n", json_object_get_int(my_int)); printf("my_int.to_string()=%s\n", json_object_to_json_string(my_int)); my_array = json_object_new_array(); json_object_array_add(my_array, json_object_new_int(1)); json_object_array_add(my_array, json_object_new_int(2)); json_object_array_add(my_array, json_object_new_int(3)); json_object_array_put_idx(my_array, 4, json_object_new_int(5)); printf("my_array=\n"); for(i=0; i < json_object_array_length(my_array); i++) { struct json_object *obj = json_object_array_get_idx(my_array, i); printf("\t[%d]=%s\n", i, json_object_to_json_string(obj)); } printf("my_array.to_string()=%s\n", json_object_to_json_string(my_array)); my_object = json_object_new_object(); json_object_object_add(my_object, "abc", json_object_new_int(12)); json_object_object_add(my_object, "foo", json_object_new_string("bar")); json_object_object_add(my_object, "bool0", json_object_new_boolean(0)); json_object_object_add(my_object, "bool1", json_object_new_boolean(1)); json_object_object_add(my_object, "baz", json_object_new_string("bang")); json_object_object_add(my_object, "baz", json_object_new_string("fark")); json_object_object_del(my_object, "baz"); json_object_object_add(my_object, "arr", my_array); printf("my_object=\n"); json_object_object_foreach(my_object, key, val) { printf("\t%s: %s\n", key, json_object_to_json_string(val)); }
//Make sure data is sanatized static void dirserver_newMCData(const char *data, GSocketAddress *src) { struct json_object *json_tmp; struct json_object *json = json_tokener_parse(data); ub_assert(json != NULL); if( is_error(json) ){ syslog(LOG_DEBUG,"dirserver_newMCData: invalid json"); return; } json_tmp = json_object_object_get(json, "cmd"); if( json_tmp == NULL || is_error(json_tmp) ){ syslog(LOG_DEBUG,"dirserver_newMCData: no cmd field in json"); return; } if( json_object_get_type(json_tmp) != json_type_string ){ syslog(LOG_DEBUG,"dirserver_newMCData: no cmd field is not a string"); return; } enum commandlist cmd = dirserver_parseCommand( json_object_get_string(json_tmp)); json_object_object_del(json, "cmd"); switch( cmd ){ case DISCOVER_DIRECTORY: dirserver_announce(dirserver_getJsonString(json, "service-type",NULL), dirserver_getJsonString(json, "protocol",NULL), dirserver_getJsonBool(json, "local-only",FALSE), src); break; case UPDATE_SERVICE: dirserver_updateServiceCmd(json); break; case DELETE_SERVICE: dirserver_deleteServiceCmd(json); case NO_COMMAND: break; }; json_object_put(json); }
int pv_add_json ( pv_param_t* pvp, json_t * obj ) { json_t *dest; json_name * id; pv_json_t * var; json_tag * tag; int poz; id = (json_name *) pvp->pvn.u.dname; var = get_pv_json(pvp); if( var == NULL ) { if( id->tags ) { LM_ERR("Object is not initialized yet\n"); return -1; } var = (pv_json_t *) pkg_malloc(sizeof(pv_json_t)); if( var == NULL ) { LM_ERR("Out of memory\n"); return -1; } memset(var,0,sizeof(pv_json_t)); var->name = id->name; var->next = all; var->data = obj; all = var; return 0; } if( id ->tags == NULL) { if( var->data ) json_object_put(var->data); var->data = obj; return 0; } dest = get_object(var, pvp, &tag, 1, 1); if( dest == NULL ) { LM_NOTICE("Could not find object with that path\n"); return -1; } if( tag->type & TAG_KEY ) { memcpy(buff,tag->key.s,tag->key.len); buff[tag->key.len] = 0; if( obj == NULL ) json_object_object_del(dest,buff); else json_object_object_add(dest,buff,obj); } if( tag->type & TAG_IDX ) { poz = tag->idx; if( tag->type & TAG_END ) { if( obj == NULL) { LM_ERR("Invalid parameter for deletion\n"); return -1; } json_object_array_add(dest,obj); return 0; } if( poz < 0 ) poz += json_object_array_length(dest); if( poz<0 || poz >= json_object_array_length(dest)) { LM_ERR("Attempting to replace at invalid index in array:%d\n", poz); return -1; } if( obj == NULL) { if( poz >= json_object_array_length(dest)) { LM_ERR("Index out of bounds for deletion\n"); return -1; } json_object_array_del(dest,poz); } else json_object_array_put_idx(dest,poz,obj); } return 0; }
void axis2_json_write_node(json_object* parent, axiom_node_t* om_node, const axutil_env_t* env) { axiom_element_t* elem; axiom_node_t* child_node; const axis2_char_t* local_name; json_object* obj; json_object* array = NULL; if (!om_node || axiom_node_get_node_type(om_node, env) != AXIOM_ELEMENT) return; elem = (axiom_element_t*)axiom_node_get_data_element(om_node, env); local_name = axiom_element_get_localname(elem, env); child_node = axiom_node_get_first_element(om_node, env); /* find existing object */ if (json_object_object_get_ex(parent, local_name, &obj)) { /* check that object is array? */ if (!json_object_is_type(obj, json_type_array)) { /* convert to array */ obj = json_object_get(obj); array = json_object_new_array(); json_object_array_add(array, obj); json_object_object_del(parent, local_name); json_object_object_add(parent, local_name, array); } else array = obj; } if (!child_node) { /* this is a leaf node */ json_object* json_value = NULL; /* check for nillable */ if (!axis2_json_element_is_nil(elem, env)) { const axis2_char_t* value = axiom_element_get_text(elem, env, om_node); json_value = json_object_new_string(value ? value : ""); } if (array) json_object_array_add(array, json_value); else json_object_object_add(parent, local_name, json_value); return; } /* iterate through children elements */ obj = json_object_new_object(); if (array) json_object_array_add(array, obj); else json_object_object_add(parent, local_name, obj); for (; child_node; child_node = axiom_node_get_next_sibling(child_node, env)) if (axiom_node_get_node_type(child_node, env) == AXIOM_ELEMENT) axis2_json_write_node(obj, child_node, env); }
dpl_status_t dpl_posix_stream_put(dpl_ctx_t *ctx, dpl_stream_t *stream, char *buf, unsigned int len, struct json_object **statusp) { dpl_status_t ret = DPL_FAILURE; int iret; struct json_object *offset_object = NULL; unsigned int cur_off = 0; char path[MAXPATHLEN]; int fd = -1; int written = 0; DPL_TRACE(ctx, DPL_TRACE_BACKEND, "ctx=%p stream=%p buf=%p len=%u", ctx, stream, buf, len); if (stream->locator_is_id) { ret = DPL_ENOTSUPP; goto end; } iret = snprintf(path, sizeof (path), "/%s/%s", ctx->base_path ? ctx->base_path : "", stream->locator); if (iret > sizeof(path)) { ret = DPL_ENAMETOOLONG; goto end; } if (NULL == stream->status) { offset_object = json_object_new_int64(0); if (NULL == offset_object) { ret = DPL_ENOMEM; goto end; } stream->status = json_object_new_object(); if (NULL == stream->status) { json_object_put(offset_object); ret = DPL_ENOMEM; goto end; } json_object_object_add(stream->status, "offset", offset_object); } else { if (json_object_object_get_ex(stream->status, "offset", &offset_object) == FALSE) { ret = DPL_FAILURE; goto end; } } cur_off = json_object_get_int64(offset_object); fd = open(path, O_CREAT|O_TRUNC|O_WRONLY, 0600); if (-1 == fd) { ret = dpl_posix_map_errno(); perror("open"); goto end; } written = pwrite(fd, buf, len, cur_off); if (written < len) { ret = dpl_posix_map_errno(); perror("pwrite"); goto end; } offset_object = json_object_new_int64(cur_off + written); if (NULL == offset_object) { ret = DPL_ENOMEM; goto end; } json_object_object_del(stream->status, "offset"); json_object_object_add(stream->status, "offset", offset_object); // Grab status object for the caller and return it. if (statusp) { *statusp = stream->status; json_object_get(*statusp); } ret = DPL_SUCCESS; end: if (-1 != fd) close(fd); DPL_TRACE(ctx, DPL_TRACE_BACKEND, "ret=%d", ret); return ret; }
dpl_status_t dpl_posix_stream_get(dpl_ctx_t *ctx, dpl_stream_t *stream, unsigned int len, char **bufp, unsigned int *lenp, struct json_object **statusp) { dpl_status_t ret = DPL_FAILURE; int iret; struct json_object *offset_object = NULL; unsigned int cur_off = 0; char path[MAXPATHLEN]; char *buf = NULL; int fd = -1; int rd = 0; DPL_TRACE(ctx, DPL_TRACE_BACKEND, "ctx=%p stream=%p len=%u", ctx, stream, len); if (stream->locator_is_id) { ret = DPL_ENOTSUPP; goto end; } iret = snprintf(path, sizeof (path), "/%s/%s", ctx->base_path ? ctx->base_path : "", stream->locator); if (iret > sizeof(path)) { ret = DPL_ENAMETOOLONG; goto end; } buf = malloc(len); if (NULL == buf) { ret = DPL_ENOMEM; goto end; } if (NULL == stream->status) { offset_object = json_object_new_int64(0); if (NULL == offset_object) { ret = DPL_ENOMEM; goto end; } stream->status = json_object_new_object(); if (NULL == stream->status) { json_object_put(offset_object); ret = DPL_ENOMEM; goto end; } json_object_object_add(stream->status, "offset", offset_object); } else { if (json_object_object_get_ex(stream->status, "offset", &offset_object) == FALSE) { ret = DPL_FAILURE; goto end; } } cur_off = json_object_get_int64(offset_object); fd = open(path, O_RDONLY); if (-1 == fd) { ret = dpl_posix_map_errno(); perror("open"); goto end; } rd = pread(fd, buf, len, cur_off); if (rd < 0) { ret = dpl_posix_map_errno(); perror("pread"); goto end; } offset_object = json_object_new_int64(cur_off + rd); if (NULL == offset_object) { ret = DPL_ENOMEM; goto end; } json_object_object_del(stream->status, "offset"); json_object_object_add(stream->status, "offset", offset_object); // Grab status object for the caller and return it. if (statusp) { *statusp = stream->status; json_object_get(*statusp); } if (lenp) *lenp = rd; if (bufp) { *bufp = buf; buf = NULL; } ret = DPL_SUCCESS; end: if (-1 != fd) close(fd); if (NULL != buf) free(buf); DPL_TRACE(ctx, DPL_TRACE_BACKEND, "ret=%d", ret); return ret; }
// handle cloud msg here, for example: send to USART or echo to cloud OSStatus mico_fogcloud_msg_dispatch(mico_Context_t* context, struct mico_service_t service_table[], mico_fogcloud_msg_t *cloud_msg, int* ret_status) { msg_dispatch_log_trace(); OSStatus err = kNoErr; char* recv_sub_topic_ptr = NULL; int recv_sub_topic_len = 0; json_object *recv_json_object = NULL; char* response_sub_topic = NULL; json_object *response_json_obj = NULL; const char *response_json_string = NULL; json_object *response_services_obj = NULL; json_object *response_prop_obj = NULL; json_object *response_err_obj = NULL; *ret_status = MSG_PROP_UNPROCESSED; if((NULL == context) || (NULL == cloud_msg->topic) || (0 == cloud_msg->topic_len) ) { return kParamErr; } // strip "<device_id>/in" to get response sub-topic, then response to: "<device_id>/out/<sub-topic>" recv_sub_topic_len = (int)cloud_msg->topic_len - (strlen(context->flashContentInRam.appConfig.fogcloudConfig.deviceId) + strlen(FOGCLOUD_MSG_TOPIC_IN)); if(recv_sub_topic_len <= 0){ // unsupported topic msg_dispatch_log("ERROR: Message from unsupported topic: %.*s \t data[%d]: %s, ignored.", cloud_msg->topic_len, cloud_msg->topic, cloud_msg->data_len, cloud_msg->data); err = kUnsupportedErr; goto exit; } recv_sub_topic_ptr = (char*)(cloud_msg->topic) + strlen(context->flashContentInRam.appConfig.fogcloudConfig.deviceId) + strlen(FOGCLOUD_MSG_TOPIC_IN); response_sub_topic = (char*)malloc(recv_sub_topic_len); // response to where msg come from, remove leading '/' if(NULL == response_sub_topic){ msg_dispatch_log("malloc reponse topic memory err!"); err = kNoMemoryErr; goto exit; } memset(response_sub_topic, '\0', recv_sub_topic_len); strncpy(response_sub_topic, recv_sub_topic_ptr + 1, recv_sub_topic_len-1); // remove leading '/' as send sub-topic //msg_dispatch_log("recv_sub_topic[%d]=[%.*s]", recv_sub_topic_len, recv_sub_topic_len, recv_sub_topic_ptr); //msg_dispatch_log("response_sub_topic[%d]=[%s]", strlen(response_sub_topic), response_sub_topic); // parse sub topic string if( 0 == strncmp((char*)FOGCLOUD_MSG_TOPIC_IN_READ, recv_sub_topic_ptr, strlen((char*)FOGCLOUD_MSG_TOPIC_IN_READ)) ){ // from /read msg_dispatch_log("Recv read cmd: %.*s, data[%d]: %s", recv_sub_topic_len, recv_sub_topic_ptr, cloud_msg->data_len, cloud_msg->data); // parse input json data recv_json_object = json_tokener_parse((const char*)(cloud_msg->data)); if (NULL == recv_json_object){ // input json format error, response to sub-topic "err" with error code response_err_obj = json_object_new_object(); require( response_err_obj, exit ); json_object_object_add(response_err_obj, MICO_PROP_KEY_RESP_STATUS, json_object_new_int(MICO_PROP_CODE_DATA_FORMAT_ERR)); response_json_string = json_object_to_json_string(response_err_obj); err = MicoFogCloudMsgSend(context, FOGCLOUD_MSG_TOPIC_OUT_ERROR, (unsigned char*)response_json_string, strlen(response_json_string)); goto exit; } //msg_dispatch_log("Recv read object=%s", json_object_to_json_string(recv_json_object)); // read properties, return "services/read/err" sub-obj in response_json_obj response_json_obj = mico_read_properties(service_table, recv_json_object); // send reponse for read data if(NULL == response_json_obj){ // read failed msg_dispatch_log("ERROR: read properties error!"); json_object_object_add(response_err_obj, MICO_PROP_KEY_RESP_STATUS, json_object_new_int(MICO_PROP_CODE_READ_FAILED)); response_json_string = json_object_to_json_string(response_err_obj); err = MicoFogCloudMsgSend(context, FOGCLOUD_MSG_TOPIC_OUT_ERROR, (unsigned char*)response_json_string, strlen(response_json_string)); } else { // send err code response_err_obj = json_object_object_get(response_json_obj, MICO_PROP_KEY_RESP_ERROR); if( (NULL != response_err_obj) && (NULL != json_object_get_object(response_err_obj)->head) ){ response_json_string = json_object_to_json_string(response_err_obj); err = MicoFogCloudMsgSend(context, FOGCLOUD_MSG_TOPIC_OUT_ERROR, (unsigned char*)response_json_string, strlen(response_json_string)); } // send services info msg response_services_obj = json_object_object_get(response_json_obj, MICO_PROP_KEY_RESP_SERVICES); if(NULL != response_services_obj){ json_object_object_del(response_json_obj, MICO_PROP_KEY_RESP_ERROR); // remove "err" sub-obj response_json_string = json_object_to_json_string(response_json_obj); err = MicoFogCloudMsgSend(context, response_sub_topic, (unsigned char*)response_json_string, strlen(response_json_string)); } // send read ok value response_prop_obj = json_object_object_get(response_json_obj, MICO_PROP_KEY_RESP_READ); if( (NULL != response_prop_obj) && (NULL != json_object_get_object(response_prop_obj)->head) ){ response_json_string = json_object_to_json_string(response_prop_obj); err = MicoFogCloudMsgSend(context, response_sub_topic, (unsigned char*)response_json_string, strlen(response_json_string)); } // return process result *ret_status = MSG_PROP_READ; } } else if( 0 == strncmp((char*)FOGCLOUD_MSG_TOPIC_IN_WRITE, recv_sub_topic_ptr, strlen((char*)FOGCLOUD_MSG_TOPIC_IN_WRITE)) ){ // from /write msg_dispatch_log("Recv write cmd: %.*s, data[%d]: %s", recv_sub_topic_len, recv_sub_topic_ptr, cloud_msg->data_len, cloud_msg->data); // parse input json data recv_json_object = json_tokener_parse((const char*)(cloud_msg->data)); if (NULL == recv_json_object){ // input json format error, response to sub-topic "err" with error code response_err_obj = json_object_new_object(); require( response_err_obj, exit ); json_object_object_add(response_err_obj, MICO_PROP_KEY_RESP_STATUS, json_object_new_int(MICO_PROP_CODE_DATA_FORMAT_ERR)); response_json_string = json_object_to_json_string(response_err_obj); err = MicoFogCloudMsgSend(context, FOGCLOUD_MSG_TOPIC_OUT_ERROR, (unsigned char*)response_json_string, strlen(response_json_string)); goto exit; } //msg_dispatch_log("Recv write object=%s", json_object_to_json_string(recv_json_object)); // write properties, return "write/err" sub-obj in response_json_obj response_json_obj = mico_write_properties(service_table, recv_json_object); // send reponse for write data if(NULL == response_json_obj){ // write failed msg_dispatch_log("ERROR: write properties error!"); json_object_object_add(response_err_obj, MICO_PROP_KEY_RESP_STATUS, json_object_new_int(MICO_PROP_CODE_WRITE_FAILED)); response_json_string = json_object_to_json_string(response_err_obj); err = MicoFogCloudMsgSend(context, FOGCLOUD_MSG_TOPIC_OUT_ERROR, (unsigned char*)response_json_string, strlen(response_json_string)); } else { // send err code response_err_obj = json_object_object_get(response_json_obj, MICO_PROP_KEY_RESP_ERROR); if( (NULL != response_err_obj) && (NULL != json_object_get_object(response_err_obj)->head) ){ response_json_string = json_object_to_json_string(response_err_obj); err = MicoFogCloudMsgSend(context, FOGCLOUD_MSG_TOPIC_OUT_ERROR, (unsigned char*)response_json_string, strlen(response_json_string)); } // send write ok value response_prop_obj = json_object_object_get(response_json_obj, MICO_PROP_KEY_RESP_WRITE); if( (NULL != response_prop_obj) && (NULL != json_object_get_object(response_prop_obj)->head) ){ response_json_string = json_object_to_json_string(response_prop_obj); err = MicoFogCloudMsgSend(context, response_sub_topic, (unsigned char*)response_json_string, strlen(response_json_string)); } // return process result *ret_status = MSG_PROP_WROTE; } } else{ // unknown topic, ignore msg err = kUnsupportedErr; msg_dispatch_log("ERROR: Message from unknown topic: %.*s \t data[%d]: %s, ignored.", recv_sub_topic_len, cloud_msg->topic, cloud_msg->data_len, cloud_msg->data); } exit: if(NULL != recv_json_object){ json_object_put(recv_json_object); recv_json_object = NULL; } if(NULL != response_json_obj){ json_object_put(response_json_obj); response_json_obj = NULL; } if(NULL != response_sub_topic){ free(response_sub_topic); response_sub_topic = NULL; } return err; }
int main(int argc, char **argv) { json_tokener *tok; json_object *my_string, *my_int, *my_object, *my_array; json_object *new_obj; int i; MC_SET_DEBUG(1); my_string = json_object_new_string("\t"); printf("my_string=%s\n", json_object_get_string(my_string)); printf("my_string.to_string()=%s\n", json_object_to_json_string(my_string)); json_object_put(my_string); my_string = json_object_new_string("\\"); printf("my_string=%s\n", json_object_get_string(my_string)); printf("my_string.to_string()=%s\n", json_object_to_json_string(my_string)); json_object_put(my_string); my_string = json_object_new_string("foo"); printf("my_string=%s\n", json_object_get_string(my_string)); printf("my_string.to_string()=%s\n", json_object_to_json_string(my_string)); my_int = json_object_new_int(9); printf("my_int=%d\n", json_object_get_int(my_int)); printf("my_int.to_string()=%s\n", json_object_to_json_string(my_int)); my_array = json_object_new_array(); json_object_array_add(my_array, json_object_new_int(1)); json_object_array_add(my_array, json_object_new_int(2)); json_object_array_add(my_array, json_object_new_int(3)); json_object_array_put_idx(my_array, 4, json_object_new_int(5)); printf("my_array=\n"); for(i=0; i < json_object_array_length(my_array); i++) { json_object *obj = json_object_array_get_idx(my_array, i); printf("\t[%d]=%s\n", i, json_object_to_json_string(obj)); } printf("my_array.to_string()=%s\n", json_object_to_json_string(my_array)); my_object = json_object_new_object(); json_object_object_add(my_object, "abc", json_object_new_int(12)); json_object_object_add(my_object, "foo", json_object_new_string("bar")); json_object_object_add(my_object, "bool0", json_object_new_boolean(0)); json_object_object_add(my_object, "bool1", json_object_new_boolean(1)); json_object_object_add(my_object, "baz", json_object_new_string("bang")); json_object_object_add(my_object, "baz", json_object_new_string("fark")); json_object_object_del(my_object, "baz"); /*json_object_object_add(my_object, "arr", my_array);*/ /*printf("my_object=\n"); json_object_object_foreach(my_object, key, val) { printf("\t%s: %s\n", key, json_object_to_json_string(val)); } printf("my_object.to_string()=%s\n", json_object_to_json_string(my_object)); */ new_obj = json_tokener_parse("\"\003\""); printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj)); json_object_put(new_obj); new_obj = json_tokener_parse("/* hello */\"foo\""); printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj)); json_object_put(new_obj); new_obj = json_tokener_parse("// hello\n\"foo\""); printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj)); json_object_put(new_obj); new_obj = json_tokener_parse("\"\\u0041\\u0042\\u0043\""); printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj)); json_object_put(new_obj); new_obj = json_tokener_parse("null"); printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj)); json_object_put(new_obj); new_obj = json_tokener_parse("True"); printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj)); json_object_put(new_obj); new_obj = json_tokener_parse("12"); printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj)); json_object_put(new_obj); new_obj = json_tokener_parse("12.3"); printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj)); json_object_put(new_obj); new_obj = json_tokener_parse("[\"\\n\"]"); printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj)); json_object_put(new_obj); new_obj = json_tokener_parse("[\"\\nabc\\n\"]"); printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj)); json_object_put(new_obj); new_obj = json_tokener_parse("[null]"); printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj)); json_object_put(new_obj); new_obj = json_tokener_parse("[]"); printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj)); json_object_put(new_obj); new_obj = json_tokener_parse("[false]"); printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj)); json_object_put(new_obj); new_obj = json_tokener_parse("[\"abc\",null,\"def\",12]"); printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj)); json_object_put(new_obj); new_obj = json_tokener_parse("{}"); printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj)); json_object_put(new_obj); new_obj = json_tokener_parse("{ \"foo\": \"bar\" }"); printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj)); json_object_put(new_obj); new_obj = json_tokener_parse("{ \"foo\": \"bar\", \"baz\": null, \"bool0\": true }"); printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj)); json_object_put(new_obj); new_obj = json_tokener_parse("{ \"foo\": [null, \"foo\"] }"); printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj)); json_object_put(new_obj); new_obj = json_tokener_parse("{ \"abc\": 12, \"foo\": \"bar\", \"bool0\": false, \"bool1\": true, \"arr\": [ 1, 2, 3, null, 5 ] }"); printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj)); json_object_put(new_obj); /* enum json_tokener_error error = json_tokener_success; new_obj = json_tokener_parse_verbose("{ foo }", &error); assert (error == json_tokener_error_parse_object_key_name); assert (new_obj == NULL); new_obj = json_tokener_parse("{ foo }"); assert (new_obj == NULL); // if(is_error(new_obj)) printf("got error as expected\n"); new_obj = json_tokener_parse("foo"); assert (new_obj == NULL); new_obj = json_tokener_parse_verbose("foo", &error); assert (new_obj == NULL); assert (error == json_tokener_error_parse_boolean); */ new_obj = json_tokener_parse("{ \"foo"); if(is_error(new_obj)) printf("got error as expected\n"); /* test incremental parsing */ tok = json_tokener_new(); new_obj = json_tokener_parse_ex(tok, "{ \"foo", 6); if(is_error(new_obj)) printf("got error as expected\n"); new_obj = json_tokener_parse_ex(tok, "\": {\"bar", 8); if(is_error(new_obj)) printf("got error as expected\n"); new_obj = json_tokener_parse_ex(tok, "\":13}}", 6); printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj)); json_object_put(new_obj); json_tokener_free(tok); json_object_put(my_string); json_object_put(my_int); json_object_put(my_object); json_object_put(my_array); return 0; }
/* read multiple properties; * input: json object of property iids to read, like {"1":1, "2":2}, * NOTE: function get iid from key string. * if input is {}, means read device meta data. * return: * return json object: * { * "services": [ ... ], * "read": { k:v, k:v, ... }, * "err": { "status": <err_code>, * "properties": {k:v, k:v, ...} * } * } * object "read" return successfully read properties value, like {"1":100, "2":99}; * object "services" return device meta data table; * object "err" return err properties and err code in value, liek {"3": -70401, "4": -70402} * if execute error, return NULL. */ json_object* mico_read_properties(struct mico_service_t *service_table, json_object *prop_read_list_obj) { json_object *outJsonObj = NULL; json_object *out_read_obj = NULL; json_object *out_err_obj = NULL; json_object *out_err_prop_obj = NULL; int iid = 0; require( service_table, exit ); require( prop_read_list_obj, exit ); // read meata data if( (NULL == prop_read_list_obj) || (NULL == json_object_get_object(prop_read_list_obj)->head) ){ // input: {} // create "services" json data outJsonObj = mico_get_device_info(service_table); if(NULL == outJsonObj){ properties_log("ERROR: mico_get_device_info error!"); outJsonObj = json_object_new_object(); require( outJsonObj, exit ); out_err_obj = json_object_new_object(); require( out_err_obj, exit ); json_object_object_add(outJsonObj, MICO_PROP_KEY_RESP_ERROR, out_err_obj); json_object_object_add(out_err_obj, MICO_PROP_KEY_RESP_STATUS, json_object_new_int(MICO_PROP_CODE_READ_FAILED)); } else{ out_err_obj = json_object_new_object(); require( out_err_obj, exit ); json_object_object_add(outJsonObj, MICO_PROP_KEY_RESP_ERROR, out_err_obj); json_object_object_add(out_err_obj, MICO_PROP_KEY_RESP_STATUS, json_object_new_int(MICO_PROP_CODE_READ_SUCCESS)); } } else{ // create "read" && "err" sub-obj outJsonObj = json_object_new_object(); require( outJsonObj, exit ); out_read_obj = json_object_new_object(); require( out_read_obj, exit ); json_object_object_add(outJsonObj, MICO_PROP_KEY_RESP_READ, out_read_obj); out_err_obj = json_object_new_object(); require( out_err_obj, exit ); json_object_object_add(outJsonObj, MICO_PROP_KEY_RESP_ERROR, out_err_obj); out_err_prop_obj = json_object_new_object(); require( out_err_prop_obj, exit ); json_object_object_add(out_err_obj, MICO_PROP_KEY_RESP_ERROR_PROPERTIES, out_err_prop_obj); // read for each prop json_object_object_foreach(prop_read_list_obj, key, val) { iid = json_object_get_int(val); _property_read_create_response(service_table, key, iid, out_read_obj, out_err_prop_obj); } // "status" obj for read status code if( (NULL == json_object_get_object(out_err_prop_obj)->head) ){ // no err json_object_object_del(out_err_obj, MICO_PROP_KEY_RESP_ERROR_PROPERTIES); // remove empty "properties" sub-obj json_object_object_add(out_err_obj, MICO_PROP_KEY_RESP_STATUS, json_object_new_int(MICO_PROP_CODE_READ_SUCCESS)); } else{ json_object_object_add(out_err_obj, MICO_PROP_KEY_RESP_STATUS, json_object_new_int(MICO_PROP_CODE_READ_PARTIAL_FAILED)); } }
int main(int argc, char **argv) { json_object *my_string, *my_int, *my_object, *my_array; int i; #ifdef TEST_FORMATTED int sflags = 0; #endif MC_SET_DEBUG(1); #ifdef TEST_FORMATTED sflags = parse_flags(argc, argv); #endif my_string = json_object_new_string("\t"); printf("my_string=%s\n", json_object_get_string(my_string)); printf("my_string.to_string()=%s\n", json_object_to_json_string(my_string)); json_object_put(my_string); my_string = json_object_new_string("\\"); printf("my_string=%s\n", json_object_get_string(my_string)); printf("my_string.to_string()=%s\n", json_object_to_json_string(my_string)); json_object_put(my_string); my_string = json_object_new_string("foo"); printf("my_string=%s\n", json_object_get_string(my_string)); printf("my_string.to_string()=%s\n", json_object_to_json_string(my_string)); my_int = json_object_new_int(9); printf("my_int=%d\n", json_object_get_int(my_int)); printf("my_int.to_string()=%s\n", json_object_to_json_string(my_int)); my_array = json_object_new_array(); json_object_array_add(my_array, json_object_new_int(1)); json_object_array_add(my_array, json_object_new_int(2)); json_object_array_add(my_array, json_object_new_int(3)); json_object_array_put_idx(my_array, 4, json_object_new_int(5)); printf("my_array=\n"); for(i=0; i < json_object_array_length(my_array); i++) { json_object *obj = json_object_array_get_idx(my_array, i); printf("\t[%d]=%s\n", i, json_object_to_json_string(obj)); } printf("my_array.to_string()=%s\n", json_object_to_json_string(my_array)); json_object_put(my_array); my_array = json_object_new_array(); json_object_array_add(my_array, json_object_new_int(3)); json_object_array_add(my_array, json_object_new_int(1)); json_object_array_add(my_array, json_object_new_int(2)); json_object_array_put_idx(my_array, 4, json_object_new_int(0)); printf("my_array=\n"); for(i=0; i < json_object_array_length(my_array); i++) { json_object *obj = json_object_array_get_idx(my_array, i); printf("\t[%d]=%s\n", i, json_object_to_json_string(obj)); } printf("my_array.to_string()=%s\n", json_object_to_json_string(my_array)); json_object_array_sort(my_array, sort_fn); printf("my_array=\n"); for(i=0; i < json_object_array_length(my_array); i++) { json_object *obj = json_object_array_get_idx(my_array, i); printf("\t[%d]=%s\n", i, json_object_to_json_string(obj)); } printf("my_array.to_string()=%s\n", json_object_to_json_string(my_array)); my_object = json_object_new_object(); json_object_object_add(my_object, "abc", json_object_new_int(12)); json_object_object_add(my_object, "foo", json_object_new_string("bar")); json_object_object_add(my_object, "bool0", json_object_new_boolean(0)); json_object_object_add(my_object, "bool1", json_object_new_boolean(1)); json_object_object_add(my_object, "baz", json_object_new_string("bang")); json_object *baz_obj = json_object_new_string("fark"); json_object_get(baz_obj); json_object_object_add(my_object, "baz", baz_obj); json_object_object_del(my_object, "baz"); /* baz_obj should still be valid */ printf("baz_obj.to_string()=%s\n", json_object_to_json_string(baz_obj)); json_object_put(baz_obj); /*json_object_object_add(my_object, "arr", my_array);*/ printf("my_object=\n"); json_object_object_foreach(my_object, key, val) { printf("\t%s: %s\n", key, json_object_to_json_string(val)); }