json_object *make_array() { json_object *my_array; 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)); json_object_array_put_idx(my_array, 3, json_object_new_int(4)); json_object_array_put_idx(my_array, 6, json_object_new_int(7)); return my_array; }
static int json_pointer_set_single_path( struct json_object *parent, const char *path, struct json_object *value) { if (json_object_is_type(parent, json_type_array)) { int32_t idx; /* RFC (Chapter 4) states that '-' may be used to add new elements to an array */ if (path[0] == '-' && path[1] == '\0') return json_object_array_add(parent, value); if (!is_valid_index(parent, path, &idx)) return -1; return json_object_array_put_idx(parent, idx, value); } /* path replacements should have been done in json_pointer_get_single_path(), and we should still be good here */ if (json_object_is_type(parent, json_type_object)) return json_object_object_add(parent, path, value); /* Getting here means that we tried to "dereference" a primitive JSON type (like string, int, bool). i.e. add a sub-object to it */ errno = ENOENT; return -1; }
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)); }
/* 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)); }
DLL_PUBLIC int result(judgm_manage_info *info,line_result_data *res_data){ manage_result_info *res_info; json_object *jso_item; char tpath[PATH_MAX + 1]; res_info = (manage_result_info*)info->private_data; res_info->count++; if(res_data->status > res_info->result){ res_info->result = res_data->status; } res_info->totalscore += res_data->score; res_info->totalruntime += res_data->runtime; if(res_data->memory > res_info->maxmemory){ res_info->maxmemory = res_data->memory; } jso_item = json_object_new_object(); json_object_object_add(jso_item,"status",json_object_new_int(res_data->status)); json_object_object_add(jso_item,"score",json_object_new_double(res_data->score)); json_object_object_add(jso_item,"runtime",json_object_new_int64(res_data->runtime)); json_object_object_add(jso_item,"memory",json_object_new_int64(res_data->memory / 1024UL)); if(strlen(res_data->err_msg) > 0){ printf(" strlen %d\n",strlen(res_data->err_msg)); json_object_object_add(jso_item,"errmsg",json_object_new_string(res_data->err_msg)); } json_object_array_put_idx(res_info->jso_resarray,res_data->id - 1,jso_item); printf("jmod count %d %d\n",res_info->count,res_info->allcount); if(res_info->count == res_info->allcount){ snprintf(tpath,sizeof(tpath),"%s/result",info->res_path); json_object_to_file_ext(tpath,res_info->jso_res,JSON_C_TO_STRING_PLAIN); info->result = res_info->result; info->score = res_info->totalscore; info->runtime = res_info->totalruntime; info->memory = res_info->maxmemory; delete res_info; return 1; } return 0; }
/* adding an object does NOT increment reference count */ int cli_json_addowner(json_object *owner, json_object *child, const char *key, int idx) { json_type objty; if (NULL == owner) { cli_dbgmsg("json: no owner object specified to cli_json_addowner\n"); return CL_ENULLARG; } if (NULL == child) { cli_dbgmsg("json: no child object specified to cli_json_addowner\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_addowner\n"); return CL_ENULLARG; } json_object_object_add(owner, key, child); } else if (objty == json_type_array) { if (idx < 0 || NULL == json_object_array_get_idx(owner, idx)) json_object_array_add(owner, child); else if (0 != json_object_array_put_idx(owner, idx, child)) { /* 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; } /* increment reference count */ json_object_get(child); return CL_SUCCESS; }
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; }
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)); }
int getGroupname() { LDAP *ld; struct json_object *my_object,*my_array; const char* split = "CN=,"; char *p; char*tmp; char *user; int i = 0; int nl ; int result; int auth_method = LDAP_AUTH_SIMPLE; int desired_version = LDAP_VERSION3; char *ldap_host = "WIN2K82"; char *root_dn = "*****@*****.**"; char *root_pw = "123qwe!@#"; BerElement* ber; LDAPMessage* msg; LDAPMessage* entry; char* base="CN=Users,dc=abc,dc=com"; char* filter="(objectClass=group)"; char* errstring; char* dn = NULL; char* attr; char** vals; if ((ld = ldap_init(ldap_host, LDAP_PORT)) == NULL ) { perror( "ldap_init failed" ); exit( EXIT_FAILURE ); } /* set the LDAP version to be 3 */ if (ldap_set_option(ld, LDAP_OPT_PROTOCOL_VERSION, &desired_version) != LDAP_OPT_SUCCESS) { ldap_perror(ld, "ldap_set_option"); exit(EXIT_FAILURE); } if (ldap_bind_s(ld, root_dn, root_pw, auth_method) != LDAP_SUCCESS ) { ldap_perror( ld, "ldap_bind" ); exit( EXIT_FAILURE ); } if (ldap_search_s(ld, base, LDAP_SCOPE_SUBTREE, filter, NULL, 0, &msg) != LDAP_SUCCESS) { ldap_perror( ld, "ldap_search_s" ); exit(EXIT_FAILURE); } my_object = json_object_new_object(); my_array = json_object_new_array(); int n = 0;//find return /* Iterate through the returned entries */ for(entry = ldap_first_entry(ld, msg); entry != NULL; entry = ldap_next_entry(ld, entry)) { if((dn = ldap_get_dn(ld, entry)) != NULL) { //printf("Returned dn: %s\n", dn); tmp = dn; p = strtok (tmp,","); if(p){ p = strtok(p,"="); p = strtok(NULL, ","); BlToSl(p); //if (p) //printf("p=%s\n",p); } i++; json_object_array_add(my_array, json_object_new_int(i)); json_object_array_put_idx(my_array, i, json_object_new_int(i)); struct json_object *obj = json_object_array_get_idx(my_array, i); json_object_object_add(my_object, json_object_to_json_string(obj), json_object_new_string(p)); ldap_memfree(dn); } } //second search base="CN=Builtin,dc=abc,dc=com"; if (ldap_search_s(ld, base, LDAP_SCOPE_SUBTREE, filter, NULL, 0, &msg) != LDAP_SUCCESS) { ldap_perror( ld, "ldap_search_s" ); exit(EXIT_FAILURE); } for(entry = ldap_first_entry(ld, msg); entry != NULL; entry = ldap_next_entry(ld, entry)) { if((dn = ldap_get_dn(ld, entry)) != NULL) { //printf("Returned dn: %s\n", dn); tmp = dn; p = strtok (tmp,","); if(p){ p = strtok(p,"="); p = strtok(NULL, ","); BlToSl(p); //if (p) //printf("p=%s\n",p); } i++; json_object_array_add(my_array, json_object_new_int(i)); json_object_array_put_idx(my_array, i, json_object_new_int(i)); struct json_object *obj = json_object_array_get_idx(my_array, i); json_object_object_add(my_object, json_object_to_json_string(obj), json_object_new_string(p)); ldap_memfree(dn); } } printf("%s\n", json_object_to_json_string(my_object)); /* clean up */ ldap_msgfree(msg); result = ldap_unbind_s(ld); if (result != 0) { fprintf(stderr, "ldap_unbind_s: %s\n", ldap_err2string(result)); exit( EXIT_FAILURE ); } return EXIT_SUCCESS; }
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; }
int HalCtlsNormalizeJsonValues(AFB_ApiT apiHandle, struct CtlHalAlsaCtlProperties *alsaCtlProperties, json_object *toConvertJ, json_object **ConvertedJ) { int err = 0, idx, count, initialValue, convertedValue; json_type toConvertType; json_object *toConvertObjectJ, *convertedValueJ, *convertedArrayJ; toConvertType = json_object_get_type(toConvertJ); switch(toConvertType) { case json_type_array: count = (int) json_object_array_length(toConvertJ); break; case json_type_null: case json_type_object: count = 0; AFB_ApiWarning(apiHandle, "Can't normalize json values :\n-- %s", json_object_get_string(toConvertJ)); *ConvertedJ = json_object_get(toConvertJ); return -1; default: count = 1; break; } convertedArrayJ = json_object_new_array(); for(idx = 0; idx < count; idx++) { if(toConvertType == json_type_array) toConvertObjectJ = json_object_array_get_idx(toConvertJ, idx); else toConvertObjectJ = toConvertJ; switch(alsaCtlProperties->type) { case SND_CTL_ELEM_TYPE_INTEGER: case SND_CTL_ELEM_TYPE_INTEGER64: if(! json_object_is_type(toConvertObjectJ, json_type_int)) { err += -10*idx; AFB_ApiWarning(apiHandle, "Try normalize an integer control but value sent in json is not an integer : '%s'", json_object_get_string(toConvertObjectJ)); convertedValueJ = toConvertObjectJ; break; } initialValue = json_object_get_int(toConvertObjectJ); convertedValue = HalCtlsConvertPercentageToValue(initialValue, alsaCtlProperties->minval, alsaCtlProperties->maxval); if(convertedValue == -INT_MAX) { AFB_ApiWarning(apiHandle, "Can't normalize %i (using min %i et max %i), value set to 0", initialValue, alsaCtlProperties->minval, alsaCtlProperties->maxval); convertedValue = 0; } if(alsaCtlProperties->step) { // Round value to the nearest step convertedValue = (int) round((double) convertedValue / (double) alsaCtlProperties->step); convertedValue *= alsaCtlProperties->step; } convertedValueJ = json_object_new_int(convertedValue); break; case SND_CTL_ELEM_TYPE_BOOLEAN: if(! (json_object_is_type(toConvertObjectJ, json_type_int) || json_object_is_type(toConvertObjectJ, json_type_boolean))) { err += -1000*idx; AFB_ApiWarning(apiHandle, "Try normalize a boolean control but value sent in json is not a boolean/integer : '%s'", json_object_get_string(toConvertObjectJ)); convertedValueJ = toConvertObjectJ; break; } initialValue = json_object_get_int(toConvertObjectJ); convertedValueJ = json_object_new_int(initialValue ? 1 : 0); break; default: AFB_ApiWarning(apiHandle, "Normalization not handle for the alsa control type %i, sending back the object as it was '%s'", (int) alsaCtlProperties->type, json_object_get_string(toConvertJ)); convertedValueJ = toConvertObjectJ; break; } json_object_array_put_idx(convertedArrayJ, idx, convertedValueJ); } *ConvertedJ = convertedArrayJ; return 0; }