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); } }
void bson_from_json_type(bson *b, yajl_val value, const char* key) { if( YAJL_IS_STRING( value ) ) { char* string = YAJL_GET_STRING( value ); bson_append_string( b, key, string ); } else if( YAJL_IS_NUMBER( value ) ) { char* string = value->u.number.r; size_t len = strlen( string ); // Hack to detect a double, since 'flags' is always set to double. if( memchr( string, '.', len ) || memchr( string, 'e', len ) || memchr( string, 'E', len ) ) { double number = YAJL_GET_DOUBLE( value ); bson_append_double( b, key, number ); } else { uint64_t number = YAJL_GET_INTEGER( value ); if( number <= INT_MIN && number <= INT_MAX) bson_append_int( b, key, (int)number ); else bson_append_long( b, key, number); } } else if ( YAJL_IS_FALSE( value ) ) { bson_append_bool( b, key, 0 ); } else if ( YAJL_IS_TRUE( value ) ) { bson_append_bool( b, key, 1 ); } else if ( YAJL_IS_ARRAY( value ) ) { bson_append_start_array( b, key ); bson_from_json_array( b, value ); bson_append_finish_array( b ); } else if ( YAJL_IS_OBJECT( value ) ) { bson_append_start_object( b, key ); bson_from_json_object( b, value ); bson_append_finish_object( b ); } }
SEXP ParseValue(yajl_val node, int bigint){ if(YAJL_IS_NULL(node)){ return R_NilValue; } if(YAJL_IS_STRING(node)){ SEXP tmp = PROTECT(allocVector(STRSXP, 1)); SET_STRING_ELT(tmp, 0, mkCharCE(YAJL_GET_STRING(node), CE_UTF8)); UNPROTECT(1); return tmp; } if(YAJL_IS_INTEGER(node)){ long long int val = YAJL_GET_INTEGER(node); /* 2^53 is highest int stored as double without loss */ if(bigint && (val > 9007199254740992 || val < -9007199254740992)){ char buf[32]; #ifdef _WIN32 snprintf(buf, 32, "%I64d", val); #else snprintf(buf, 32, "%lld", val); #endif return mkString(buf); /* see .Machine$integer.max in R */ } else if(val > 2147483647 || val < -2147483647){ return ScalarReal(val); } else { return ScalarInteger(val); } } if(YAJL_IS_DOUBLE(node)){ return(ScalarReal(YAJL_GET_DOUBLE(node))); } if(YAJL_IS_NUMBER(node)){ /* A number that is not int or double (very rare) */ /* This seems to correctly round to Inf/0/-Inf */ return(ScalarReal(YAJL_GET_DOUBLE(node))); } if(YAJL_IS_TRUE(node)){ return(ScalarLogical(1)); } if(YAJL_IS_FALSE(node)){ return(ScalarLogical(0)); } if(YAJL_IS_OBJECT(node)){ return(ParseObject(node, bigint)); } if(YAJL_IS_ARRAY(node)){ return(ParseArray(node, bigint)); } error("Invalid YAJL node type."); }
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; }