/* * 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); }
/* 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; }
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 int js_parser(lua_State *L) { yajl_handle* handle; luaL_checktype(L, 1, LUA_TTABLE); handle = (yajl_handle*)lua_newuserdata(L, sizeof(yajl_handle)); *handle = yajl_alloc(&js_parser_callbacks, NULL, (void*)L); luaL_getmetatable(L, "yajl.parser.meta"); lua_setmetatable(L, -2); lua_getfield(L, 1, "allow_comments"); if ( ! lua_isnil(L, -1) ) { yajl_config(*handle, yajl_allow_comments, lua_toboolean(L, -1)); } lua_pop(L, 1); lua_getfield(L, 1, "check_utf8"); if ( ! lua_isnil(L, -1) ) { yajl_config(*handle, yajl_dont_validate_strings, !lua_toboolean(L, -1)); } lua_pop(L, 1); lua_getfield(L, 1, "events"); /* Create callback function that calls yajl_parse[_complete]() upvalue(1) = yajl_handle* upvalue(2) = events table */ lua_pushcclosure(L, &js_parser_parse, 2); return 1; }
static J4statusPluginContext * _j4status_i3bar_output_init(J4statusCoreInterface *core) { J4statusPluginContext *context; context = g_new0(J4statusPluginContext, 1); context->core = core; context->colours.no_state = NULL; context->colours.unavailable = g_strdup("#0000FF"); context->colours.bad = g_strdup("#FF0000"); context->colours.average = g_strdup("#FFFF00"); context->colours.good = g_strdup("#00FF00"); GKeyFile *key_file; key_file = j4status_config_get_key_file("i3bar"); if ( key_file != NULL ) { _j4status_i3bar_output_update_colour(&context->colours.no_state, key_file, "NoStateColour"); _j4status_i3bar_output_update_colour(&context->colours.unavailable, key_file, "UnavailableColour"); _j4status_i3bar_output_update_colour(&context->colours.bad, key_file, "BadColour"); _j4status_i3bar_output_update_colour(&context->colours.average, key_file, "AverageColour"); _j4status_i3bar_output_update_colour(&context->colours.good, key_file, "GoodColour"); context->align = g_key_file_get_boolean(key_file, "i3bar", "Align", NULL); context->no_click_events = g_key_file_get_boolean(key_file, "i3bar", "NoClickEvents", NULL); g_key_file_free(key_file); } context->json_handle = yajl_alloc(&_j4status_i3bar_output_click_events_callbacks, NULL, context); return 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; }
/* * Document-method: new * * call-seq: new([:symbolize_keys => true, [:allow_comments => false[, :check_utf8 => false]]]) * * :symbolize_keys will turn hash keys into Ruby symbols, defaults to false. * * :allow_comments will turn on/off the check for comments inside the JSON stream, defaults to true. * * :check_utf8 will validate UTF8 characters found in the JSON stream, defaults to true. */ static VALUE rb_yajl_parser_new(int argc, VALUE * argv, VALUE klass) { yajl_parser_wrapper * wrapper; yajl_parser_config cfg; VALUE opts, obj; int allowComments = 1, checkUTF8 = 1, symbolizeKeys = 0; /* Scan off config vars */ if (rb_scan_args(argc, argv, "01", &opts) == 1) { Check_Type(opts, T_HASH); if (rb_hash_aref(opts, sym_allow_comments) == Qfalse) { allowComments = 0; } if (rb_hash_aref(opts, sym_check_utf8) == Qfalse) { checkUTF8 = 0; } if (rb_hash_aref(opts, sym_symbolize_keys) == Qtrue) { symbolizeKeys = 1; } } cfg = (yajl_parser_config){allowComments, checkUTF8}; obj = Data_Make_Struct(klass, yajl_parser_wrapper, yajl_parser_wrapper_mark, yajl_parser_wrapper_free, wrapper); wrapper->parser = yajl_alloc(&callbacks, &cfg, NULL, (void *)obj); wrapper->nestedArrayLevel = 0; wrapper->nestedHashLevel = 0; wrapper->objectsFound = 0; wrapper->symbolizeKeys = symbolizeKeys; wrapper->builderStack = rb_ary_new(); wrapper->parse_complete_callback = Qnil; rb_obj_call_init(obj, 0, 0); return obj; }
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 */
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; }
CJSONVariantParser::CJSONVariantParser(IParseCallback *callback) { m_callback = callback; #if YAJL_MAJOR == 2 m_handler = yajl_alloc(&callbacks, NULL, this); yajl_config(m_handler, yajl_allow_comments, 1); yajl_config(m_handler, yajl_dont_validate_strings, 0); #else yajl_parser_config cfg = { 1, 1 }; m_handler = yajl_alloc(&callbacks, &cfg, NULL, this); #endif m_status = ParseVariable; }
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); }
void* json_init(shttp_connection_t* conn, const yajl_callbacks * callbacks, void *ctx) { yajl_alloc_funcs funcs; funcs.malloc = &json_malloc; funcs.realloc = &json_realloc; funcs.free = &json_free; funcs.ctx = conn; return yajl_alloc(callbacks, &funcs, ctx); }
int SLNJSONFilterParserCreate(SLNSessionRef const session, SLNJSONFilterParserRef *const out) { if(!SLNSessionHasPermission(session, SLN_RDONLY)) return DB_EACCES; SLNJSONFilterParserRef parser = calloc(1, sizeof(struct SLNJSONFilterParser)); if(!parser) return DB_ENOMEM; parser->session = session; parser->JSONParser = yajl_alloc(&callbacks, NULL, parser); parser->depth = -1; *out = parser; return 0; }
CJSONVariantParser::CJSONVariantParser(IParseCallback *callback) { m_callback = callback; yajl_parser_config cfg = { 1, 1 }; m_handler = yajl_alloc(&callbacks, &cfg, NULL, this); m_status = ParseVariable; }
static int lyajl_new_parser (lua_State *L) { int r = luaL_ref(L, LUA_REGISTRYINDEX); luvit_parser_t *parser = parser_new(L); parser->ref = malloc(sizeof(luv_ref_t)); parser->ref->L = L; parser->ref->r = r; parser->handle = yajl_alloc(&lyajl_callbacks, NULL, (void*)parser->ref); luaL_getmetatable(L, JSON_PARSER_HANDLE); lua_setmetatable(L, -2); return 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; }
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 */
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); }
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); } }
/* * 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); }
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; }
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 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); }
/** * Initialise JSON parser. */ int json_init(modsec_rec *msr, char **error_msg) { /** * yajl configuration and callbacks */ static yajl_callbacks callbacks = { yajl_null, yajl_boolean, NULL /* yajl_integer */, NULL /* yajl_double */, yajl_number, yajl_string, yajl_start_map, yajl_map_key, yajl_end_map, NULL /* yajl_start_array */, NULL /* yajl_end_array */ }; if (error_msg == NULL) return -1; *error_msg = NULL; msr_log(msr, 4, "JSON parser initialization"); msr->json = apr_pcalloc(msr->mp, sizeof(json_data)); if (msr->json == NULL) return -1; /** * Prefix and current key are initially empty */ msr->json->prefix = (unsigned char *) NULL; msr->json->current_key = (unsigned char *) NULL; /** * yajl initialization * * yajl_parser_config definition: * http://lloyd.github.io/yajl/yajl-2.0.1/yajl__parse_8h.html#aec816c5518264d2ac41c05469a0f986c * * TODO: make UTF8 validation optional, as it depends on Content-Encoding */ if (msr->txcfg->debuglog_level >= 9) { msr_log(msr, 9, "yajl JSON parsing callback initialization"); } msr->json->handle = yajl_alloc(&callbacks, NULL, msr); yajl_config(msr->json->handle, yajl_allow_partial_values, 0); return 1; }
/* * 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 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); }
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; }