static int cj_curl_perform (cj_t *db, CURL *curl) /* {{{ */ { int status; long rc; char *url; yajl_handle yprev = db->yajl; db->yajl = yajl_alloc (&ycallbacks, NULL, NULL, (void *)db); if (db->yajl == NULL) { ERROR ("curl_json plugin: yajl_alloc failed."); db->yajl = yprev; return (-1); } url = NULL; curl_easy_getinfo(curl, CURLINFO_EFFECTIVE_URL, &url); status = curl_easy_perform (curl); if (status != 0) { ERROR ("curl_json plugin: curl_easy_perform failed with status %i: %s (%s)", status, db->curl_errbuf, (url != NULL) ? url : "<null>"); yajl_free (db->yajl); db->yajl = yprev; return (-1); } curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &rc); /* The response code is zero if a non-HTTP transport was used. */ if ((rc != 0) && (rc != 200)) { ERROR ("curl_json plugin: curl_easy_perform failed with " "response code %ld (%s)", rc, url); yajl_free (db->yajl); db->yajl = yprev; return (-1); } status = yajl_parse_complete (db->yajl); if (status != yajl_status_ok) { unsigned char *errmsg; errmsg = yajl_get_error (db->yajl, /* verbose = */ 0, /* jsonText = */ NULL, /* jsonTextLen = */ 0); ERROR ("curl_json plugin: yajl_parse_complete failed: %s", (char *) errmsg); yajl_free_error (db->yajl, errmsg); yajl_free (db->yajl); db->yajl = yprev; return (-1); } yajl_free (db->yajl); db->yajl = yprev; return (0); } /* }}} int cj_curl_perform */
yajl_val yajl_tree_parse_options (const char *input, char *error_buffer, size_t error_buffer_size, yajl_tree_option options) { static const yajl_callbacks callbacks = { /* null = */ handle_null, /* boolean = */ handle_boolean, /* integer = */ NULL, /* double = */ NULL, /* number = */ handle_number, /* string = */ handle_string, /* start map = */ handle_start_map, /* map key = */ handle_string, /* end map = */ handle_end_map, /* start array = */ handle_start_array, /* end array = */ handle_end_array }; yajl_handle handle; yajl_status status; char * internal_err_str; context_t ctx = { NULL, NULL, NULL, 0 }; ctx.errbuf = error_buffer; ctx.errbuf_size = error_buffer_size; if (error_buffer != NULL) memset (error_buffer, 0, error_buffer_size); handle = yajl_alloc (&callbacks, NULL, &ctx); yajl_config(handle, yajl_allow_comments, (options & yajl_tree_option_dont_allow_comments) ? 0 : 1); yajl_config(handle, yajl_allow_trailing_separator, (options & yajl_tree_option_allow_trailing_separator) ? 1 : 0); status = yajl_parse(handle, (unsigned char *) input, strlen (input)); status = yajl_complete_parse (handle); if (status != yajl_status_ok) { if (error_buffer != NULL && error_buffer_size > 0) { internal_err_str = (char *) yajl_get_error(handle, 1, (const unsigned char *) input, strlen(input)); snprintf(error_buffer, error_buffer_size, "%s", internal_err_str); YA_FREE(&(handle->alloc), internal_err_str); } while (ctx.stack) { yajl_tree_free(context_pop(&ctx)); } yajl_free (handle); return NULL; } yajl_free (handle); return (ctx.root); }
static int cj_perform (cj_t *db) /* {{{ */ { int status; yajl_handle yprev = db->yajl; db->yajl = yajl_alloc (&ycallbacks, #if HAVE_YAJL_V2 /* alloc funcs = */ NULL, #else /* alloc funcs = */ NULL, NULL, #endif /* context = */ (void *)db); if (db->yajl == NULL) { ERROR ("curl_json plugin: yajl_alloc failed."); db->yajl = yprev; return (-1); } if (db->url) status = cj_curl_perform (db); else status = cj_sock_perform (db); if (status < 0) { yajl_free (db->yajl); db->yajl = yprev; return (-1); } #if HAVE_YAJL_V2 status = yajl_complete_parse(db->yajl); #else status = yajl_parse_complete(db->yajl); #endif if (status != yajl_status_ok) { unsigned char *errmsg; errmsg = yajl_get_error (db->yajl, /* verbose = */ 0, /* jsonText = */ NULL, /* jsonTextLen = */ 0); ERROR ("curl_json plugin: yajl_parse_complete failed: %s", (char *) errmsg); yajl_free_error (db->yajl, errmsg); yajl_free (db->yajl); db->yajl = yprev; return (-1); } yajl_free (db->yajl); db->yajl = yprev; return (0); } /* }}} int cj_perform */
/* * Public functions */ yajl_val yajl_tree_parse (const char *input, char *error_buffer, size_t error_buffer_size) { static const yajl_callbacks callbacks = { /* null = */ handle_null, /* boolean = */ handle_boolean, /* integer = */ NULL, /* double = */ NULL, /* number = */ handle_number, /* string = */ handle_string, /* start map = */ handle_start_map, /* map key = */ handle_string, /* end map = */ handle_end_map, /* start array = */ handle_start_array, /* end array = */ handle_end_array }; yajl_handle handle; yajl_status status; context_t ctx = { NULL, NULL, NULL, 0 }; ctx.errbuf = error_buffer; ctx.errbuf_size = error_buffer_size; if (error_buffer != NULL) memset (error_buffer, 0, error_buffer_size); handle = yajl_alloc (&callbacks, NULL, &ctx); yajl_config(handle, yajl_allow_comments, 1); status = yajl_parse(handle, (unsigned char *) input, strlen (input)); status = yajl_complete_parse (handle); if (status != yajl_status_ok) { if (error_buffer != NULL && error_buffer_size > 0) { snprintf( error_buffer, error_buffer_size, "%s", (char *) yajl_get_error(handle, 1, (const unsigned char *) input, strlen(input))); } yajl_free (handle); return NULL; } yajl_free (handle); return (ctx.root); }
static VALUE mParser_do_yajl_parse(VALUE self, VALUE str, VALUE yajl_opts) { yajl_handle hand; yajl_status stat; unsigned char *err; volatile CTX ctx; rb_ivar_set(self, rb_intern("key"), Qnil); rb_ivar_set(self, rb_intern("stack"), rb_ary_new()); rb_ivar_set(self, rb_intern("key_stack"), rb_ary_new()); ctx.self = self; ctx.symbolizeKeys = get_opts_key(self, "symbolize_keys"); ctx.uniqueKeyChecking = get_opts_key(self, "unique_key_checking"); hand = yajl_alloc(&callbacks, NULL, (void *)&ctx); if (rb_hash_aref(yajl_opts, ID2SYM(rb_intern("yajl_allow_comments"))) == Qtrue) { yajl_config(hand, yajl_allow_comments, 1); } if (rb_hash_aref(yajl_opts, ID2SYM(rb_intern("yajl_dont_validate_strings"))) == Qtrue) { yajl_config(hand, yajl_dont_validate_strings, 1); } if (rb_hash_aref(yajl_opts, ID2SYM(rb_intern("yajl_allow_trailing_garbage"))) == Qtrue) { yajl_config(hand, yajl_allow_trailing_garbage, 1); } if (rb_hash_aref(yajl_opts, ID2SYM(rb_intern("yajl_allow_multiple_values"))) == Qtrue) { yajl_config(hand, yajl_allow_multiple_values, 1); } if (rb_hash_aref(yajl_opts, ID2SYM(rb_intern("yajl_allow_partial_values"))) == Qtrue) { yajl_config(hand, yajl_allow_partial_values, 1); } if ((stat = yajl_parse(hand, (unsigned char *)RSTRING_PTR(str), RSTRING_LEN(str))) != yajl_status_ok) { err = yajl_get_error(hand, 1, (unsigned char *)RSTRING_PTR(str), RSTRING_LEN(str)); goto raise; } if ((stat = yajl_complete_parse(hand)) != yajl_status_ok) { err = yajl_get_error(hand, 1, (unsigned char *)RSTRING_PTR(str), RSTRING_LEN(str)); goto raise; } yajl_free(hand); return rb_ary_pop(rb_ivar_get(self, rb_intern("stack"))); raise: if (hand) { yajl_free(hand); } rb_raise(cParseError, "%s", err); }
void yajl_parser_wrapper_free(void * wrapper) { yajl_parser_wrapper * w = wrapper; if (w) { yajl_free(w->parser); free(w); } }
~YajlHandle() { if (errstring) yajl_free_error(hand, errstring); if (hand) yajl_free(hand); }
static void _j4status_i3bar_output_uninit(J4statusPluginContext *context) { yajl_free(context->json_handle); g_free(context); }
static const char *parse_json(state_t *st, unsigned char *buf, size_t len) { yajl_parser_config cfg = { 1, /* allow comments */ 0 /* don't check UTF-8 */ }; yajl_handle yh; yajl_status ys; const char *err=NULL; alloc_funcs.ctx = st->env; /* will be passed to alloc funcs */ yh = yajl_alloc(&callbacks, &cfg, &alloc_funcs, st); ys = yajl_parse(yh, buf, len); if (ys == yajl_status_insufficient_data) { ys = yajl_parse_complete(yh); } if (ys == yajl_status_insufficient_data) { err = "unexpected end of document"; } else if (ys != yajl_status_ok) { unsigned char *msg = yajl_get_error(yh, 0, NULL, 0); strncpy(st->errmsg, (char *)msg, sizeof(st->errmsg)-1); yajl_free_error(yh, msg); st->errmsg[sizeof(st->errmsg)] = 0; err = st->errmsg; } yajl_free(yh); return err; }
/* * Start parsing the received bar configuration json-string * */ void parse_config_json(char *json) { yajl_handle handle; yajl_status state; #if YAJL_MAJOR < 2 yajl_parser_config parse_conf = { 0, 0 }; handle = yajl_alloc(&outputs_callbacks, &parse_conf, NULL, NULL); #else handle = yajl_alloc(&outputs_callbacks, NULL, NULL); #endif state = yajl_parse(handle, (const unsigned char*) json, strlen(json)); /* FIXME: Proper errorhandling for JSON-parsing */ switch (state) { case yajl_status_ok: break; case yajl_status_client_canceled: #if YAJL_MAJOR < 2 case yajl_status_insufficient_data: #endif case yajl_status_error: ELOG("Could not parse config-reply!\n"); exit(EXIT_FAILURE); break; } yajl_free(handle); }
struct aws_dynamo_put_item_response * aws_dynamo_parse_put_item_response(const char *response, int response_len, struct aws_dynamo_attribute *attributes, int num_attributes) { yajl_handle hand; yajl_status stat; struct put_item_ctx _ctx = { 0 }; _ctx.r = calloc(sizeof(*(_ctx.r)), 1); if (_ctx.r == NULL) { Warnx("aws_dynamo_parse_put_item_response: response alloc failed."); return NULL; } if (num_attributes > 0) { _ctx.r->attributes = malloc(sizeof(*(_ctx.r->attributes)) * num_attributes); if (_ctx.r->attributes == NULL) { Warnx("aws_dynamo_parse_put_item_response: attribute alloc failed."); free(_ctx.r); return NULL; } memcpy(_ctx.r->attributes, attributes, sizeof(*(attributes)) * num_attributes); _ctx.r->num_attributes = num_attributes; } #if YAJL_MAJOR == 2 hand = yajl_alloc(&put_item_callbacks, NULL, &_ctx); yajl_parse(hand, response, response_len); stat = yajl_complete_parse(hand); #else hand = yajl_alloc(&put_item_callbacks, NULL, NULL, &_ctx); yajl_parse(hand, response, response_len); stat = yajl_parse_complete(hand); #endif if (stat != yajl_status_ok) { unsigned char *str = yajl_get_error(hand, 1, response, response_len); Warnx("aws_dynamo_parse_put_item_response: json parse failed, '%s'", (const char *)str); yajl_free_error(hand, str); yajl_free(hand); aws_dynamo_free_put_item_response(_ctx.r); return NULL; } yajl_free(hand); return _ctx.r; }
/* XXX add an incremental streaming parser - yajl trivially supports it */ virJSONValuePtr virJSONValueFromString(const char *jsonstring) { yajl_handle hand; virJSONParser parser = { NULL, NULL, 0 }; virJSONValuePtr ret = NULL; # ifndef WITH_YAJL2 yajl_parser_config cfg = { 1, 1 }; # endif VIR_DEBUG("string=%s", jsonstring); # ifdef WITH_YAJL2 hand = yajl_alloc(&parserCallbacks, NULL, &parser); if (hand) { yajl_config(hand, yajl_allow_comments, 1); yajl_config(hand, yajl_dont_validate_strings, 0); } # else hand = yajl_alloc(&parserCallbacks, &cfg, NULL, &parser); # endif if (!hand) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Unable to create JSON parser")); goto cleanup; } if (yajl_parse(hand, (const unsigned char *)jsonstring, strlen(jsonstring)) != yajl_status_ok) { unsigned char *errstr = yajl_get_error(hand, 1, (const unsigned char*)jsonstring, strlen(jsonstring)); virReportError(VIR_ERR_INTERNAL_ERROR, _("cannot parse json %s: %s"), jsonstring, (const char*) errstr); VIR_FREE(errstr); virJSONValueFree(parser.head); goto cleanup; } ret = parser.head; cleanup: yajl_free(hand); if (parser.nstate) { size_t i; for (i = 0; i < parser.nstate; i++) { VIR_FREE(parser.state[i].key); } VIR_FREE(parser.state); } VIR_DEBUG("result=%p", parser.head); return ret; }
CJSONVariantParser::~CJSONVariantParser() { #if YAJL_MAJOR == 2 yajl_complete_parse(m_handler); #else yajl_parse_complete(m_handler); #endif yajl_free(m_handler); }
SLNFilterRef SLNJSONFilterParserEnd(SLNJSONFilterParserRef const parser) { if(!parser) return NULL; assertf(parser->JSONParser, "Parser in invalid state"); yajl_status const err = yajl_complete_parse(parser->JSONParser); yajl_free(parser->JSONParser); parser->JSONParser = NULL; assertf(-1 == parser->depth, "Parser ended at invalid depth %d", parser->depth); SLNFilterRef const filter = parser->stack[0]; parser->stack[0] = NULL; return yajl_status_ok == err ? filter : NULL; }
static void sl_json_parse_check_error(sl_vm_t* vm, sl_string_t* str, json_parse_t* json, yajl_status status) { uint8_t* err_str; SLVAL err; if(status == yajl_status_client_canceled) { /* the only reason we'd cancel the parse is if the data structre is too deep */ yajl_free(json->yajl); sl_throw_message2(vm, vm->store[cJSON_ParseError], "JSON structure recurses too deep"); } if(status == yajl_status_error) { err_str = yajl_get_error(json->yajl, 0, (const uint8_t*)str->buff, (size_t)str->buff_len); err = sl_make_cstring(vm, (char*)err_str); yajl_free_error(json->yajl, err_str); yajl_free(json->yajl); sl_throw(vm, sl_make_error2(vm, vm->store[cJSON_ParseError], err)); } }
static void jp_destroy(JsonParserCtx *ctx) { while (ctx->nestingChain) { jp_nesting_chain_head_pop(ctx); } JsonElFree(ctx->firstEl); if (ctx->yajl_handle) { yajl_free(ctx->yajl_handle); } }
void json_streamer_destroy(struct json_streamer *json) { if (json) { if (json->yajl) yajl_free(json->yajl); if (json->ctx) free(json->ctx); free(json); } }
static void rest_json_payload_free(void *f) { int i; struct rest_json_payload *json = f; if(json->parser) yajl_free(json->parser); if(json->error) free(json->error); for(i=0;i<MAX_DEPTH;i++) if(json->keys[i]) free(json->keys[i]); if(json->last_value) free(json->last_value); free(json); }
int parseSampleFromFile(Sample *sample, FILE *inputFile) { yajl_handle hand; static unsigned char fileData[READ_BUFFER_SIZE]; ParseContext g; g.sample = sample; yajl_status stat; size_t rd; /* allow comments */ yajl_parser_config cfg = { 1, 1 }; int retval = 1, done = 0; /* ok. open file. let's read and parse */ hand = yajl_alloc(&callbacks, &cfg, NULL, (void *) &g); while (!done) { rd = fread((void *) fileData, 1, sizeof(fileData) - 1, inputFile); if (rd == 0) { if (!feof(inputFile)) { fprintf(stderr, "error on file read.\n"); retval = 0; break; } done = 1; } fileData[rd] = 0; if (done) /* parse any remaining buffered data */ stat = yajl_parse_complete(hand); else /* read file data, pass to parser */ stat = yajl_parse(hand, fileData, rd); if (stat != yajl_status_ok && stat != yajl_status_insufficient_data) { unsigned char * str = yajl_get_error(hand, 1, fileData, rd); fprintf(stderr, "%s", (const char *) str); yajl_free_error(hand, str); retval = 0; if( stat == yajl_status_client_canceled) { fprintf(stderr, "%s\n", g.lastError); } break; } } yajl_free(hand); return retval; }
// Client records will be coming through in alphabetical order. // FIX THIS: If a client is deleted on the server, it is not deleted from // clist. int json_input(struct asfd *asfd, struct sel *sel) { static yajl_handle yajl=NULL; cslist=&sel->clist; sselbu=&sel->backup; sllines=&sel->llines; if(!yajl) { if(!(yajl=yajl_alloc(&callbacks, NULL, NULL))) goto error; yajl_config(yajl, yajl_dont_validate_strings, 1); } if(yajl_parse(yajl, (const unsigned char *)asfd->rbuf->buf, asfd->rbuf->len)!=yajl_status_ok) { do_yajl_error(yajl, asfd); goto error; } if(!map_depth) { // Got to the end of the JSON object. if(!sel->gotfirstresponse) sel->gotfirstresponse=1; if(yajl_complete_parse(yajl)!=yajl_status_ok) { do_yajl_error(yajl, asfd); goto error; } yajl_free(yajl); yajl=NULL; } return 0; error: yajl_free(yajl); yajl=NULL; return -1; }
void SLNJSONFilterParserFree(SLNJSONFilterParserRef *const parserptr) { SLNJSONFilterParserRef parser = *parserptr; if(!parser) return; parser->session = NULL; if(parser->JSONParser) { yajl_free(parser->JSONParser); parser->JSONParser = NULL; } for(size_t i = 0; i < DEPTH_MAX; ++i) { SLNFilterFree(&parser->stack[i]); } assert_zeroed(parser, 1); FREE(parserptr); parser = NULL; }
TEST_F(Yajl, yajl_parse_nullcallbacks) { for (size_t i = 0; i < kTrialCount; i++) { yajl_handle hand = yajl_alloc(&nullcallbacks, NULL, NULL); yajl_status stat = yajl_parse(hand, (unsigned char*)json_, length_ - 1); //ASSERT_EQ(yajl_status_ok, stat); if (stat != yajl_status_ok) { unsigned char * str = yajl_get_error(hand, 1, (unsigned char*)json_, length_ + 1); fprintf(stderr, "%s", (const char *) str); } stat = yajl_complete_parse(hand); ASSERT_EQ(yajl_status_ok, stat); yajl_free(hand); } }
void reformat(ErlDrvPort port, char* buf, int len) { yajl_handle hand; /* generator config */ yajl_gen_config conf = { 1, " " }; yajl_gen g; yajl_status stat; /* allow comments */ yajl_parser_config cfg = { 1, 1 }; g = yajl_gen_alloc(&conf, NULL); /* ok. open file. let's read and parse */ hand = yajl_alloc(&callbacks, &cfg, NULL, (void *) g); /* read file data, pass to parser */ stat = yajl_parse(hand, (unsigned char*) buf, len); if (stat != yajl_status_ok && stat != yajl_status_insufficient_data) { char* err = (char*) yajl_get_error(hand, 1, (unsigned char*) buf, len); int len = strlen(err); ErlDrvTermData msg[] = { ERL_DRV_ATOM, driver_mk_atom("error"), ERL_DRV_BUF2BINARY, (ErlDrvTermData) err, (ErlDrvUInt) len, ERL_DRV_TUPLE, 2 }; driver_send_term(port, driver_caller(port), msg, sizeof(msg) / sizeof(msg[0])); } else { const unsigned char* json; unsigned int len; yajl_gen_get_buf(g, &json, &len); ErlDrvTermData msg[] = { ERL_DRV_ATOM, driver_mk_atom("ok"), ERL_DRV_BUF2BINARY, (ErlDrvTermData) json, (ErlDrvUInt) len, ERL_DRV_TUPLE, 2 }; driver_send_term(port, driver_caller(port), msg, sizeof(msg) / sizeof(msg[0])); yajl_gen_clear(g); } yajl_gen_free(g); yajl_free(hand); }
int main(int argc, char ** argv) { yajl_handle hand; yajl_status stat; yajl_parser_config cfg = { 1, 1 }; int done = 0; hand = yajl_alloc(&callbacks, &cfg, NULL, (void *) g); while (!done) { rd = fread((void *) fileData, 1, sizeof(fileData) - 1, stdin); if (rd == 0) { if (!feof(stdin)) { fprintf(stderr, "error on file read.\n"); break; } done = 1; } fileData[rd] = 0; if (done) /* parse any remaining buffered data */ stat = yajl_parse_complete(hand); else /* read file data, pass to parser */ stat = yajl_parse(hand, fileData, rd); if (stat != yajl_status_ok && stat != yajl_status_insufficient_data) { unsigned char * str = yajl_get_error(hand, 1, fileData, rd); fprintf(stderr, (const char *) str); yajl_free_error(hand, str); } else { const unsigned char * buf; unsigned int len; yajl_gen_get_buf(g, &buf, &len); fwrite(buf, 1, len, stdout); yajl_gen_clear(g); } } yajl_gen_free(g); yajl_free(hand); return 0; }
void jsonfill(const char *filename,int *array) { std::ifstream fin(filename); fin.seekg(0,std::ios::end); int length = fin.tellg(); fin.seekg(0,std::ios::beg); unsigned char *buffer = new unsigned char [length]; fin.read ((char *) buffer,length); fin.close(); yajl_handle handl = yajl_alloc(&call,&conf,NULL,&array); yajl_parse(handl,buffer,length); yajl_free(handl); free(buffer); }
/* * Parse the JSON protocol header to determine protocol version and features. * In case the buffer does not contain a valid header (invalid JSON, or no * version field found), the 'correct' field of the returned header is set to * false. The amount of bytes consumed by parsing the header is returned in * *consumed (if non-NULL). * */ void parse_json_header(i3bar_child *child, const unsigned char *buffer, int length, unsigned int *consumed) { static yajl_callbacks version_callbacks = { .yajl_boolean = header_boolean, .yajl_integer = header_integer, .yajl_map_key = &header_map_key, }; child_init(child); current_key = NO_KEY; yajl_handle handle = yajl_alloc(&version_callbacks, NULL, child); /* Allow trailing garbage. yajl 1 always behaves that way anyways, but for * yajl 2, we need to be explicit. */ yajl_config(handle, yajl_allow_trailing_garbage, 1); yajl_status state = yajl_parse(handle, buffer, length); if (state != yajl_status_ok) { child_init(child); if (consumed != NULL) *consumed = 0; } else { if (consumed != NULL) *consumed = yajl_get_bytes_consumed(handle); } yajl_free(handle); }
struct aws_dynamo_create_table_response *aws_dynamo_parse_create_table_response(const char *response, int response_len) { yajl_handle hand; yajl_status stat; struct ctx _ctx = { 0 }; _ctx.r = calloc(sizeof(*(_ctx.r)), 1); if (_ctx.r == NULL) { Warnx("aws_dynamo_parse_create_table_response: response alloc failed."); return NULL; } #if YAJL_MAJOR == 2 hand = yajl_alloc(&handle_callbacks, NULL, &_ctx); yajl_parse(hand, response, response_len); stat = yajl_complete_parse(hand); #else hand = yajl_alloc(&handle_callbacks, NULL, NULL, &_ctx); yajl_parse(hand, response, response_len); stat = yajl_parse_complete(hand); #endif if (stat != yajl_status_ok) { unsigned char *str = yajl_get_error(hand, 1, response, response_len); Warnx("aws_dynamo_parse_create_table_response: json parse failed, '%s'", (const char *)str); yajl_free_error(hand, str); yajl_free(hand); aws_dynamo_free_create_table_response(_ctx.r); return NULL; } yajl_free(hand); return _ctx.r; }
/* * Start parsing the received JSON string * */ void parse_outputs_json(char *json) { struct outputs_json_params params; params.outputs_walk = NULL; params.cur_key = NULL; params.json = json; params.in_rect = false; yajl_handle handle; yajl_status state; handle = yajl_alloc(&outputs_callbacks, NULL, (void *)¶ms); state = yajl_parse(handle, (const unsigned char *)json, strlen(json)); /* FIXME: Proper errorhandling for JSON-parsing */ switch (state) { case yajl_status_ok: break; case yajl_status_client_canceled: case yajl_status_error: ELOG("Could not parse outputs reply!\n"); exit(EXIT_FAILURE); break; } yajl_free(handle); }
VOID ReadData(REQUEST_CONTEXT* context) { yajl_handle hand; yajl_parser_config cfg = { 1, 1 }; json_ctx ctx = { 0, 0 }; hand = JSONCodec::AllocateHandle(&cfg, &ctx); unsigned char temp[BUFFER_SIZE]; DWORD read = 0; BOOL res = FALSE; while(true) { res = WinHttpReadData(context->hRequest, temp, BUFFER_SIZE, &read); if(!res) { Log::Error("Error reading response data"); break; } if(read == 0) break; temp[read] = 0; yajl_status status = yajl_parse(hand, temp, read); if(status == yajl_status_error) { Log::Error("Error parsing response data"); res = FALSE; break; } } yajl_parse_complete(hand); yajl_free(hand); JSONCodec::Decode(ctx.current, context->px); JSONCodec::FreeJsonValue(ctx.current); }
static SLVAL sl_json_parse(sl_vm_t* vm, SLVAL self, size_t argc, SLVAL* argv) { sl_string_t* str = sl_get_string(vm, argv[0]); json_parse_t json; yajl_alloc_funcs alloc_funcs = { sl_yajl_alloc, sl_yajl_realloc, sl_yajl_free, NULL }; alloc_funcs.ctx = vm->arena; json.vm = vm; json.max_depth = 32; json.stack_len = 0; json.stack_cap = 32; json.stack = sl_alloc(vm->arena, sizeof(SLVAL) * json.stack_cap); json.type_stack = sl_alloc(vm->arena, json.stack_cap); if(argc > 1) { json.max_depth = sl_get_int(sl_expect(vm, argv[1], vm->lib.Int)); } json.yajl = yajl_alloc(&callbacks, &alloc_funcs, &json); sl_json_parse_check_error(vm, str, &json, yajl_parse(json.yajl, str->buff, str->buff_len)); sl_json_parse_check_error(vm, str, &json, yajl_complete_parse(json.yajl)); yajl_free(json.yajl); /* must've been OK! */ return json.stack[0]; (void)self; }