virtual StringResultBase* SaxRoundtrip(const char* json, size_t length) const { VinenthzStringResult* sr = new VinenthzStringResult(); json_printer printer; json_print_init(&printer, string_buffer_append, &sr->sb); json_config config; memset(&config, 0, sizeof(json_config)); config.max_nesting = 0; config.max_data = 0; config.allow_c_comments = 0; config.allow_yaml_comments = 0; json_parser parser; json_parser_init(&parser, &config, (json_parser_callback)json_print_raw, &printer); uint32_t processed; if (json_parser_string(&parser, json, (uint32_t)length, &processed) == 0) sr->AppendEnds(); else { delete sr; sr = 0; } json_parser_free(&parser); json_print_free(&printer); return sr; }
int parse(const std::string& str, uint32_t& line, uint32_t& column) { if (err_code_ != 0) { return err_code_; } uint32_t processed = 0; err_code_ = json_parser_string(&parser_, str.c_str(), static_cast<uint32_t>(str.size()), &processed); if (err_code_ || !json_parser_is_done(&parser_)) { line = 1; column = 1; for (uint32_t i = 0; i < processed; ++i) { if (str[i] == '\n') { ++line; column = 1; } else { ++column; } } err_code_ = err_code_ ? err_code_ : JSON_ERROR_UTF8 + 1; } return err_code_; }
virtual ParseResultBase* Parse(const char* json, size_t length) const { VinenthzParseResult* pr = new VinenthzParseResult; json_config config; memset(&config, 0, sizeof(json_config)); config.max_nesting = 0; config.max_data = 0; config.allow_c_comments = 0; config.allow_yaml_comments = 0; json_parser parser; json_parser_dom dom; json_parser_dom_init(&dom, tree_create_structure, tree_create_data, tree_append); json_parser_init(&parser, &config, json_parser_dom_callback, &dom); uint32_t processed; if (!json_parser_string(&parser, json, (uint32_t)length, &processed)) pr->root = (json_val_t*)dom.root_structure; else { delete pr; pr = 0; } json_parser_free(&parser); json_parser_dom_free(&dom); return pr; }
int process_file(json_parser *parser, FILE *input, int *retlines, int *retcols) { char buffer[4096]; int ret = 0; int32_t read; int lines, col, i; lines = 1; col = 0; while (1) { uint32_t processed; read = fread(buffer, 1, 4096, input); if (read <= 0) break; ret = json_parser_string(parser, buffer, read, &processed); for (i = 0; i < processed; i++) { if (buffer[i] == '\n') { col = 0; lines++; } else col++; } if (ret) break; } if (retlines) *retlines = lines; if (retcols) *retcols = col; return ret; }
xmmsv_t * xmmsv_from_json (const char *spec) { json_config conf = { 0, /* buffer_initial_size (0=default) */ 0, /* max_nesting (0=no limit) */ 0, /* max_data (0=no limit) */ 1, /* allow_c_comments */ 0, /* allow_yaml_comments */ NULL, /* user_calloc */ NULL /* user_realloc */ }; json_parser_dom dom; json_parser parser; xmmsv_t *value; int error; json_parser_dom_init (&dom, (json_parser_dom_create_structure) create_structure, (json_parser_dom_create_data) create_data, (json_parser_dom_append) append); json_parser_init (&parser, &conf, json_parser_dom_callback, &dom); error = json_parser_string (&parser, spec, strlen (spec), NULL); if (error != 0) { switch (error) { case JSON_ERROR_BAD_CHAR: fprintf (stderr, "Failed to parse due to bad character!\n"); break; case JSON_ERROR_UNEXPECTED_CHAR: fprintf (stderr, "Failed to parse due to unexpected character!\n"); break; case JSON_ERROR_NO_MEMORY: fprintf (stderr, "Failed to parse (%d)!\n", error); break; } return NULL; } assert (dom.root_structure != NULL); assert (dom.stack_offset == 0); value = (xmmsv_t *) dom.root_structure; json_parser_dom_free (&dom); json_parser_free (&parser); return value; }
int parse(std::istream& stream, uint32_t &line, uint32_t &column) { if (err_code_ != 0) { return err_code_; } char buffer[4096]; line = 1; column = 1; for ( ; ; ) { stream.read(buffer, sizeof(buffer)); std::streamsize read = stream.gcount(); if (read <= 0) { break; } uint32_t processed = 0; err_code_ = json_parser_string(&parser_, buffer, static_cast<uint32_t>(read), &processed); // check for early return if (!err_code_ && json_parser_is_done(&parser_)) { return err_code_; } for (uint32_t i = 0; i < processed; ++i) { if (buffer[i] == '\n') { ++line; column = 1; } else { ++column; } } if (err_code_) { break; } } if (err_code_ || !json_parser_is_done(&parser_)) { err_code_ = err_code_ ? err_code_ : JSON_ERROR_UTF8 + 1; } return err_code_; }
static int jsonParse(json_parser *parser, json_printer *printer, char* buf, int len, int done) { int ret; u32 processed; //printf("jsonParse: %s, len=%d\n", buf, len); ret = json_parser_string(parser, buf, len, &processed); if(ret) { printf("jsonParse: Error ret=%d\n", ret); } if(done) { json_parser_free(parser); json_print_free(printer); } return ret; }
static int parse_clfjson(void *ctx, str_t *str, logmeta_t *meta) { clfjson_state_t state = {0, 0, 0, str, meta}; json_parser parser; size_t processed = 0; logmeta_clear(meta); logmeta_hash(meta, str); json_parser_init(&parser, NULL, clfjson_state, &state); json_parser_string(&parser, (const char *)str->ptr, str->len, &processed); str_ptime_rfc1123(logmeta_field(meta, LOGPIPE_TIMESTAMP), &meta->utc_timestamp); json_parser_free(&parser); return ! logmeta_field_isempty(meta, LOGPIPE_C_IP) && ! logmeta_field_isempty(meta, LOGPIPE_TIMESTAMP) && ! logmeta_field_isempty(meta, LOGPIPE_CS_METHOD) && ! logmeta_field_isempty(meta, LOGPIPE_CS_URI_STEM) && ! logmeta_field_isempty(meta, LOGPIPE_SC_STATUS); }
virtual bool SaxStatistics(const char* json, size_t length, Stat* stat) const { json_config config; memset(&config, 0, sizeof(json_config)); config.max_nesting = 0; config.max_data = 0; config.allow_c_comments = 0; config.allow_yaml_comments = 0; memset(stat, 0, sizeof(Stat)); StatContext context; context.stat = stat; context.after_key = true; json_parser parser; json_parser_init(&parser, &config, (json_parser_callback)stat_callback, &context); uint32_t processed; bool ret = (json_parser_string(&parser, json, (uint32_t)length, &processed) == 0); json_parser_free(&parser); return ret; }
jobj * jobj_parse(const char * json_str) { int res; jobj * o = NULL; json_parser_dom dom; json_parser p; json_config config; memset(&config, 0, sizeof(json_config)); res = json_parser_dom_init(&dom, dom_mknode, dom_mkval, dom_append); if(res) return NULL; res = json_parser_init(&p, &config, json_parser_dom_callback, &dom); if(res) return NULL; res = json_parser_string(&p, json_str, strlen(json_str), NULL); if(!res && json_parser_is_done(&p)) o = dom.root_structure; json_parser_free(&p); json_parser_dom_free(&dom); return o; }