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; }
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 */
CJSONVariantParser::~CJSONVariantParser() { #if YAJL_MAJOR == 2 yajl_complete_parse(m_handler); #else yajl_parse_complete(m_handler); #endif yajl_free(m_handler); }
bool jsaxparser_end(jsaxparser_ref parser) { #if YAJL_VERSION < 20000 parser->status = yajl_parse_complete(parser->handle); #else parser->status = yajl_complete_parse(parser->handle); #endif return jsaxparser_process_error(parser, "", 0, true); }
/* * Document-method: parse * * call-seq: * parse(input, buffer_size=8092) * parse(input, buffer_size=8092) { |obj| ... } * * +input+ can either be a string or an IO to parse JSON from * * +buffer_size+ is the size of chunk that will be parsed off the input (if it's an IO) for each loop of the parsing process. * 8092 is a good balance between the different types of streams (off disk, off a socket, etc...), but this option * is here so the caller can better tune their parsing depending on the type of stream being passed. * A larger read buffer will perform better for files off disk, where as a smaller size may be more efficient for * reading off of a socket directly. * * If a block was passed, it's called when an object has been parsed off the stream. This is especially * usefull when parsing a stream of multiple JSON objects. * * NOTE: you can optionally assign the +on_parse_complete+ callback, and it will be called the same way the optional * block is for this method. */ static VALUE rb_yajl_parser_parse(int argc, VALUE * argv, VALUE self) { yajl_status stat; yajl_parser_wrapper * wrapper; VALUE rbufsize, input, blk; unsigned int len; const char * cptr; GetParser(self, wrapper); /* setup our parameters */ rb_scan_args(argc, argv, "11&", &input, &rbufsize, &blk); if (NIL_P(rbufsize)) { rbufsize = INT2FIX(READ_BUFSIZE); } else { Check_Type(rbufsize, T_FIXNUM); } if (!NIL_P(blk)) { rb_yajl_parser_set_complete_cb(self, blk); } if (TYPE(input) == T_STRING) { cptr = RSTRING_PTR(input); len = RSTRING_LEN(input); yajl_parse_chunk((const unsigned char*)cptr, len, wrapper->parser); } else if (rb_respond_to(input, intern_io_read)) { VALUE parsed = rb_str_new(0, FIX2LONG(rbufsize)); while (rb_funcall(input, intern_io_read, 2, rbufsize, parsed) != Qnil) { cptr = RSTRING_PTR(parsed); len = RSTRING_LEN(parsed); yajl_parse_chunk((const unsigned char*)cptr, len, wrapper->parser); } } else { rb_raise(cParseError, "input must be a string or IO"); } /* parse any remaining buffered data */ stat = yajl_parse_complete(wrapper->parser); if (wrapper->parse_complete_callback != Qnil) { yajl_check_and_fire_callback((void *)self); return Qnil; } // Because we've overridden the yajl_parse_complete state of the parser to allow // us to parse multiple objects out of one stream; we can't use it to determine // whether we've parsed a single object. Instead we'll check whether we've successfully // parsed a single token. if (!RARRAY_LEN(wrapper->builderStack) || wrapper->nestedHashLevel || wrapper->nestedArrayLevel) { rb_raise(cParseError, "unexpected end of JSON string"); } return rb_ary_pop(wrapper->builderStack); }
yajl_status ajv_parse_complete(ajv_handle hand) { yajl_status stat = yajl_parse_complete(hand->yajl); if (stat == yajl_status_ok || stat == yajl_status_insufficient_data) { if ( hand->s && !ajv_state_finished(hand) ) { ajv_set_error(hand, ajv_e_incomplete_container, NULL, "Empty root", strlen("Empty root")); stat = yajl_status_error; } } return stat; }
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 */
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; }
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; }
RC::ConstHandle<Value> parse( char const *data, unsigned len ) { yajl_status yajlStatus; RC::ConstHandle<Value> result; m_contextStack.push_back( new DirectContext( result ) ); yajlStatus = yajl_parse( m_yajlHandle, (const unsigned char *)data, len ); if ( yajlStatus != yajl_status_ok ) throw Exception( "JSON parser error: " + std::string( yajl_status_to_string( yajlStatus ) ) ); yajlStatus = yajl_parse_complete( m_yajlHandle ); if ( yajlStatus != yajl_status_ok ) throw Exception( "JSON parser error: " + std::string( yajl_status_to_string( yajlStatus ) ) ); Context *lastContext = m_contextStack.back(); m_contextStack.pop_back(); delete lastContext; return result; }
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; }
/* * Document-method: parse * * call-seq: * parse(input, buffer_size=8092) * parse(input, buffer_size=8092) { |obj| ... } * * +input+ can either be a string or an IO to parse JSON from * * +buffer_size+ is the size of chunk that will be parsed off the input (if it's an IO) for each loop of the parsing process. * 8092 is a good balance between the different types of streams (off disk, off a socket, etc...), but this option * is here so the caller can better tune their parsing depending on the type of stream being passed. * A larger read buffer will perform better for files off disk, where as a smaller size may be more efficient for * reading off of a socket directly. * * If a block was passed, it's called when an object has been parsed off the stream. This is especially * usefull when parsing a stream of multiple JSON objects. * * NOTE: you can optionally assign the +on_parse_complete+ callback, and it will be called the same way the optional * block is for this method. */ static VALUE rb_yajl_parser_parse(int argc, VALUE * argv, VALUE self) { yajl_status stat; yajl_parser_wrapper * wrapper; VALUE rbufsize, input, blk; unsigned int len; const char * cptr; GetParser(self, wrapper); /* setup our parameters */ rb_scan_args(argc, argv, "11&", &input, &rbufsize, &blk); if (NIL_P(rbufsize)) { rbufsize = INT2FIX(READ_BUFSIZE); } else { Check_Type(rbufsize, T_FIXNUM); } if (!NIL_P(blk)) { rb_yajl_parser_set_complete_cb(self, blk); } if (TYPE(input) == T_STRING) { cptr = RSTRING_PTR(input); len = RSTRING_LEN(input); yajl_parse_chunk((const unsigned char*)cptr, len, wrapper->parser); } else if (rb_respond_to(input, intern_io_read)) { VALUE parsed = rb_str_new(0, FIX2LONG(rbufsize)); while (rb_funcall(input, intern_io_read, 2, rbufsize, parsed) != Qnil) { cptr = RSTRING_PTR(parsed); len = RSTRING_LEN(parsed); yajl_parse_chunk((const unsigned char*)cptr, len, wrapper->parser); } } else { rb_raise(cParseError, "input must be a string or IO"); } /* parse any remaining buffered data */ stat = yajl_parse_complete(wrapper->parser); if (wrapper->parse_complete_callback != Qnil) { yajl_check_and_fire_callback((void *)self); return Qnil; } return rb_ary_pop(wrapper->builderStack); }
static long chf_parse(dbChannel *chan, const char **pjson) { parseContext parser = { chan, NULL, 0 }; yajl_handle yh = yajl_alloc(&chf_callbacks, &chf_config, &chf_alloc, &parser); const char *json = *pjson; size_t jlen = strlen(json); yajl_status ys; long status; if (!yh) return S_db_noMemory; ys = yajl_parse(yh, (const unsigned char *) json, (unsigned int) jlen); if (ys == yajl_status_insufficient_data) ys = yajl_parse_complete(yh); switch (ys) { case yajl_status_ok: status = 0; *pjson += yajl_get_bytes_consumed(yh); break; case yajl_status_error: { unsigned char *err; err = yajl_get_error(yh, 1, (const unsigned char *) json, (unsigned int) jlen); printf("dbChannelCreate: %s\n", err); yajl_free_error(yh, err); } /* fall through */ default: status = S_db_notFound; } if (parser.filter) { assert(status); parser.filter->plug->fif->parse_abort(parser.filter); freeListFree(chFilterFreeList, parser.filter); } yajl_free(yh); return status; }
PyObject *_internal_decode(_YajlDecoder *self, char *buffer, unsigned int buflen) { yajl_handle parser = NULL; yajl_status yrc; yajl_parser_config config = { 1, 1 }; PyObject *root; if (self->elements.used > 0) { py_yajl_ps_free(self->elements); py_yajl_ps_init(self->elements); } if (self->keys.used > 0) { py_yajl_ps_free(self->keys); py_yajl_ps_init(self->keys); } /* callbacks, config, allocfuncs */ parser = yajl_alloc(&decode_callbacks, &config, NULL, (void *)(self)); yrc = yajl_parse(parser, (const unsigned char *)(buffer), buflen); yajl_parse_complete(parser); yajl_free(parser); if (yrc != yajl_status_ok) { PyErr_SetObject(PyExc_ValueError, PyUnicode_FromString(yajl_status_to_string(yrc))); return NULL; } if (self->root == NULL) { PyErr_SetObject(PyExc_ValueError, PyUnicode_FromString("The root object is NULL")); return NULL; } // Callee now owns memory, we'll leave refcnt at one and // null out our pointer. root = self->root; self->root = NULL; return root; }
static package bf_parse_json(Var arglist, Byte next, void *vdata, Objid progr) { yajl_handle hand; yajl_parser_config cfg = { 1, 1 }; yajl_status stat; struct parse_context pctx; pctx.top = &pctx.stack; pctx.stack.v.type = TYPE_INT; pctx.stack.v.v.num = 0; pctx.mode = MODE_COMMON_SUBSET; const char *str = arglist.v.list[1].v.str; size_t len = strlen(str); package pack; int done = 0; if (1 < arglist.v.list[0].v.num) { if (!mystrcasecmp(arglist.v.list[2].v.str, "common-subset")) { pctx.mode = MODE_COMMON_SUBSET; } else if (!mystrcasecmp(arglist.v.list[2].v.str, "embedded-types")) { pctx.mode = MODE_EMBEDDED_TYPES; } else { free_var(arglist); return make_error_pack(E_INVARG); } } hand = yajl_alloc(&callbacks, &cfg, NULL, (void *)&pctx); while (!done) { if (len == 0) done = 1; if (done) stat = yajl_parse_complete(hand); else stat = yajl_parse(hand, (const unsigned char *)str, len); len = 0; if (done) { if (stat != yajl_status_ok) { /* clean up the stack */ while (pctx.top != &pctx.stack) { Var v = POP(pctx.top); free_var(v); } pack = make_error_pack(E_INVARG); } else { Var v = POP(pctx.top); pack = make_var_pack(v); } } } yajl_free(hand); free_var(arglist); return pack; }
ERL_NIF_TERM reverse_tokens(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) { decode_ctx ctx; yajl_parser_config conf = {0, 1}; // No comments, check utf8 yajl_handle handle = yajl_alloc(&decoder_callbacks, &conf, NULL, &ctx); yajl_status status; unsigned int used; ErlNifBinary bin; ERL_NIF_TERM ret; ctx.env = env; ctx.head = enif_make_list_from_array(env, NULL, 0); if(!enif_inspect_iolist_as_binary(env, argv[0], &bin)) { ret = enif_make_badarg(env); goto done; } status = yajl_parse(handle, bin.data, bin.size); used = handle->bytesConsumed; // Parsing something like "2.0" (without quotes) will // cause a spurious semi-error. We add the extra size // check so that "2008-20-10" doesn't pass. if(status == yajl_status_insufficient_data && used == bin.size) { status = yajl_parse_complete(handle); } if(status == yajl_status_ok && used != bin.size) { if(check_rest(bin.data, bin.size, used) == CANCEL) { ret = enif_make_tuple(env, 2, enif_make_atom(env, "error"), enif_make_atom(env, "garbage_after_value") ); goto done; } } switch(status) { case yajl_status_ok: ret = enif_make_tuple(env, 2, enif_make_atom(env, "ok"), ctx.head); goto done; case yajl_status_error: ret = make_error(handle, env); goto done; case yajl_status_insufficient_data: ret = enif_make_tuple(env, 2, enif_make_atom(env, "error"), enif_make_atom(env, "insufficient_data") ); goto done; case yajl_status_client_canceled: /* the only time we do this is when we can't allocate a binary. */ ret = enif_make_tuple(env, 2, enif_make_atom(env, "error"), enif_make_atom(env, "insufficient_memory") ); goto done; default: ret = enif_make_tuple(env, 2, enif_make_atom(env, "error"), enif_make_atom(env, "unknown") ); goto done; } done: if(handle != NULL) yajl_free(handle); return ret; }
int main(int argc, char ** argv) { yajl_handle hand; const char * fileName; static unsigned char * fileData = NULL; unsigned int bufSize = BUF_SIZE; yajl_status stat; size_t rd; yajl_parser_config cfg = { 0, 1 }; int i, j, done; /* memory allocation debugging: allocate a structure which collects * statistics */ yajlTestMemoryContext memCtx = { 0,0 }; /* memory allocation debugging: allocate a structure which holds * allocation routines */ yajl_alloc_funcs allocFuncs = { yajlTestMalloc, yajlTestRealloc, yajlTestFree, (void *) NULL }; allocFuncs.ctx = (void *) &memCtx; /* check arguments. We expect exactly one! */ for (i=1;i<argc;i++) { if (!strcmp("-c", argv[i])) { cfg.allowComments = 1; } else if (!strcmp("-b", argv[i])) { if (++i >= argc) usage(argv[0]); /* validate integer */ for (j=0;j<(int)strlen(argv[i]);j++) { if (argv[i][j] <= '9' && argv[i][j] >= '0') continue; fprintf(stderr, "-b requires an integer argument. '%s' " "is invalid\n", argv[i]); usage(argv[0]); } bufSize = atoi(argv[i]); if (!bufSize) { fprintf(stderr, "%d is an invalid buffer size\n", bufSize); } } else { fprintf(stderr, "invalid command line option: '%s'\n", argv[i]); usage(argv[0]); } } fileData = (unsigned char *) malloc(bufSize); if (fileData == NULL) { fprintf(stderr, "failed to allocate read buffer of %u bytes, exiting.", bufSize); exit(2); } fileName = argv[argc-1]; /* ok. open file. let's read and parse */ hand = yajl_alloc(&callbacks, &cfg, &allocFuncs, NULL); done = 0; while (!done) { rd = fread((void *) fileData, 1, bufSize, stdin); if (rd == 0) { if (!feof(stdin)) { fprintf(stderr, "error reading from '%s'\n", fileName); break; } done = 1; } 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_insufficient_data && stat != yajl_status_ok) { unsigned char * str = yajl_get_error(hand, 0, fileData, rd); fflush(stdout); fprintf(stderr, (char *) str); yajl_free_error(hand, str); break; } } yajl_free(hand); free(fileData); /* finally, print out some memory statistics */ /* (lth) only print leaks here, as allocations and frees may vary depending * on read buffer size, causing false failures. * * printf("allocations:\t%u\n", memCtx.numMallocs); * printf("frees:\t\t%u\n", memCtx.numFrees); */ fflush(stderr); fflush(stdout); printf("memory leaks:\t%u\n", memCtx.numMallocs - memCtx.numFrees); return 0; }
int main(int argc, char ** argv) { yajl_handle hand; static unsigned char fileData[65536]; /* generator config */ yajl_gen_config conf = { 1, " " }; yajl_gen g; yajl_status stat; size_t rd; /* allow comments */ yajl_parser_config cfg = { 1, 1 }; int retval = 0, done = 0; /* check arguments.*/ int a = 1; while ((a < argc) && (argv[a][0] == '-') && (strlen(argv[a]) > 1)) { int i; for ( i=1; i < strlen(argv[a]); i++) { switch (argv[a][i]) { case 'm': conf.beautify = 0; break; case 'u': cfg.checkUTF8 = 0; break; default: fprintf(stderr, "unrecognized option: '%c'\n\n", argv[a][i]); usage(argv[0]); } } ++a; } if (a < argc) { usage(argv[0]); } g = yajl_gen_alloc(&conf, NULL); /* 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, stdin); if (rd == 0) { if (!feof(stdin)) { fprintf(stderr, "error on file read.\n"); retval = 1; 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 = 1; break; } 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 retval; }
CJSONVariantParser::~CJSONVariantParser() { yajl_parse_complete(m_handler); }
int main(int argc, char ** argv) { yajl_status stat; size_t rd; yajl_handle hand; static unsigned char fileData[65536]; int quiet = 0; int retval = 0, done = 0; yajl_parser_config cfg = { 0, 1 }; /* check arguments.*/ int a = 1; while ((a < argc) && (argv[a][0] == '-') && (strlen(argv[a]) > 1)) { size_t i; for ( i=1; i < strlen(argv[a]); i++) { switch (argv[a][i]) { case 'q': quiet = 1; break; case 'c': cfg.allowComments = 1; break; case 'u': cfg.checkUTF8 = 0; break; default: fprintf(stderr, "unrecognized option: '%c'\n\n", argv[a][i]); usage(argv[0]); } } ++a; } if (a < argc) { usage(argv[0]); } /* allocate a parser */ hand = yajl_alloc(NULL, &cfg, NULL, NULL); while (!done) { rd = fread((void *) fileData, 1, sizeof(fileData) - 1, stdin); retval = 0; if (rd == 0) { if (!feof(stdin)) { if (!quiet) { fprintf(stderr, "error encountered on file read\n"); } retval = 1; 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) { if (!quiet) { unsigned char * str = yajl_get_error(hand, 1, fileData, rd); fprintf(stderr, "%s", (const char *) str); yajl_free_error(hand, str); } retval = 1; break; } } yajl_free(hand); if (!quiet) { printf("JSON is %s\n", retval ? "invalid" : "valid"); } return retval; }
Validator* parse_schema_n(char const *str, size_t len, UriResolver *uri_resolver, char const *root_scope, JschemaErrorFunc error_func, void *error_ctxt) { //JsonSchemaParserTrace(stdout, ">>> "); void *parser = JsonSchemaParserAlloc(malloc); YajlContext yajl_context = { .parser = parser, .parser_ctxt = { .validator = NULL, .error = SEC_OK, }, }; const bool allow_comments = true; #if YAJL_VERSION < 20000 yajl_parser_config yajl_opts = { allow_comments, 0, }; yajl_handle yh = yajl_alloc(&callbacks, &yajl_opts, NULL, &yajl_context); #else yajl_handle yh = yajl_alloc(&callbacks, NULL, &yajl_context); yajl_config(yh, yajl_allow_comments, allow_comments ? 1 : 0); yajl_config(yh, yajl_dont_validate_strings, 1); #endif // YAJL_VERSION if (!yh) { JsonSchemaParserFree(parser, free); return NULL; } if (yajl_status_ok != yajl_parse(yh, (const unsigned char *)str, len)) { if (yajl_context.parser_ctxt.error == SEC_OK) { unsigned char *err = yajl_get_error(yh, 0/*verbose*/, (const unsigned char *)str, len); if (error_func) error_func(yajl_get_bytes_consumed(yh), SEC_SYNTAX, (const char *) err, error_ctxt); yajl_free_error(yh, err); } else { if (error_func) error_func(yajl_get_bytes_consumed(yh), yajl_context.parser_ctxt.error, SchemaGetErrorMessage(yajl_context.parser_ctxt.error), error_ctxt); } yajl_free(yh); JsonSchemaParserFree(parser, free); return NULL; } #if YAJL_VERSION < 20000 if (yajl_status_ok != yajl_parse_complete(yh)) #else if (yajl_status_ok != yajl_complete_parse(yh)) #endif { if (yajl_context.parser_ctxt.error == SEC_OK) { unsigned char *err = yajl_get_error(yh, 0, (const unsigned char *)str, len); if (error_func) error_func(yajl_get_bytes_consumed(yh), SEC_SYNTAX, (const char *) err, error_ctxt); yajl_free_error(yh, err); } else { if (error_func) error_func(yajl_get_bytes_consumed(yh), yajl_context.parser_ctxt.error, SchemaGetErrorMessage(yajl_context.parser_ctxt.error), error_ctxt); } yajl_free(yh); JsonSchemaParserFree(parser, free); return NULL; } // Let the parser finish its job. static TokenParam token_param; JsonSchemaParser(parser, 0, token_param, &yajl_context.parser_ctxt); // Even if parsing was completed there can be an error if (!yajl_context.parser_ctxt.error == SEC_OK) { if (error_func) error_func(yajl_get_bytes_consumed(yh), yajl_context.parser_ctxt.error, SchemaGetErrorMessage(yajl_context.parser_ctxt.error), error_ctxt); validator_unref(yajl_context.parser_ctxt.validator); yajl_free(yh); JsonSchemaParserFree(parser, free); return NULL; } yajl_free(yh); JsonSchemaParserFree(parser, free); // Post-parse processing Validator *v = yajl_context.parser_ctxt.validator; // Move parsed features to the validators validator_apply_features(v); // Combine type validator and other validators contaiters (allOf, anyOf, etc.) validator_combine(v); if (uri_resolver) validator_collect_uri(v, root_scope, uri_resolver); // Substitute every SchemaParsing by its type validator for every node // in the AST. Validator *result = validator_finalize_parse(v); // Forget about SchemaParsing then. validator_unref(v); return result; }
orderly_json * orderly_read_json(orderly_alloc_funcs * alloc, const char * jsonText, unsigned int * len) { static yajl_callbacks callbacks = { o_json_parse_null, o_json_parse_boolean, o_json_parse_integer, o_json_parse_double, NULL, o_json_parse_string, o_json_parse_start_map, o_json_parse_map_key, o_json_parse_end_map, o_json_parse_start_array, o_json_parse_end_array }; yajl_handle hand; yajl_status stat; /* allow comments! */ yajl_parser_config cfg = { 1, 1 }; o_json_parse_context pc; orderly_json * j = NULL; memset((void *) &pc, 0, sizeof(pc)); pc.alloc = alloc; /* allocate a parser */ hand = yajl_alloc(&callbacks, &cfg, (const yajl_alloc_funcs *) alloc, (void *) &pc); /* read file data, pass to parser */ stat = yajl_parse(hand, (const unsigned char *) jsonText, *len); *len = yajl_get_bytes_consumed(hand); if (stat == yajl_status_insufficient_data) { stat = yajl_parse_complete(hand); } if (stat != yajl_status_ok) { /* unsigned char * str = yajl_get_error(hand, 1, (const unsigned char *) jsonText, *len); */ /* fprintf(stderr, (const char *) str); */ /* yajl_free_error(hand, str); */ } else if (!orderly_ps_length(pc.nodeStack)) { /* XXX: ERROR! */ } else { /* we're ok! */ j = orderly_ps_current(pc.nodeStack); } yajl_free(hand); orderly_ps_free(alloc, pc.nodeStack); orderly_ps_free(alloc, pc.keyStack); return j; }
/** * Initiate JSON parsing and print error if one occurs */ static int cconn_process_json(struct cconn *io) { if((io->request_type != ASOK_REQ_DATA) && (io->request_type != ASOK_REQ_SCHEMA)) { return -EDOM; } int result = 1; yajl_handle hand; yajl_status status; hand = yajl_alloc(&callbacks, #if HAVE_YAJL_V2 /* alloc funcs = */ NULL, #else /* alloc funcs = */ NULL, NULL, #endif /* context = */ (void *)(&io->yajl)); if(!hand) { ERROR ("ceph plugin: yajl_alloc failed."); return ENOMEM; } io->yajl.depth = 0; switch(io->request_type) { case ASOK_REQ_DATA: io->yajl.handler = node_handler_fetch_data; result = cconn_process_data(io, &io->yajl, hand); break; case ASOK_REQ_SCHEMA: //init daemon specific variables io->d->ds_num = 0; io->d->last_idx = 0; io->d->last_poll_data = NULL; io->yajl.handler = node_handler_define_schema; io->yajl.handler_arg = io->d; result = traverse_json(io->json, io->json_len, hand); break; } if(result) { goto done; } #if HAVE_YAJL_V2 status = yajl_complete_parse(hand); #else status = yajl_parse_complete(hand); #endif if (status != yajl_status_ok) { unsigned char *errmsg = yajl_get_error (hand, /* verbose = */ 0, /* jsonText = */ NULL, /* jsonTextLen = */ 0); ERROR ("ceph plugin: yajl_parse_complete failed: %s", (char *) errmsg); yajl_free_error (hand, errmsg); yajl_free (hand); return 1; } done: yajl_free (hand); return result; }
int main(int argc, char ** argv) { yajl_handle hand; static unsigned char fileData[65536]; /* generator config */ yajl_gen_config conf = { 1, " " }; yajl_gen g; yajl_status stat; size_t rd; /* allow comments */ yajl_parser_config cfg = { 1, 1 }; int done = 0; /* check arguments. We expect exactly one! */ if (argc == 2) { if (!strcmp("-m", argv[1])) { conf.beautify = 0; } else if (!strcmp("-u", argv[1])) { cfg.checkUTF8 = 0; } else { usage(argv[0]); } } else if (argc != 1) { usage(argv[0]); } g = yajl_gen_alloc(&conf, NULL); /* 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, 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; }
/** * 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; }
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 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); }