static void feed_data(lcbex_vrow_ctx_t *ctx, const char *data, size_t ndata) { size_t old_len = ctx->current_buf.len; buffer_append(&ctx->current_buf, data, ndata); jsonsl_feed(ctx->jsn, ctx->current_buf.s + old_len, ndata); /** * Do we need to cut off some bytes? */ if (ctx->keep_pos > ctx->min_pos) { size_t lentmp, diff = ctx->keep_pos - ctx->min_pos; const char *buf = get_buffer_region(ctx, ctx->keep_pos, -1, &lentmp); memmove(ctx->current_buf.s, buf, ctx->current_buf.len - diff); ctx->current_buf.len -= diff; } ctx->min_pos = ctx->keep_pos; }
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; }
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); }
static void feed_data(lcbjsp_PARSER *ctx, const char *data, size_t ndata) { size_t old_len = ctx->current_buf.nused; buffer_append(&ctx->current_buf, data, ndata); jsonsl_feed(ctx->jsn, ctx->current_buf.base + old_len, ndata); /* Do we need to cut off some bytes? */ if (ctx->keep_pos > ctx->min_pos) { size_t lentmp, diff = ctx->keep_pos - ctx->min_pos; const char *buf = get_buffer_region(ctx, ctx->keep_pos, -1, &lentmp); memmove(ctx->current_buf.base, buf, ctx->current_buf.nused - diff); ctx->current_buf.nused -= diff; } ctx->min_pos = ctx->keep_pos; }