static int js_parser_parse(lua_State *L) { yajl_handle* handle = (yajl_handle*) lua_touserdata(L, lua_upvalueindex(1)); if ( lua_isnil(L, 1) ) { js_parser_assert(L, yajl_complete_parse(*handle), handle, NULL, 0, __FILE__, __LINE__); } else { size_t len; const unsigned char* buff = (const unsigned char*) luaL_checklstring(L, 1, &len); if ( NULL == buff ) return 0; js_parser_assert(L, yajl_parse(*handle, buff, len), handle, buff, len, __FILE__, __LINE__); } return 0; }
inline bool __JSONParseWithString(__JSONRef json, CFStringRef string, CFErrorRef *error) { bool success = 1; json->yajlParser = yajl_alloc(&json->yajlParserCallbacks, &json->yajlAllocFuncs, (void *)json); if (json->yajlParser) { // yajl_config(json->yajlParser, yajl_allow_comments, kJSONReadOptionAllowComments | options ? 1 : 0); // yajl_config(json->yajlParser, yajl_dont_validate_strings, kJSONReadOptionCheckUTF8 | options ? 1 : 0); CFDataRef data = CFStringCreateExternalRepresentation(json->allocator, string, kCFStringEncodingUTF8, 0); if (data) { if ((json->yajlParserStatus = yajl_parse(json->yajlParser, CFDataGetBytePtr(data), CFDataGetLength(data))) != yajl_status_ok) { if (error) { success = 0; unsigned char * str = yajl_get_error(json->yajlParser, 1, CFDataGetBytePtr(data), CFDataGetLength(data)); fprintf(stderr, "%s", (const char *) str); yajl_free_error(json->yajlParser, str); *error = CFErrorCreateWithUserInfoKeysAndValues(json->allocator, CFSTR("com.github.mirek.CoreJSON"), (CFIndex)json->yajlParserStatus, (const void *) { kCFErrorDescriptionKey }, (const void *) { CFSTR("Test") }, 1); } // TODO: Error stuff //printf("ERROR: %s\n", yajl_get_error(json->yajlParser, 1, __JSONUTF8StringGetBuffer(utf8), __JSONUTF8StringGetMaximumSize(utf8))); } json->yajlParserStatus = yajl_complete_parse(json->yajlParser); CFRelease(data); } else {
static size_t cj_curl_callback (void *buf, /* {{{ */ size_t size, size_t nmemb, void *user_data) { cj_t *db; size_t len; yajl_status status; len = size * nmemb; if (len <= 0) return (len); db = user_data; if (db == NULL) return (0); status = yajl_parse(db->yajl, (unsigned char *)buf, len); if (status == yajl_status_ok) return (len); #if !HAVE_YAJL_V2 else if (status == yajl_status_insufficient_data) return (len); #endif unsigned char *msg = yajl_get_error(db->yajl, /* verbose = */ 1, /* jsonText = */ (unsigned char *) buf, (unsigned int) len); ERROR ("curl_json plugin: yajl_parse failed: %s", msg); yajl_free_error(db->yajl, msg); return (0); /* abort write callback */ } /* }}} size_t cj_curl_callback */
static void _j4status_i3bar_ouput_action(J4statusPluginContext *context, gchar *line) { gsize length = strlen(line); yajl_status json_state; json_state = yajl_parse(context->json_handle, (const unsigned char *) line, length); if ( json_state == yajl_status_ok ) return; g_free(context->parse_context.name); g_free(context->parse_context.instance); if ( json_state == yajl_status_error ) { unsigned char *str_error; str_error = yajl_get_error(context->json_handle, 0, (const unsigned char *) line, length); g_warning("Couldn't parse section from i3bar: %s", str_error); yajl_free_error(context->json_handle, str_error); } else if ( json_state == yajl_status_client_canceled ) { g_warning("i3bar JSON protocol error: %s", context->parse_context.error); g_free(context->parse_context.error); } }
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); }
/* if this method throw an exception then the object is invalid */ bool JsonShredder::Shred(uint64_t docseq, const std::string& json, std::string* idout, std::string* errout) { YajlHandle hand(&ctx_); yajl_status stat; bool success = true; ctx_.docseq = docseq; stat = yajl_parse(hand, (unsigned char*)json.c_str(), json.length()); if (stat == yajl_status_ok) stat = yajl_complete_parse(hand); if (stat == yajl_status_client_canceled) { if (ctx_.exception_ptr) { // some sorta exception occurred. rethrow. std::rethrow_exception(ctx_.exception_ptr); } else { // error message must be in tempbuff *errout = ctx_.tempbuff; success = false; } } else if (stat != yajl_status_ok) { hand.GetError(json, errout); success = false; } if (ctx_.docid.length() == 0) { *errout = "missing _id field"; success = false; } *idout = ctx_.docid; return success; }
std::vector<char> LKStreamTranslator::process(const std::vector<char> &chunk) { std::vector<char> retval; // We work with C strings here, because YAJL const char *str = chunk.data(); size_t len = chunk.size(); // Abort if the string is empty anyways if(len == 0) return retval; // Skip any BOM if there is one const unsigned char bom[] = {0xEF, 0xBB, 0xBF}; if(len >= 3 && memcmp(bom, str, 3) == 0) { str += 3; len -= 3; } // Abort if it had nothing but a BOM if(len == 0) return retval; // KanColle specific: most datablobs are prefixed with "svdata=", that's // needed for the game to work, but we can't parse with it left in const char *svdata = "svdata="; if(memcmp(svdata, str, (size_t)std::min(strlen(svdata), len)) == 0) { str += strlen(svdata); len -= strlen(svdata); retval.insert(retval.end(), svdata, svdata + strlen(svdata)); } if(len > 0) { if(yajl_parse(ctx.parser, (const unsigned char *)str, len) != yajl_status_ok) { unsigned char *error = yajl_get_error(ctx.parser, true, (const unsigned char *)str, len); std::string error_string((const char *)error); yajl_free_error(ctx.parser, error); throw std::runtime_error("JSON Parsing Error: " + error_string); } const unsigned char *buf; size_t buf_len; yajl_gen_get_buf(ctx.gen, &buf, &buf_len); if(buf_len > 0) { retval.insert(retval.end(), buf, buf + buf_len); yajl_gen_clear(ctx.gen); } } return retval; }
static yajl_status jp_parse(JsonParserCtx *ctx, const unsigned char *s, unsigned len) { if (!ctx->yajl_handle) { ctx->yajl_handle = yajl_alloc(&yp_yajl_callbacks, NULL, static_cast<void*>(ctx)); } yajl_status status = yajl_parse(ctx->yajl_handle, s, len); return status; }
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; }
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); }
size_t parse_json(void *ptr, size_t size, size_t nmemb, void *stream) { size_t total_size = size * nmemb; yajl_handle y_handle = (yajl_handle)stream; syslog(LOG_DEBUG, "about to parse"); yajl_parse(y_handle, ptr, total_size); return total_size; }
static struct rest_json_payload * rest_get_json_upload(mtev_http_rest_closure_t *restc, int *mask, int *complete) { struct rest_json_payload *rxc; mtev_http_request *req = mtev_http_session_request(restc->http_ctx); httptrap_closure_t *ccl = NULL; int content_length; char buffer[32768]; content_length = mtev_http_request_content_length(req); rxc = restc->call_closure; rxc->check = noit_poller_lookup(rxc->check_id); if (!rxc->check) { *complete = 1; return NULL; } if(!strcmp(rxc->check->module, "httptrap")) ccl = rxc->check->closure; rxc->immediate = noit_httptrap_check_asynch(ccl ? ccl->self : NULL, rxc->check); while(!rxc->complete) { int len; len = mtev_http_session_req_consume( restc->http_ctx, buffer, MIN(content_length - rxc->len, sizeof(buffer)), sizeof(buffer), mask); if(len > 0) { yajl_status status; _YD("inbound payload chunk (%d bytes) continuing YAJL parse\n", len); status = yajl_parse(rxc->parser, (unsigned char *)buffer, len); if(status != yajl_status_ok) { unsigned char *err; *complete = 1; err = yajl_get_error(rxc->parser, 0, (unsigned char *)buffer, len); rxc->error = strdup((char *)err); yajl_free_error(rxc->parser, err); return rxc; } rxc->len += len; } if(len < 0 && errno == EAGAIN) return NULL; else if(len < 0) { *complete = 1; return NULL; } content_length = mtev_http_request_content_length(req); if((mtev_http_request_payload_chunked(req) && len == 0) || (rxc->len == content_length)) { rxc->complete = 1; _YD("no more data, finishing YAJL parse\n"); yajl_complete_parse(rxc->parser); } } *complete = 1; return rxc; }
bool hkvJsonStreamReader::Parse(const char* pszFileName, unsigned int uiChunkSize) { yajl_status stat; void* pFile; size_t rd; // Allocate read buffer unsigned char* pFileData = (unsigned char*)(YA_MALLOC(&(m_pHandle->alloc), uiChunkSize)); assert(pFileData); // Open file pFile = m_pFileHandler->Open(pszFileName, hkvJsonFileHandler::JFM_READ); if (pFile == NULL) return false; for (;;) { rd = m_pFileHandler->Read((void*)pFileData, uiChunkSize - 1, pFile); if (rd == 0) { if (!m_pFileHandler->IsEOF(pFile)) fprintf(stderr, "Error encountered on file read.\n"); break; } pFileData[rd] = 0; // Pass to parser stat = yajl_parse(m_pHandle, pFileData, rd); if (stat != yajl_status_ok) break; } // Parse any remaining buffered data stat = yajl_complete_parse(m_pHandle); if (stat != yajl_status_ok) { unsigned char * str = yajl_get_error(m_pHandle, 1, pFileData, rd); fprintf(stderr, "%s", (const char *) str); yajl_free_error(m_pHandle, str); assert(false); return false; } // Close the file m_pFileHandler->Close(pFile); // Free read buffer YA_FREE(&(m_pHandle->alloc), pFileData); return true; }
void yajl_parse_chunk(const unsigned char * chunk, unsigned int len, yajl_handle parser) { yajl_status stat; stat = yajl_parse(parser, chunk, len); if (stat != yajl_status_ok && stat != yajl_status_insufficient_data) { unsigned char * str = yajl_get_error(parser, 1, chunk, len); rb_raise(cParseError, "%s", (const char *) str); yajl_free_error(parser, str); } }
yajl_status yajl_parse_complete(yajl_handle hand) { /* The particular case we want to handle is a trailing number. * Further input consisting of digits could cause our interpretation * of the number to change (buffered "1" but "2" comes in). * A very simple approach to this is to inject whitespace to terminate * any number in the lex buffer. */ return yajl_parse(hand, (const unsigned char *)" ", 1); }
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; }
bool json_streamer_push(struct json_streamer *json, const unsigned char *data, size_t size) { yajl_status status; if (!json || !json->yajl || !data || !size) return false; status = yajl_parse(json->yajl, data, size); if (status == yajl_status_client_canceled) yajl_init(json, json->frame_callback, json->userdata, true); return (status != yajl_status_error); }
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); }
/** * Feed one chunk of data to the JSON parser. */ int json_process_chunk(modsec_rec *msr, const char *buf, unsigned int size, char **error_msg) { if (error_msg == NULL) return -1; *error_msg = NULL; /* Feed our parser and catch any errors */ msr->json->status = yajl_parse(msr->json->handle, buf, size); if (msr->json->status != yajl_status_ok) { /* We need to free the yajl error message later, how to do this? */ *error_msg = yajl_get_error(msr->json->handle, 0, buf, size); return -1; } return 1; }
/* * 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); }
/** * Read JSON data and parse it using the given YAJL parser. * @param url URL of the JSON data. * @param hand YAJL parser handle. * @return -1 on error, 0 on success. */ static int soundcloud_parse_json(const char *url, yajl_handle hand, GMutex* mutex, GCond* cond) { struct input_stream *input_stream; GError *error = NULL; char buffer[4096]; unsigned char *ubuffer = (unsigned char *)buffer; size_t nbytes; input_stream = input_stream_open(url, mutex, cond, &error); if (input_stream == NULL) { if (error != NULL) { g_warning("%s", error->message); g_error_free(error); } return -1; } g_mutex_lock(mutex); input_stream_wait_ready(input_stream); yajl_status stat; int done = 0; while (!done) { nbytes = input_stream_read(input_stream, buffer, sizeof(buffer), &error); if (nbytes == 0) { if (error != NULL) { g_warning("%s", error->message); g_error_free(error); } if (input_stream_eof(input_stream)) { done = true; } else { g_mutex_unlock(mutex); input_stream_close(input_stream); return -1; } } if (done) stat = yajl_parse_complete(hand); else stat = yajl_parse(hand, ubuffer, nbytes); if (stat != yajl_status_ok && stat != yajl_status_insufficient_data) { unsigned char *str = yajl_get_error(hand, 1, ubuffer, nbytes); g_warning("%s", str); yajl_free_error(hand, str); break; } } g_mutex_unlock(mutex); input_stream_close(input_stream); return 0; }
/* * 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; }
void json_parse_to_ei(ErlDrvData session, const unsigned char* s, int len, int opts) { int parseValue = opts & EEP0018_PARSE_VALUE; unsigned char* yajl_msg = 0; unsigned char* msg = 0; ErlDrvPort port = (ErlDrvPort) session; /* * initialize yajl parser */ State state; init(&state, opts); yajl_parser_config conf = { YAJL_ALLOW_COMMENTS }; // , YAJL_CHECK_UTF8 }; yajl_handle handle = yajl_alloc(&callbacks, &conf, &state); /* start parser */ yajl_status stat; if(parseValue) stat = yajl_parse(handle, (const unsigned char*) "[ ", 2); stat = yajl_parse(handle, s, len); if(parseValue) stat = yajl_parse(handle, (const unsigned char*) " ]", 2); /* * sometimes the parser is still stuck inside a JSON token. This finishs * the token no matter what. */ if(stat == yajl_status_insufficient_data) stat = yajl_parse(handle, (const unsigned char*) " ", 1); /* * get an error message on errors */ switch(stat) { case yajl_status_ok: break; case yajl_status_insufficient_data: msg = (unsigned char*)"Insufficient data"; break; default: msg = yajl_msg = yajl_get_error(handle, 0, s, len); break; } /* * if result is not ok: we write {error, "reason"} instead. This is * something that will never be encoded from any JSON data. */ if(msg) { ei_x_free(&state.ei_buf); ei_x_new_with_version(&state.ei_buf); ei_x_encode_tuple_header(&state.ei_buf, 2); ei_x_encode_atom_len(&state.ei_buf, "error", 5); ei_x_encode_string_len(&state.ei_buf, (const char*) msg, strlen((const char*) msg)); if(yajl_msg) yajl_free_error(yajl_msg); } send_data(port, EEP0018_EI, state.ei_buf.buff, state.ei_buf.index); deinit(&state); }
/* * 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; }