bool Parser::parseFile(const string &filename) { FILE *f = fopen(filename.c_str(), "rt"); if (!f) return false; jsonsl_t jsn = jsonsl_new(0x1000); jsonsl_enable_all_callbacks(jsn); jsn->action_callback = jsonslAction; jsn->error_callback = jsonslError; Parser parser; jsn->data = &parser; const size_t bufferSize = 4096; char buffer[bufferSize]; for (;;) { size_t n = fread(buffer,1,bufferSize,f); jsonsl_feed(jsn, buffer, n); if (n<bufferSize) break; } jsonsl_destroy(jsn); fclose(f); return true; }
void lcbvrow_reset(lcbvrow_PARSER* ctx) { /** * We create a copy, and set its relevant fields. All other * fields are zeroed implicitly. Then we copy the object back. */ jsonsl_reset(ctx->jsn); jsonsl_reset(ctx->jsn_rdetails); lcb_string_clear(&ctx->current_buf); lcb_string_clear(&ctx->meta_buf); lcb_string_clear(&ctx->last_hk); /* Initially all callbacks are enabled so that we can search for the * rows array. */ ctx->jsn->action_callback_POP = initial_pop_callback; ctx->jsn->action_callback_PUSH = initial_push_callback; ctx->jsn->error_callback = parse_error_callback; ctx->jsn->max_callback_level = 4; ctx->jsn->data = ctx; jsonsl_enable_all_callbacks(ctx->jsn); ctx->have_error = 0; ctx->initialized = 0; ctx->meta_complete = 0; ctx->rowcount = 0; ctx->min_pos = 0; ctx->keep_pos = 0; ctx->header_len = 0; ctx->last_row_endpos = 0; }
bson_t * sepia_read_json(struct sepia_request * request, int * error) { jsonsl_t parser = jsonsl_new(MAX_NESTING_LEVEL); jsonsl_enable_all_callbacks(parser); parser->action_callback = on_stack_change; parser->error_callback = on_error; char * buffer = GC_MALLOC(BUFFER_SIZE); struct bson_state state; state.cur_entry = -1; state.error = JSONSL_ERROR_SUCCESS; state.entry[0].bson = NULL; state.text = buffer; parser->data = &state; int read; do { read = sepia_read_data(request, buffer, BUFFER_SIZE); jsonsl_feed(parser, buffer, read); } while (read > 0); if (error != NULL) { * error = state.error; } jsonsl_destroy(parser); return (state.cur_entry == -1 && state.error == JSONSL_ERROR_SUCCESS) ? state.entry[0].bson : NULL; }
bool Parser::parseString(const char *json) { jsonsl_t jsn = jsonsl_new(0x1000); jsonsl_enable_all_callbacks(jsn); jsn->action_callback = jsonslAction; jsn->error_callback = jsonslError; Parser parser; jsn->data = &parser; jsonsl_feed(jsn, json, strlen(json)); jsonsl_destroy(jsn); return true; }
void lcbvrow_parse_row(lcbvrow_PARSER *vp, lcbvrow_ROW *vr) { miniparse_ctx ctx = { NULL }; ctx.datum = vr; ctx.root = vr->row.iov_base; jsonsl_reset(vp->jsn_rdetails); jsonsl_enable_all_callbacks(vp->jsn_rdetails); vp->jsn_rdetails->max_callback_level = 3; vp->jsn_rdetails->action_callback_POP = miniparse_callback; vp->jsn_rdetails->data = &ctx; jsonsl_feed(vp->jsn_rdetails, vr->row.iov_base, vr->row.iov_len); }
void lcbex_vrow_reset(lcbex_vrow_ctx_t* ctx) { /** * We create a copy, and set its relevant fields. All other * fields are zeroed implicitly. Then we copy the object back. */ lcbex_vrow_ctx_t ctx_copy = { 0 }; jsonsl_reset(ctx->jsn); buffer_reset(&ctx->current_buf, 0); buffer_reset(&ctx->meta_buf, 0); buffer_reset(&ctx->last_hk, 0); /** * Initially all callbacks are enabled so that we can search for the * rows array. */ ctx->jsn->action_callback_POP = initial_pop_callback; ctx->jsn->action_callback_PUSH = initial_push_callback; ctx->jsn->error_callback = parse_error_callback; ctx->jsn->max_callback_level = 4; ctx->jsn->data = ctx; jsonsl_enable_all_callbacks(ctx->jsn); ctx_copy.jsn = ctx->jsn; ctx_copy.user_cookie = ctx->user_cookie; ctx_copy.callback = ctx->callback; ctx_copy.jpr = ctx->jpr; ctx_copy.current_buf = ctx->current_buf; ctx_copy.meta_buf = ctx->meta_buf; ctx_copy.last_hk = ctx->last_hk; *ctx = ctx_copy; }