SEXP ParseObject(yajl_val node, int bigint){ int len = YAJL_GET_OBJECT(node)->len; SEXP keys = PROTECT(allocVector(STRSXP, len)); SEXP vec = PROTECT(allocVector(VECSXP, len)); for (int i = 0; i < len; ++i) { SET_STRING_ELT(keys, i, mkCharCE(YAJL_GET_OBJECT(node)->keys[i], CE_UTF8)); SET_VECTOR_ELT(vec, i, ParseValue(YAJL_GET_OBJECT(node)->values[i], bigint)); } setAttrib(vec, R_NamesSymbol, keys); UNPROTECT(2); return vec; }
/* Add YAJL value into YAJL generator handle (JSON object) * * jgen - YAJL generator handle allocated by yajl_gen_alloc() * jval - YAJL value usually returned by yajl_tree_get() */ static yajl_gen_status ovs_yajl_gen_val(yajl_gen jgen, yajl_val jval) { size_t array_len = 0; yajl_val *jvalues = NULL; yajl_val jobj_value = NULL; const char *obj_key = NULL; size_t obj_len = 0; yajl_gen_status yajl_gen_ret = yajl_gen_status_ok; if (jval == NULL) return yajl_gen_generation_complete; if (YAJL_IS_STRING(jval)) OVS_YAJL_CALL(ovs_yajl_gen_tstring, jgen, YAJL_GET_STRING(jval)); else if (YAJL_IS_DOUBLE(jval)) OVS_YAJL_CALL(yajl_gen_double, jgen, YAJL_GET_DOUBLE(jval)); else if (YAJL_IS_INTEGER(jval)) OVS_YAJL_CALL(yajl_gen_double, jgen, YAJL_GET_INTEGER(jval)); else if (YAJL_IS_TRUE(jval)) OVS_YAJL_CALL(yajl_gen_bool, jgen, 1); else if (YAJL_IS_FALSE(jval)) OVS_YAJL_CALL(yajl_gen_bool, jgen, 0); else if (YAJL_IS_NULL(jval)) OVS_YAJL_CALL(yajl_gen_null, jgen); else if (YAJL_IS_ARRAY(jval)) { /* create new array and add all elements into the array */ array_len = YAJL_GET_ARRAY(jval)->len; jvalues = YAJL_GET_ARRAY(jval)->values; OVS_YAJL_CALL(yajl_gen_array_open, jgen); for (size_t i = 0; i < array_len; i++) OVS_YAJL_CALL(ovs_yajl_gen_val, jgen, jvalues[i]); OVS_YAJL_CALL(yajl_gen_array_close, jgen); } else if (YAJL_IS_OBJECT(jval)) { /* create new object and add all elements into the object */ OVS_YAJL_CALL(yajl_gen_map_open, jgen); obj_len = YAJL_GET_OBJECT(jval)->len; for (size_t i = 0; i < obj_len; i++) { obj_key = YAJL_GET_OBJECT(jval)->keys[i]; jobj_value = YAJL_GET_OBJECT(jval)->values[i]; OVS_YAJL_CALL(ovs_yajl_gen_tstring, jgen, obj_key); OVS_YAJL_CALL(ovs_yajl_gen_val, jgen, jobj_value); } OVS_YAJL_CALL(yajl_gen_map_close, jgen); } else { OVS_ERROR("%s() unsupported value type %d (skip)", __FUNCTION__, (int)(jval)->type); goto yajl_gen_failure; } return yajl_gen_status_ok; yajl_gen_failure: OVS_ERROR("%s() error to generate value", __FUNCTION__); return yajl_gen_ret; }
void yajl_tree_free (yajl_val v) { if (v == NULL) return; if (YAJL_IS_STRING(v)) { free(v->u.string); free(v); } else if (YAJL_IS_NUMBER(v)) { free(v->u.number.r); free(v); } else if (YAJL_GET_OBJECT(v)) { yajl_object_free(v); } else if (YAJL_GET_ARRAY(v)) { yajl_array_free(v); } else /* if (yajl_t_true or yajl_t_false or yajl_t_null) */ { free(v); } }
/* Get YAJL value by key from YAJL dictionary * * EXAMPLE: * { * "key_a" : <YAJL return value> * "key_b" : <YAJL return value> * } */ yajl_val ovs_utils_get_value_by_key(yajl_val jval, const char *key) { const char *obj_key = NULL; /* check params */ if (!YAJL_IS_OBJECT(jval) || (key == NULL)) return NULL; /* find a value by key */ for (size_t i = 0; i < YAJL_GET_OBJECT(jval)->len; i++) { obj_key = YAJL_GET_OBJECT(jval)->keys[i]; if (strcmp(obj_key, key) == 0) return YAJL_GET_OBJECT(jval)->values[i]; } return NULL; }
Analogsignal* parseJSON(char* responseGnode){ yajl_val node; char errbuf[1024]; node = yajl_tree_parse((const char *) responseGnode, errbuf, sizeof(errbuf)); const char * path[] = { "selected", 0 }; yajl_val v = yajl_tree_get(node, path, yajl_t_array); yajl_val node2 = YAJL_GET_ARRAY(v)->values[0]; const char * path2[] = { "permalink", 0 }; yajl_val v2 = yajl_tree_get(node2, path2, yajl_t_string); // if (v2) printf("%s: %s\n", path2[0], YAJL_GET_STRING(v2)); yajl_val node3 = YAJL_GET_OBJECT(v2); const char * path3[] = { "fields", 0 }; yajl_val v3 = yajl_tree_get(node2, path3, yajl_t_object); const char * path4[] = { "description", 0 }; yajl_val v4 = yajl_tree_get(v3, path4, yajl_t_string); Analogsignal *analog = (Analogsignal *)malloc(sizeof(Analogsignal)); strcpy (analog->permalink, YAJL_GET_STRING(v2)); strcpy (analog->description, YAJL_GET_STRING(v4)); yajl_tree_free(node); return analog; }
void message_json_handler_function(yajl_val j, void *context) { state *s = context; // LLog("[message_json_handler_function] will read value type\n"); // LLog("[message_json_handler_function] value is %d\n", (unsigned int)j); // LLog("[message_json_handler_function] value type is %d\n", j->type); if (!YAJL_IS_OBJECT(j)) { LLog("[message_json_handler_function] value was not an object, bailing out\n"); return; } int num_vals = YAJL_GET_OBJECT(j)->len; for (int i = 0; i < num_vals; i++) { const char *name = YAJL_GET_OBJECT(j)->keys[i]; //LLog("[message_json_handler_function] did get name = %s\n", name); yajl_val value = YAJL_GET_OBJECT(j)->values[i]; if (YAJL_IS_INTEGER(value)) { //LLog("[message_json_handler_function] value is integer\n"); state_set_int(s, name, YAJL_GET_INTEGER(value)); } else if (YAJL_IS_DOUBLE(value)) { //LLog("[message_json_handler_function] value is double\n"); state_set_float(s, name, (float)YAJL_GET_DOUBLE(value)); } else if (YAJL_IS_TRUE(value)) { //LLog("[message_json_handler_function] value is true\n"); state_set_int(s, name, 1); } else if (YAJL_IS_FALSE(value)) { //LLog("[message_json_handler_function] value is false\n"); state_set_int(s, name, 0); } else { //LLog("[message_json_handler_function] value is complex\n"); state_set_json(s, name, value); } } }
static char * parse_json_get_object_string (const char *json, const char *key, int flags, const char *func, const char *cmd) { char *str, *ret; yajl_val tree = NULL, node; size_t i, len; tree = parse_json (json, func); if (tree == NULL) return NULL; if (! YAJL_IS_OBJECT (tree)) goto bad_type; len = YAJL_GET_OBJECT (tree)->len; for (i = 0; i < len; ++i) { if (STREQ (YAJL_GET_OBJECT (tree)->keys[i], key)) { node = YAJL_GET_OBJECT (tree)->values[i]; if ((flags & GET_STRING_NULL_TO_EMPTY) && YAJL_IS_NULL (node)) ret = strdup (""); else { str = YAJL_GET_STRING (node); if (str == NULL) goto bad_type; ret = strdup (str); } if (ret == NULL) reply_with_perror ("strdup"); yajl_tree_free (tree); return ret; } } bad_type: reply_with_error ("output of '%s' was not a JSON object " "containing a key '%s' of type string", cmd, key); yajl_tree_free (tree); return NULL; }
Map * LoadMap(char * map_info){ yajl_val node; char errbuf[1024]; short x,y; short i; Map * map = (Map *) malloc(sizeof(Map)); //logger("Parsing packet from JSON message"); node = yajl_tree_parse(( char *) map_info, errbuf, sizeof(errbuf)); if (node == NULL) { fprintf(stderr,"parse_error: "); if (strlen(errbuf)){ fprintf(stderr," %s", errbuf); }else{ fprintf(stderr,"unknown error"); } fprintf(stderr,"\n"); return false; } const char * path[] = { "map_id", ( char *) 0 }; map->area_id = YAJL_GET_INTEGER(yajl_tree_get(node, path, yajl_t_number)); path[0] = "cell_size"; map->cell_size = YAJL_GET_INTEGER(yajl_tree_get(node, path, yajl_t_number)); path[0] = "x_cells"; map->x_cells = YAJL_GET_INTEGER(yajl_tree_get(node, path, yajl_t_number)); path[0] = "y_cells"; map->y_cells = YAJL_GET_INTEGER(yajl_tree_get(node, path, yajl_t_number)); map->cells = (Cell **) malloc(map->x_cells * sizeof(Cell *)); for (x = 0; x < map->x_cells; x++){ map->cells[x] = (Cell *) malloc(map->y_cells * sizeof(Cell)); } path[0] = "cells"; for (x = 0; x < map->x_cells; x++) for (y = 0; y < map->y_cells; y++){ map->cells[x][y].prob_location = 1; map->cells[x][y].transition_cell = true; yajl_val cell = YAJL_GET_ARRAY(YAJL_GET_ARRAY((yajl_tree_get(node,path, yajl_t_array)))->values[x])->values[y]; if (YAJL_IS_NUMBER(cell)){ map->cells[x][y].prob_location = YAJL_GET_INTEGER(YAJL_GET_ARRAY(YAJL_GET_ARRAY((yajl_tree_get(node,path, yajl_t_array)))->values[x])->values[y]); map->cells[x][y].transition_cell = false; }else if (YAJL_IS_NULL(cell)){ map->cells[x][y].transp.area_id = 0; map->cells[x][y].transp.x_cell = 0; map->cells[x][y].transp.y_cell = 0; }else{ for (i = 0; i < YAJL_GET_OBJECT(cell)->len; i++){ if (!strcmp(YAJL_GET_OBJECT(cell)->keys[i], "area_id")) map->cells[x][y].transp.area_id = YAJL_GET_INTEGER(YAJL_GET_OBJECT(cell)->values[i]); else if (!strcmp(YAJL_GET_OBJECT(cell)->keys[i], "x_cell")) map->cells[x][y].transp.x_cell = YAJL_GET_INTEGER(YAJL_GET_OBJECT(cell)->values[i]); else if (!strcmp(YAJL_GET_OBJECT(cell)->keys[i], "y_cell")) map->cells[x][y].transp.y_cell = YAJL_GET_INTEGER(YAJL_GET_OBJECT(cell)->values[i]); } } } return map; }