void douban_radio_parse(struct douban_radio* douban, const char* buffer, rt_size_t length) { JSON_config config; struct JSON_parser_struct* jc = NULL; const char* ptr; init_JSON_config(&config); config.depth = 19; config.callback = &_parse_callback; config.callback_ctx = douban; config.allow_comments = 1; config.handle_floats_manually = 0; jc = new_JSON_parser(&config); ptr = buffer; while (ptr < buffer + length) { if (!JSON_parser_char(jc, *ptr++)) { // rt_kprintf("JSON_parser_error: parse failed\n"); break; } } if (!JSON_parser_done(jc)) { // rt_kprintf("JSON_parser_end: syntax error\n"); } delete_JSON_parser(jc); }
void createPTreeFromJsonString(const char *json, bool caseInsensitive, StringBuffer &xml, const char *tail) { int count = 0, result = 0; JSonToXmlStringContext jcontext(xml, tail); struct JSON_parser_struct* jc = NULL; JSON_config config; init_JSON_config(&config); config.depth = 19; config.callback = &ptree_builder; config.allow_comments = 1; config.handle_floats_manually = 0; config.callback_ctx = &jcontext; /* Important! Set locale before parser is created.*/ setlocale(LC_ALL, "english"); jc = new_JSON_parser(&config); const char *finger = json; for (; *finger ; finger++) { if (!JSON_parser_char(jc, *finger)) { throw MakeStringException(-1, "JSON_parser_char: syntax error, byte %d\n", (int) (finger - json)); } } if (!JSON_parser_done(jc)) { throw MakeStringException(-1, "JSON_parser_end: syntax error\n"); } //TBD: worry about exception cleanup later delete_JSON_parser(jc); }
int tr_jsonParse( const void * vbuf, size_t len, tr_benc * setme_benc, const uint8_t ** setme_end ) { int line = 1; int column = 1; int err = 0; const unsigned char * buf = vbuf; const void * bufend = buf + len; struct JSON_config_struct config; struct JSON_parser_struct * checker; struct json_benc_data data; init_JSON_config( &config ); config.callback = callback; config.callback_ctx = &data; config.depth = -1; data.hasContent = FALSE; data.key = NULL; data.top = setme_benc; data.stack = TR_PTR_ARRAY_INIT; checker = new_JSON_parser( &config ); while( ( buf != bufend ) && JSON_parser_char( checker, *buf ) ) { if( *buf != '\n' ) ++column; else { ++line; column = 1; } ++buf; } if( buf != bufend ) { tr_err( "JSON parser failed at line %d, column %d: \"%.16s\"", line, column, buf ); err = EILSEQ; } if( !data.hasContent ) err = EINVAL; if( setme_end ) *setme_end = (const uint8_t*) buf; delete_JSON_parser( checker ); tr_ptrArrayDestruct( &data.stack, NULL ); return err; }
int main(int argc, char* argv[]) { int count = 0; FILE* input; JSON_config config; struct JSON_parser_struct* jc = NULL; init_JSON_config(&config); config.depth = 20; config.callback = &print; config.allow_comments = 1; config.handle_floats_manually = 0; /* Important! Set locale before parser is created.*/ if (argc >= 2) { if (!setlocale(LC_ALL, argv[1])) { fprintf(stderr, "Failed to set locale to '%s'\n", argv[1]); } } else { fprintf(stderr, "No locale provided, C locale is used\n"); } jc = new_JSON_parser(&config); input = stdin; for (; input ; ++count) { int next_char = fgetc(input); if(count == 1601) fprintf(stderr, "1061\n"); if (next_char <= 0) { break; } fprintf(stderr, "%d) %c %u\n", count, next_char, (unsigned int) next_char);fflush(stderr); if (!JSON_parser_char(jc, next_char)) { delete_JSON_parser(jc); fprintf(stderr, "JSON_parser_char: syntax error, byte %d\n", count); return 1; } } if (!JSON_parser_done(jc)) { delete_JSON_parser(jc); fprintf(stderr, "JSON_parser_end: syntax error\n"); return 1; } return 0; }
ham_status_t config_parse_string(const char *string, config_table_t **params) { unsigned count=0; JSON_config config; struct JSON_parser_struct *jc=0; config_table_t *p=(config_table_t *)calloc(sizeof(config_table_t), 1); if (!p) return (HAM_OUT_OF_MEMORY); *params=0; init_JSON_config(&config); config.depth=20; config.callback=__parser_cb; config.callback_ctx=(void *)p; config.allow_comments=1; config.handle_floats_manually=1; jc=new_JSON_parser(&config); while (*string) { if (!JSON_parser_char(jc, *string)) { delete_JSON_parser(jc); hlog(3, "JSON syntax error in byte %u\n", count); config_clear_table(p); return (HAM_INV_PARAMETER); } count++; string++; } if (!JSON_parser_done(jc)) { delete_JSON_parser(jc); config_clear_table(p); return (HAM_INV_PARAMETER); } *params=p; delete_JSON_parser(jc); return (0); }
/** * Process a JSON object that we got from the burrow server. * Presently intended to be called on a complete JSON thingie. In future * perhaps this should be extended so it can be called on an incomplete * JSON response, allowing us to start parsing before we get the entire * message. * * @param backend the http backend * @param jsontext the actual JSON text we got from the burrow server * @param jsonsize the size of the JSON text we got from the burrow server * @return 0 if successful, otherwise errno value if it fails. */ int burrow_backend_http_parse_json(burrow_backend_t *backend, char *jsontext, size_t jsonsize) { JSON_config config; struct JSON_parser_struct* jc = NULL; init_JSON_config(&config); json_processing_t *json_processing = burrow_easy_json_st_create(backend); config.depth = 19; config.callback = &burrow_backend_http_json_callback; config.callback_ctx = json_processing; config.allow_comments = 1; config.handle_floats_manually = 0; jc = new_JSON_parser(&config); int i; for (i = 0; i < (int)jsonsize; ++i) { int retval; int nextchar = jsontext[i]; if ((retval = JSON_parser_char(jc, nextchar)) <= 0) { burrow_error(burrow_backend_http_get_burrow(backend), EINVAL, "WARNING! JSON_parser_char (%d) at byte %d (%d = '%c')\n", retval, i, (int)nextchar, nextchar); return EINVAL; } } if (!JSON_parser_done(jc)) { burrow_error(burrow_backend_http_get_burrow(backend), EINVAL, "WARNING! JSON_parser_end indicates JSON syntax error\n"); delete_JSON_parser(jc); burrow_easy_json_st_destroy(json_processing); return EINVAL; } delete_JSON_parser(jc); burrow_easy_json_st_destroy(json_processing); return 0; }
json_item *init_json_parser(const char *json_string) { const char *pRaw; JSON_config config; struct JSON_parser_struct* jc = NULL; json_context jcx = {0, 0, NULL, NULL}; init_JSON_config(&config); config.depth = 15; config.callback = &json_callback; config.callback_ctx = &jcx; config.allow_comments = 0; config.handle_floats_manually = 0; jc = new_JSON_parser(&config); for (pRaw = json_string; *pRaw; pRaw++) { if (!JSON_parser_char(jc, *pRaw)) { free_json_item(jcx.head); delete_JSON_parser(jc); return NULL; } } if (!JSON_parser_done(jc)) { free_json_item(jcx.head); delete_JSON_parser(jc); return NULL; } delete_JSON_parser(jc); return jcx.head; }
void config_init(void) { JSON_config jc; assert(pandora); memset(&pandora->config, 0, sizeof(config_t)); pandora->config.core = true; pandora->config.state = xcalloc(1, sizeof(config_state_t)); /* Set sane defaults for configuration */ pandora->config.log_console_fd = STDERR_FILENO; pandora->config.log_level = 2; pandora->config.log_timestamp = true; pandora->config.follow_fork = 1; pandora->config.exit_wait_all = 1; pandora->config.whitelist_per_process_directories = true; pandora->config.whitelist_successful_bind = true; pandora->config.whitelist_unsupported_socket_families = true; pandora->config.abort_decision = ABORT_CONTALL; pandora->config.panic_decision = PANIC_KILL; pandora->config.panic_exit_code = -1; pandora->config.violation_decision = VIOLATION_DENY; pandora->config.violation_exit_code = -1; pandora->config.child.magic_lock = LOCK_UNSET; init_JSON_config(&jc); jc.depth = -1; jc.allow_comments = 1; jc.handle_floats_manually = 0; jc.callback = parser_callback; jc.callback_ctx = pandora->config.state; pandora->config.parser = new_JSON_parser(&jc); }
int tr_jsonParse( const void * vbuf, size_t len, tr_benc * setme_benc, const uint8_t ** setme_end ) { int err = 0; const unsigned char * buf = vbuf; const void * bufend = buf + len; struct JSON_config_struct config; struct JSON_parser_struct * checker; struct json_benc_data data; init_JSON_config( &config ); config.callback = callback; config.callback_ctx = &data; config.depth = -1; data.key = NULL; data.top = setme_benc; data.stack = tr_ptrArrayNew( ); checker = new_JSON_parser( &config ); while( ( buf != bufend ) && JSON_parser_char( checker, *buf ) ) ++buf; if( buf != bufend ) err = EILSEQ; if( setme_end ) *setme_end = (const uint8_t*) buf; delete_JSON_parser( checker ); tr_ptrArrayFree( data.stack, NULL ); return err; }
JSON_parser new_JSON_parser(JSON_config* config) { /* new_JSON_parser starts the checking process by constructing a JSON_parser object. It takes a depth parameter that restricts the level of maximum nesting. To continue the process, call JSON_parser_char for each character in the JSON text, and then call JSON_parser_done to obtain the final result. These functions are fully reentrant. */ int depth = 0; JSON_config default_config; JSON_parser jc = malloc(sizeof(struct JSON_parser_struct)); memset(jc, 0, sizeof(*jc)); /* initialize configuration */ init_JSON_config(&default_config); /* set to default configuration if none was provided */ if (config == NULL) { config = &default_config; } depth = config->depth; /* We need to be able to push at least one object */ if (depth == 0) { depth = 1; } jc->state = GO; jc->top = -1; /* Do we want non-bound stack? */ if (depth > 0) { jc->stack_capacity = depth; jc->depth = depth; if (depth <= COUNTOF(jc->static_stack)) { jc->stack = &jc->static_stack[0]; } else { jc->stack = (signed char*)malloc(jc->stack_capacity * sizeof(jc->static_stack[0])); } } else { jc->stack_capacity = COUNTOF(jc->static_stack); jc->depth = -1; jc->stack = &jc->static_stack[0]; } /* set parser to start */ push(jc, MODE_DONE); /* set up the parse buffer */ jc->parse_buffer = &jc->static_parse_buffer[0]; jc->parse_buffer_capacity = COUNTOF(jc->static_parse_buffer); parse_buffer_clear(jc); /* set up callback, comment & float handling */ jc->callback = config->callback; jc->ctx = config->callback_ctx; jc->allow_comments = config->allow_comments != 0; jc->handle_floats_manually = config->handle_floats_manually != 0; return jc; }
/** * @brief initialize * 初始化libhttp.so插件 * 在该函数内执行初始化插件级别参数结构体 * 比如: * 读取用户指定的配置文件 * 分析该配置文件(可以用json格式) * 生成http_conft_st结构体 * 并将该结构体指针赋值给bench_t的param元素 * 以后init(),act(),end()都可以通过指针得到该元素 * 从而完成了插件级别业务参数的传递 * * @param p * * @return */ void* initialize(void *p) { int scenarioId = 0; pmw = (middleware_t*)p; bench_t *pb = pmw->bp; char conf[1024] = {0}; char conf_content[10240] = {0}; FILE *conf_file; /* * 性能测试的时候 * 类似http这种协议 * 会建立非常多的句柄fd * 因此需要设置ulimit * * 每个用户都有最大的ulimit open files限制 * 必须使用root帐户更改最大数量 * 命令如下: * [root:] ulimit -n 1000000 */ /* * pb指向的bench_t结构体是处于mmap空间的 * 所以需要通过cbutil中的strpcpy方法来复制字符串 * 而不能使用strcpy等方法 * */ pmalloc(sizeof(http_conf_st), &conf_st); pb->param = conf_st; /* * 初始化json-parser */ JSON_config config; struct JSON_parser_struct* jc = NULL; init_JSON_config(&config); config.depth = 19; config.callback = &cfg_cb; config.allow_comments = 1; config.handle_floats_manually = 0; jc = new_JSON_parser(&config); /* * 初始化curl * 该函数可能线程不安全 * 在curl官网有描述 * 但在官网的这个例子里面可以在下面这个多线程代码中使用: * http://curl.haxx.se/libcurl/c/multithread.html * */ /* * curl太慢,不如自己实现get和post */ //curl_global_init(CURL_GLOBAL_ALL); printf("Config file[default \"../../conf/http.conf\"]:\n"); conf[0] = (char)fgetc(stdin); /* * 如够只输入回车 * 则读取默认conf文件位置 * */ if(conf[0] == '\n' ) strcpy(conf, "../../conf/http.conf"); else{ scanf("%s",conf+1); printf("\n"); } /* * 读入配置文件内容 * 解析配置 * */ if((conf_file = fopen(conf,"r")) == 0) fprintf(stderr,"Can't read %s",conf); else{ int char_buf; while((char_buf = fgetc(conf_file)) > 0){ if (!JSON_parser_char(jc, char_buf)) { fprintf(stderr, "JSON_parser_char: syntax error\n"); exit(-1); } } if (!JSON_parser_done(jc)) { fprintf(stderr, "JSON_parser_end: syntax error\n"); exit(-1); } fclose(conf_file); } delete_JSON_parser(jc); }
SEXP R_readFromJSON(SEXP r_input, SEXP depth, SEXP allowComments, SEXP func, SEXP data, SEXP maxChar) { JSON_config conf; struct JSON_parser_struct *parser; SEXP ans = R_NilValue; int do_unprotect = 1; RJSONParserInfo info = {NULL, NULL, CE_NATIVE}; init_JSON_config(&conf); conf.depth = INTEGER(depth)[0]; conf.allow_comments = LOGICAL(allowComments)[0]; /* Handle the callback function and data here. First the C routines and data context.*/ if(Rf_length(data)) { SEXP tmp = VECTOR_ELT(data, 1); void *ptr; switch(TYPEOF(tmp)) { case NILSXP: ptr = NULL; break; case INTSXP: case LGLSXP: ptr = INTEGER(tmp); break; case REALSXP: ptr = REAL(tmp); break; case VECSXP: ptr = VECTOR_PTR(tmp); break; default: ptr = NULL; } conf.callback = (JSON_parser_callback) R_ExternalPtrAddr(VECTOR_ELT(data, 0)); conf.callback_ctx = ptr; do_unprotect = 0; } else if(func != R_NilValue && TYPEOF(func) == CLOSXP) { /* we have a function*/ SEXP e; PROTECT(e = allocVector(LANGSXP, 3)); SETCAR(e, func); SETCAR(CDR(e), allocVector(INTSXP, 1)); SET_NAMES(CAR(CDR(e)), info.names = NEW_CHARACTER(1)); SETCAR(CDR(CDR(e)), R_NilValue); info.func = e; ans = R_NilValue; conf.callback = R_json_basicCallback; conf.callback_ctx = &info; } else if(func == R_NilValue) PROTECT(ans = NEW_LIST(1)); else { /* You what? */ PROBLEM "unhandled type of R object as handler function %d", TYPEOF(func) ERROR; } parser = new_JSON_parser(&conf); if(inherits(r_input, "connection")) { R_json_parse_connection(r_input, maxChar, parser); } else { R_json_parse_character(r_input, maxChar, parser); } if(do_unprotect) UNPROTECT(1); return(ans); }