/* Render an array to text */ static char *print_array(struct json *item, int depth, int fmt) { char **entries; char *out = 0, *ptr, *ret; int len = 5; struct json *child = item->child; int numentries = 0, i = 0, fail = 0; /* How many entries in the array? */ while (child) numentries++, child = child->next; /* Allocate an array to hold the values for each */ entries = (char **)json_malloc(numentries * sizeof(char *)); if (!entries) return 0; memset(entries, 0, numentries * sizeof(char *)); /* Retrieve all the results: */ child = item->child; while (child && !fail) { ret = print_value(child, depth + 1, fmt); entries[i++] = ret; if (ret) len += strlen(ret) + 2 + (fmt ? 1 : 0); else fail = 1; child = child->next; } /* If we didn't fail, try to malloc the output string */ if (!fail) out = (char *)json_malloc(len); /* If that fails, we fail. */ if (!out) fail = 1; /* Handle failure. */ if (fail) { json_free(entries); return 0; } /* Compose the output array. */ *out = '['; ptr = out + 1; *ptr = 0; for (i = 0; i < numentries; i++) { strcpy(ptr, entries[i]); ptr += strlen(entries[i]); if (i != numentries - 1) { *ptr++ = ','; if (fmt) *ptr++ = ' '; *ptr = 0; } } json_free(entries); *ptr++ = ']'; *ptr++ = 0; return out; }
void json_object_free(json_t *root) { // json_t* last; // json_t* next; if(root == NULL) { return; } if(root->children != NULL) { json_object_free(root->children); root->children = NULL; } if(root->next != NULL) { json_object_free(root->next); root->next = NULL; } // if(root->last != NULL) { // json_object_free(root->last); // root->last = NULL; // } if(root->value != NULL) { json_free(root->value); root->value = NULL; } json_free(root); }
int json_wlparam_strset_proc(JSONNODE* node, wlp_descr_t* wlp, void* param) { int i; wlp_strset_t* ss = (wlp_strset_t*) param; char* js = NULL; if(json_type(node) != JSON_STRING) return WLPARAM_JSON_WRONG_TYPE; js = json_as_string(node); for(i = 0; i < wlp->range.ss_num; ++i) { if(strcmp(wlp->range.ss_strings[i], js) == 0) { *ss = i; json_free(js); return WLPARAM_JSON_OK; } } /* No match found - wrong value provided*/ json_free(js); *ss = -1; return WLPARAM_JSON_OUTSIDE_RANGE; }
static json_t *fan_handle_change_state(json_t *req, int32 tzone_idx, int32 fan_idx, struct rest_uri_param *param) { int64 enabled_state = 0; result_t rs = RESULT_OK; enabled_state = json_integer_value(json_object_get(req, RMM_JSON_ENABLE_STATE)); if (!((enabled_state == FAN_STATE_ENABLED) || (enabled_state == FAN_STATE_DISABLED))) { HTTPD_ERR("unknown fan state\n"); update_response_info(param, HTTP_APPLICATION_ERROR); json_free(req); return NULL; } rs = libwrap_post_fan_change_state(tzone_idx, fan_idx, enabled_state); if (rs != RESULT_OK) { HTTPD_ERR("fan state post fail, result is %d\n", rs); update_response_info(param, HTTP_APPLICATION_ERROR); } else { update_response_info(param, HTTP_ACCEPTED); } json_free(req); int8 buff[128] = {}; snprintf(buff, sizeof(buff), "%d", (tzone_idx - 1) * MAX_PWM_NUM + fan_idx); rf_snmp_evt(INFO, MSGFanChange, buff, "enable state change"); return NULL; }
/* * Check with the CA whether a challenge has been processed. * Note: we'll only do this a limited number of times, and pause for a * time between checks, but this happens in the caller. */ static int dochngcheck(struct conn *c, struct chng *chng) { int cc; long lc; struct jsmnn *j; dodbg("%s: status", chng->uri); if ((lc = nreq(c, chng->uri)) < 0) { warnx("%s: bad comm", chng->uri); return(0); } else if (200 != lc && 201 != lc && 202 != lc) { warnx("%s: bad HTTP: %ld", chng->uri, lc); buf_dump(&c->buf); return(0); } else if (NULL == (j = json_parse(c->buf.buf, c->buf.sz))) { warnx("%s: bad JSON object", chng->uri); buf_dump(&c->buf); return(0); } else if (-1 == (cc = json_parse_response(j))) { warnx("%s: bad response", chng->uri); buf_dump(&c->buf); json_free(j); return(0); } else if (cc > 0) chng->status = 1; json_free(j); return(1); }
void json_free(struct json *jbase) { struct json_childs *jchilds = jbase->jchilds; free(jbase->name); if (jbase->value != NULL) { free(jbase->value); } if (jbase->next != NULL) { json_free(jbase->next); } while (jchilds != NULL) { json_free(jchilds->child); jchilds = jchilds->next; } free(jbase); }
void SdkHandler::passwordFinished(char *bufferchar) { CCLOG("in function:[%s], buff:[%s]", __FUNCTION__, bufferchar); int state = 0; int userId = 0; int flag = 0; JSONNODE *n = json_parse(bufferchar); if (n == NULL){ return; } JSONNODE_ITERATOR i = json_begin(n); while (i != json_end(n)){ CCLOG("Start Parse Json in [%s]", __FUNCTION__); if (*i == NULL){ break; } // recursively call ourselves to dig deeper into the tree if (json_type(*i) == JSON_ARRAY || json_type(*i) == JSON_NODE){ break; } // get the node name and value as a string json_char *node_name = json_name(*i); // find out where to store the values if (strcmp(node_name, "STATE") == 0){ json_int_t node_value = json_as_int(*i); state = node_value; } else if (strcmp(node_name, "USER_ID") == 0){ json_int_t node_value = json_as_int(*i); userId = node_value; } else if (strcmp(node_name, "LOGIN_KEY") == 0){ json_char *node_value = json_as_string(*i); json_free(node_value); } else if (strcmp(node_name, "ERROR_TYPE") == 0){ json_int_t node_value = json_as_int(*i); flag = node_value; } // cleanup and increment the iterator json_free(node_name); ++i; } CCLOG("state:[%d], flag:[%d]", state, flag); SdkInfoData *sdkLoginData = new SdkInfoData(); sdkLoginData->state = state; sdkLoginData->errorFlag = flag; SGNotificationCenter::sharedNotificationCenter()->postNotification(PASSWORDLAG,sdkLoginData,false); }
bool Json::Parse(const string& str) { JSONNODE* node = json_parse(str.c_str()); if (node == NULL) return false; JSONNODE_ITERATOR iter = json_begin(node); while (iter != json_end(node)) { json_char* nodeName = json_name(*iter); if(string(nodeName) == "" || json_type(*iter) == JSON_NULL) { json_free(nodeName); break; } if (json_type(*iter) == JSON_NODE) { if(!Parse(nodeName, *iter)) return false; } else if(json_type(*iter) == JSON_ARRAY) { JSONNODE_ITERATOR i = json_begin(*iter); while (i != json_end(*iter)) { if(json_type(*iter) == JSON_NUMBER) { mDataFloatArray[nodeName].push_back(json_as_float(*i)); } else { mDataStrArray[nodeName].push_back(json_as_string(*i)); } i++; } } else if(json_type(*iter) == JSON_NUMBER) { mDataFloat[nodeName] = json_as_float(*iter); } else if(json_type(*iter) == JSON_BOOL) { mDataBool[nodeName] = json_as_bool(*iter); } else if(json_type(*iter) == JSON_STRING) { mDataStr[nodeName] = json_as_string(*iter); } json_free(nodeName); iter++; } json_delete(node); return true; }
void SdkHandler::destroyGuestFinished(char *bufferchar) { CCLOG("in function:[%s], buff:[%s]", __FUNCTION__, bufferchar); int state = 0; int userId = 0; int flag = 0; JSONNODE *n = json_parse(bufferchar); if (n == NULL){ return; } JSONNODE_ITERATOR i = json_begin(n); while (i != json_end(n)){ CCLOG("Start Parse Json in [%s]", __FUNCTION__); if (*i == NULL){ break; } // recursively call ourselves to dig deeper into the tree if (json_type(*i) == JSON_ARRAY || json_type(*i) == JSON_NODE){ break; } // get the node name and value as a string json_char *node_name = json_name(*i); // find out where to store the values if (strcmp(node_name, "STATE") == 0){ json_int_t node_value = json_as_int(*i); state = node_value; } else if (strcmp(node_name, "USER_ID") == 0){ json_int_t node_value = json_as_int(*i); userId = node_value; } else if (strcmp(node_name, "LOGIN_KEY") == 0){ json_char *node_value = json_as_string(*i); json_free(node_value); } else if (strcmp(node_name, "ERROR_TYPE") == 0){ json_int_t node_value = json_as_int(*i); flag = node_value; } // cleanup and increment the iterator json_free(node_name); ++i; } }
void JSON_GET_TEXT_ARRAY(sLONG_PTR *pResult, PackagePtr pParams) { C_TEXT json; ARRAY_TEXT values; json.fromParamAtIndex(pParams, 1); values.setSize(1); JSONNODE *n = _fromHex(json); if(n){ if(json_type(n) == JSON_ARRAY){ JSONNODE_ITERATOR i = json_begin(n); while (i != json_end(n)){ if (*i){ json_char *s = json_as_string(*i); std::wstring w = std::wstring(s); C_TEXT t; _copyString(w, t); CUTF16String u; t.copyUTF16String(&u); values.appendUTF16String(&u); json_free(s); } ++i; } } } values.toParamAtIndex(pParams, 2); }
void JSON_Export_to_text(sLONG_PTR *pResult, PackagePtr pParams) { C_TEXT json; C_LONGINT option; C_TEXT returnValue; json.fromParamAtIndex(pParams, 1); option.fromParamAtIndex(pParams, 2); JSONNODE *n = _fromHex(json); json_char *s; if(n){ switch (option.getIntValue()) { case 1: s = json_write(n); break; default: s = json_write_formatted(n); break; } std::wstring w = std::wstring(s); _copyString(w, returnValue); json_free(s); } returnValue.setReturn(pResult); }
// sets a json value for the next index int json_array_add_int(json_t *array, int *value, json_context_t *json_context) { int exit_code = 0; json_t *json_integer_value = NULL; check_not_null(array); check_not_null(json_context); if (value != NULL) { json_integer_value = json_integer(*value); check_not_null(json_integer_value); check_result(json_array_append_new(array, json_integer_value), 0); json_integer_value = NULL; } else { check_result(json_array_append(array, json_null()), 0); } goto cleanup; error: exit_code = -1; cleanup: if (json_integer_value != NULL) { json_free(json_integer_value); } return exit_code; }
// parses a json string int json_parse_string(char *string, json_t **json) { json_t *json_return = NULL; int exit_code = 0; check_not_null(string); check_not_null(json); json_return = json_loads(string, JSON_REJECT_DUPLICATES, NULL); *json = json_return; goto cleanup; error: if (json_return != NULL) { json_free(json_return); } exit_code = -1; cleanup: return exit_code; }
u_char * dbrelay_exec_query(dbrelay_connection_t *conn, char *database, char *sql, unsigned long flags) { json_t *json = json_new(); u_char *ret; if (flags & DBRELAY_FLAG_PP) json_pretty_print(json, 1); if (flags & DBRELAY_FLAG_EMBEDCSV) json_set_mode(json, DBRELAY_JSON_MODE_CSV); api->change_db(conn->db, database); if (flags & DBRELAY_FLAG_XACT) api->exec(conn->db, api->catalogsql(DBRELAY_DBCMD_BEGIN, NULL)); if (api->exec(conn->db, sql)) { dbrelay_db_fill_data(json, conn); if (flags & DBRELAY_FLAG_XACT) api->exec(conn->db, api->catalogsql(DBRELAY_DBCMD_COMMIT, NULL)); } else { if (flags & DBRELAY_FLAG_XACT) api->exec(conn->db, api->catalogsql(DBRELAY_DBCMD_ROLLBACK, NULL)); return NULL; } ret = (u_char *) json_to_string(json); json_free(json); return ret; }
void json_array_free(JSON_array a){ JSON *curr=a; while(*curr) { json_free(*(curr++)); } free(a); }
void json_free( void* obj, const js_struct* desc ) { unsigned char* ptr = obj; const js_struct* subdesc; size_t i, *asize; void* sub; if( !obj ) return; for( i=0; i<desc->num_members; ++i ) { sub = *((void**)(ptr + desc->members[i].offset)); asize = (size_t*)(ptr + desc->members[i].sizeoffset); subdesc = desc->members[i].desc; switch( desc->members[i].type ) { case TYPE_STRING: break; case TYPE_OBJ: json_free( sub, subdesc ); break; case TYPE_OBJ_ARRAY: json_free_array( sub, *asize, subdesc ); break; default: continue; } free( sub ); } }
int main(int argc, char **argv) { FILE *f = NULL; if ((f = fopen("ticker.json", "r")) != NULL) { char buffer[BUFSIZ] = {0}; char *file = NULL; while (fread(buffer, 1, sizeof(buffer) - 1, f) > 0) { file = dstrcati(file, buffer); memset(buffer, 0, sizeof(buffer)); } struct json_value *v = NULL; if ((v = json_parse(file)) != NULL) { print_bitcoin_ticker(v); json_free(v); } else { printf("Bad JSON!\n"); } free(file); fclose(f); } else { perror("fopen()"); } }
static struct threadqueue_t *initDev(JsonNode *jdevice) { char *platform = GPIO_PLATFORM; if(config_setting_get_string("gpio-platform", 0, &platform) != 0) { logprintf(LOG_ERR, "no gpio-platform configured"); return NULL; } if(strcmp(platform, "none") == 0) { FREE(platform); logprintf(LOG_ERR, "no gpio-platform configured"); return NULL; } if(wiringXSetup(platform, logprintf1) < 0) { FREE(platform); return NULL; } FREE(platform); loop = 1; char *output = json_stringify(jdevice, NULL); JsonNode *json = json_decode(output); json_free(output); struct protocol_threads_t *node = protocol_thread_init(gpio_switch, json); return threads_register("gpio_switch", &thread, (void *)node, 0); }
void test_spaceapi__parse_directory(void) { char *str = NULL; int len = 0; FILE *f = fopen(cl_fixture("directory.json"), "r"); cl_assert(f != NULL); char buf[1024]; int count = 0; while ((count = fread(buf, 1, 1024, f)) > 0){ str = realloc(str, len + count); memcpy(str + len, buf, count); len += count; } fclose(f); str = realloc(str, len + 1); str[len] = '\0'; struct json_data *json = json_parse(str); free(str); cl_assert(json->type == JSON_OBJECT); json_free(json); }
void del_property(extend **entry, char *key) { while (*entry != NULL) { if (strcmp((*entry)->key, key) == 0) { extend *pEntry = *entry; *entry = (*entry)->next; switch(pEntry->type) { case EXTEND_STR: free(pEntry->val); break; case EXTEND_JSON: json_free(pEntry->val); break; default: break; } free(pEntry); return; } entry = &(*entry)->next; } }
void *events_clientize(void *param) { logprintf(LOG_STACK, "%s(...)", __FUNCTION__); unsigned int failures = 0; while(loop && failures <= 5) { struct ssdp_list_t *ssdp_list = NULL; int standalone = 0; settings_find_number("standalone", &standalone); if(ssdp_seek(&ssdp_list) == -1 || standalone == 1) { logprintf(LOG_DEBUG, "no pilight ssdp connections found"); char server[16] = "127.0.0.1"; if((sockfd = socket_connect(server, (unsigned short)socket_get_port())) == -1) { logprintf(LOG_DEBUG, "could not connect to pilight-daemon"); failures++; continue; } } else { if((sockfd = socket_connect(ssdp_list->ip, ssdp_list->port)) == -1) { logprintf(LOG_DEBUG, "could not connect to pilight-daemon"); failures++; continue; } } if(ssdp_list) { ssdp_free(ssdp_list); } struct JsonNode *jclient = json_mkobject(); struct JsonNode *joptions = json_mkobject(); json_append_member(jclient, "action", json_mkstring("identify")); json_append_member(joptions, "config", json_mknumber(1, 0)); json_append_member(jclient, "options", joptions); json_append_member(jclient, "media", json_mkstring("all")); char *out = json_stringify(jclient, NULL); socket_write(sockfd, out); json_free(out); json_delete(jclient); if(socket_read(sockfd, &recvBuff, 0) != 0 || strcmp(recvBuff, "{\"status\":\"success\"}") != 0) { failures++; continue; } failures = 0; while(loop) { if(socket_read(sockfd, &recvBuff, 0) != 0) { break; } else { events_queue(recvBuff); } } } if(recvBuff) { FREE(recvBuff); recvBuff = NULL; } socket_close(sockfd); return 0; }
void json_free_array( void* arr, size_t count, const js_struct* desc ) { size_t i; for( i=0; i<count; ++i ) json_free( (char*)arr + i*desc->objsize, desc ); }
// Adapated from vsnprintf() char *json_strvprintf(const char *fmt, va_list ap_in) { int n, size = 64 /*guess*/; char *p, *np; va_list ap; assert(fmt != NULL); if ( !(p = json_malloc(size)) ) return NULL; while (true) { va_copy(ap, ap_in); n = vsnprintf(p, size, fmt, ap); va_end(ap); if (n > -1 && n < size) return p; if (n > -1) size = n + 1; else size *= 2; if ( (np = json_realloc(p, size)) == NULL ) { json_free(p); return NULL; } p = np; } }
/* * Look up directories from the certificate authority. */ static int dodirs(struct conn *c, const char *addr, struct capaths *paths) { struct jsmnn *j; long lc; int rc; j = NULL; rc = 0; dodbg("%s: directories", addr); if ((lc = nreq(c, addr)) < 0) warnx("%s: bad comm", addr); else if (200 != lc && 201 != lc) warnx("%s: bad HTTP: %ld", addr, lc); else if (NULL == (j = json_parse(c->buf.buf, c->buf.sz))) warnx("json_parse"); else if ( ! json_parse_capaths(j, paths)) warnx("%s: bad CA paths", addr); else rc = 1; if (0 == rc || verbose > 1) buf_dump(&c->buf); json_free(j); return(rc); }
int rmm_cfg_get_vm_root_password(char *password, int max_len, int vm_idx) { json_t *jvm; char attr[16] = {0}; snprintf(attr, sizeof(attr),"%s%d", ATTR_VM_HEADER, vm_idx); json_t *conf = load_rmm_cfg(); if (conf == 0) { printf("Load rmm config file fail...\n"); return -1; } jvm = get_json_attr(conf, PROC_AUTO_TEST, attr); if (jvm == NULL) return -1; json_t *jpw = json_object_get(jvm, ATTR_VM_ROOT_PASSWORD); if (jpw == NULL) { printf("Can't find the %s attribute.\n", ATTR_VM_ROOT_PASSWORD); return -1; } char *data = json_string_value(jpw); strncpy_safe(password, data, max_len, max_len - 1); json_free(conf); return 0; }
int config_write(int level, const char *media) { logprintf(LOG_STACK, "%s(...)", __FUNCTION__); struct JsonNode *root = json_mkobject(); FILE *fp; sort_list(0); struct config_t *listeners = config; while(listeners) { if(listeners->sync) { struct JsonNode *child = listeners->sync(level, media); if(child != NULL) { json_append_member(root, listeners->name, child); } } listeners = listeners->next; } /* Overwrite config file with proper format */ if(!(fp = fopen(configfile, "w+"))) { logprintf(LOG_ERR, "cannot write config file: %s", configfile); return EXIT_FAILURE; } fseek(fp, 0L, SEEK_SET); char *content = json_stringify(root, "\t"); fwrite(content, sizeof(char), strlen(content), fp); fclose(fp); json_free(content); json_delete(root); return EXIT_SUCCESS; }
/* * Request a challenge for the given domain name. * This must happen for each name "alt". * On non-zero exit, fills in "chng" with the challenge. */ static int dochngreq(struct conn *c, const char *alt, struct chng *chng, const struct capaths *p) { int rc; char *req; long lc; struct jsmnn *j; j = NULL; rc = 0; dodbg("%s: req-auth: %s", p->newauthz, alt); if (NULL == (req = json_fmt_newauthz(alt))) warnx("json_fmt_newauthz"); else if ((lc = sreq(c, p->newauthz, req)) < 0) warnx("%s: bad comm", p->newauthz); else if (200 != lc && 201 != lc) warnx("%s: bad HTTP: %ld", p->newauthz, lc); else if (NULL == (j = json_parse(c->buf.buf, c->buf.sz))) warnx("%s: bad JSON object", p->newauthz); else if ( ! json_parse_challenge(j, chng)) warnx("%s: bad challenge", p->newauthz); else rc = 1; if (0 == rc || verbose > 1) buf_dump(&c->buf); json_free(j); free(req); return(rc); }
/** * hello: a simple "ping" command to check that the * device is responsive */ void cmd_hello(char *line) { JSONNODE *n = json_new(JSON_NODE); json_push_back(n, json_new_a("hello", "Greetings professor Falken")); json_char *jc = json_write_formatted(n); serial_write_string(jc); json_free(jc); json_delete(n); }
/* Delete a struct json structure. */ void json_delete(struct json *c) { struct json *next; while (c) { next = c->next; if (!(c->type & JSON_T_IS_REFERENCE) && c->child) json_delete(c->child); if (!(c->type & JSON_T_IS_REFERENCE) && c->valuestring) json_free(c->valuestring); if (c->string) json_free(c->string); if (c->print_out) json_free(c->print_out); json_free(c); c = next; } }
void Json::Reset() { json_free(mJsonStr); json_delete(mpRootNode); mpRootNode = json_new(JSON_NODE); mJsonStr = NULL; }