lcbvrow_PARSER* lcbvrow_create(void) { lcbvrow_PARSER *ctx; jsonsl_error_t err; ctx = calloc(1, sizeof(*ctx)); ctx->jsn = jsonsl_new(512); ctx->jpr = jsonsl_jpr_new("/rows/^", &err); ctx->jsn_rdetails = jsonsl_new(32); lcb_string_init(&ctx->meta_buf); lcb_string_init(&ctx->current_buf); lcb_string_init(&ctx->last_hk); if (!ctx->jpr) { abort(); } if (!ctx->jsn_rdetails) { abort(); } jsonsl_jpr_match_state_init(ctx->jsn, &ctx->jpr, 1); assert(ctx->jsn_rdetails); lcbvrow_reset(ctx); assert(ctx->jsn_rdetails); return ctx; }
lcbjsp_PARSER* lcbjsp_create(int mode) { lcbjsp_PARSER *ctx; jsonsl_error_t err; ctx = calloc(1, sizeof(*ctx)); ctx->jsn = jsonsl_new(512); ctx->mode = mode; if (ctx->mode == LCBJSP_MODE_VIEWS) { ctx->jpr = jsonsl_jpr_new("/rows/^", &err); } else { ctx->jpr = jsonsl_jpr_new("/results/^", &err); } ctx->jsn_rdetails = jsonsl_new(32); lcb_string_init(&ctx->meta_buf); lcb_string_init(&ctx->current_buf); lcb_string_init(&ctx->last_hk); if (!ctx->jpr) { abort(); } if (!ctx->jsn_rdetails) { abort(); } jsonsl_jpr_match_state_init(ctx->jsn, &ctx->jpr, 1); assert(ctx->jsn_rdetails); lcbjsp_reset(ctx); assert(ctx->jsn_rdetails); return ctx; }
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; }
lcbex_vrow_ctx_t* lcbex_vrow_create(void) { lcbex_vrow_ctx_t *ctx; jsonsl_error_t err; ctx = calloc(1, sizeof(*ctx)); ctx->jsn = jsonsl_new(512); ctx->jpr = jsonsl_jpr_new("/rows/^", &err); if (!ctx->jpr) { abort(); } jsonsl_jpr_match_state_init(ctx->jsn, &ctx->jpr, 1); lcbex_vrow_reset(ctx); return ctx; }