int sqlite3_extension_init(sqlite3 *db, char **pzErr, const sqlite3_api_routines *pApi) { SQLITE_EXTENSION_INIT2(pApi); sqlite3_create_function_v2(db, "json_extract", // args -1, SQLITE_UTF8 | SQLITE_DETERMINISTIC, // private (void *)json_tokener_new(), // function json_extract_func, // for aggregates NULL, NULL, // destroy (void(*)(void*))json_tokener_free); sqlite3_create_function_v2(db, "json_unquote", // args 1, SQLITE_UTF8 | SQLITE_DETERMINISTIC, // private (void *)json_tokener_new(), // function json_unquote_func, // for aggregates NULL, NULL, // destroy (void(*)(void*))json_tokener_free); return SQLITE_OK; }
static int json_new(lua_State *L) { struct json_state *s; struct json_tokener *tok = json_tokener_new(); if (!tok) return 0; s = lua_newuserdata(L, sizeof(*s)); if (!s) { json_tokener_free(tok); return 0; } s->tok = tok; s->obj = NULL; s->err = json_tokener_continue; luaL_getmetatable(L, LUCI_JSONC_PARSER); lua_setmetatable(L, -2); return 1; }
static tlog_grc tlog_mem_json_reader_init(struct tlog_json_reader *reader, va_list ap) { struct tlog_mem_json_reader *mem_json_reader = (struct tlog_mem_json_reader*)reader; const char *buf = va_arg(ap, const char *); size_t len = va_arg(ap, size_t); tlog_grc grc; assert(buf != NULL || len == 0); mem_json_reader->pos = buf; mem_json_reader->end = buf + len; mem_json_reader->line = 1; mem_json_reader->tok = json_tokener_new(); if (mem_json_reader->tok == NULL) { grc = TLOG_GRC_ERRNO; goto error; } return TLOG_RC_OK; error: tlog_mem_json_reader_cleanup(reader); return grc; }
/** * Load json document from memory buffer. * @param pabyData Buffer.data. * @param nLength Buffer size. * @return true on success. If error occurred it can be received using CPLGetLastErrorMsg method. * * @since GDAL 2.3 */ bool CPLJSONDocument::LoadMemory(const GByte *pabyData, int nLength) { if(nullptr == pabyData) { return false; } if( m_poRootJsonObject ) json_object_put( TO_JSONOBJ(m_poRootJsonObject) ); json_tokener *jstok = json_tokener_new(); m_poRootJsonObject = json_tokener_parse_ex( jstok, reinterpret_cast<const char*>(pabyData), nLength ); bool bParsed = jstok->err == json_tokener_success; if(!bParsed) { CPLError( CE_Failure, CPLE_AppDefined, "JSON parsing error: %s (at offset %d)", json_tokener_error_desc( jstok->err ), jstok->char_offset ); json_tokener_free( jstok ); return false; } json_tokener_free( jstok ); return bParsed; }
static json_object *_mmsvc_core_msg_json_tokener_parse_len(const char *str, int *len, mused_msg_parse_err_e *err) { struct json_tokener *tok; struct json_object *obj; g_return_val_if_fail(str != NULL, NULL); g_return_val_if_fail(len != NULL, NULL); tok = json_tokener_new(); g_return_val_if_fail(tok != NULL, NULL); obj = json_tokener_parse_ex(tok, str, *len); g_return_val_if_fail(obj != NULL, NULL); *len = tok->char_offset; if (tok->err != json_tokener_success) { LOGE("Json Error(%d) : %s", tok->err, json_tokener_error_desc(tok->err)); json_object_put(obj); obj = NULL; } _mmsvc_core_msg_json_set_error(err, tok->err); json_tokener_free(tok); return obj; }
json_object *OGRGMEParseJSON( const char* pszText ) { if( NULL != pszText ) { json_tokener* jstok = NULL; json_object* jsobj = NULL; jstok = json_tokener_new(); jsobj = json_tokener_parse_ex(jstok, pszText, -1); if( jstok->err != json_tokener_success) { CPLError( CE_Failure, CPLE_AppDefined, "JSON parsing error: %s (at offset %d)", json_tokener_error_desc(jstok->err), jstok->char_offset); json_tokener_free(jstok); return NULL; } json_tokener_free(jstok); /* JSON tree is shared for while lifetime of the reader object * and will be released in the destructor. */ return jsobj; } return NULL; }
void vz::api::Volkszaehler::api_parse_exception(CURLresponse response, char *err, size_t n) { struct json_tokener *json_tok; struct json_object *json_obj; json_tok = json_tokener_new(); json_obj = json_tokener_parse_ex(json_tok, response.data, response.size); if (json_tok->err == json_tokener_success) { json_obj = json_object_object_get(json_obj, "exception"); if (json_obj) { const std::string err_type(json_object_get_string(json_object_object_get(json_obj, "type"))); const std::string err_message( json_object_get_string(json_object_object_get(json_obj, "message"))); snprintf(err, n, "'%s': '%s'", err_type.c_str(), err_message.c_str()); // evaluate error if (err_type == "UniqueConstraintViolationException") { if (err_message.find("Duplicate entry") ) { print(log_warning, "middle says duplicated value. removing first entry!", channel()->name()); _values.pop_front(); } } } else { strncpy(err, "missing exception", n); } } else { strncpy(err, json_tokener_errors[json_tok->err], n); } json_object_put(json_obj); json_tokener_free(json_tok); }
void fromString(std::string str) { json_object *rootobject; json_tokener *tokener = json_tokener_new(); enum json_tokener_error err; do { rootobject = json_tokener_parse_ex(tokener, str.c_str(),str.length()); } while ((err = json_tokener_get_error(tokener)) == json_tokener_continue); if (err != json_tokener_success) { fprintf(stderr, "Error: %s\n", json_tokener_error_desc(err)); // Handle errors, as appropriate for your application. } if (tokener->char_offset < str.length()) // XXX shouldn't access internal fields { // Handle extra characters after parsed object as desired. // e.g. issue an error, parse another object from that point, etc... } //Good! json_object_object_foreach(rootobject, key, val) { T one(key); N two(std::string(json_object_get_string(val))); append(one,two); }
void api_parse_exception(CURLresponse response, char *err, size_t n) { struct json_tokener *json_tok; struct json_object *json_obj; json_tok = json_tokener_new(); json_obj = json_tokener_parse_ex(json_tok, response.data, response.size); if (json_tok->err == json_tokener_success) { json_obj = json_object_object_get(json_obj, "exception"); if (json_obj) { snprintf(err, n, "%s: %s", json_object_get_string(json_object_object_get(json_obj, "type")), json_object_get_string(json_object_object_get(json_obj, "message")) ); } else { strncpy(err, "missing exception", n); } } else { strncpy(err, json_tokener_errors[json_tok->err], n); } json_object_put(json_obj); json_tokener_free(json_tok); }
static size_t json_parse_callback(void *contents, size_t size, size_t nmemb, void *userp) { static struct json_tokener *jtok; static struct json_object *jobj; size_t realsize = size * nmemb; /* initialize tokener */ if (jtok == NULL) { jtok = json_tokener_new(); jtok->err = json_tokener_continue; } if (jtok->err == json_tokener_continue) { #ifdef DEBUG printf("got chunk: %d * %d = %d bytes\r\n", size, nmemb, realsize); #endif jobj = json_tokener_parse_ex(jtok, (char *) contents, realsize); if (jtok->err == json_tokener_success) { *(struct json_object **) userp = jobj; json_tokener_free(jtok); } else if (jtok->err != json_tokener_continue) { fprintf(stderr, "parse error: %s\r\n", json_tokener_errors[jtok->err]); *(void **) userp = NULL; json_tokener_free(jtok); } } return realsize; }
OGRErr OGRTopoJSONReader::Parse( const char* pszText ) { if( NULL != pszText ) { json_tokener *jstok = json_tokener_new(); json_object *jsobj = json_tokener_parse_ex(jstok, pszText, -1); if( jstok->err != json_tokener_success) { CPLError( CE_Failure, CPLE_AppDefined, "TopoJSON parsing error: %s (at offset %d)", json_tokener_error_desc(jstok->err), jstok->char_offset); json_tokener_free(jstok); return OGRERR_CORRUPT_DATA; } json_tokener_free(jstok); /* JSON tree is shared for while lifetime of the reader object * and will be released in the destructor. */ poGJObject_ = jsobj; } return OGRERR_NONE; }
static GError * _body_parse_error (GString *b) { g_assert (b != NULL); struct json_tokener *tok = json_tokener_new (); struct json_object *jbody = json_tokener_parse_ex (tok, b->str, b->len); json_tokener_free (tok); tok = NULL; if (!jbody) return NEWERROR(0, "No error explained"); struct json_object *jcode, *jmsg; struct oio_ext_json_mapping_s map[] = { {"status", &jcode, json_type_int, 0}, {"message", &jmsg, json_type_string, 0}, {NULL, NULL, 0, 0} }; GError *err = oio_ext_extract_json(jbody, map); if (!err) { int code = 0; const char *msg = "Unknown error"; if (jcode) code = json_object_get_int64 (jcode); if (jmsg) msg = json_object_get_string (jmsg); err = NEWERROR(code, "(code=%d) %s", code, msg); } json_object_put (jbody); return err; }
/* * Helper functions */ json_object *parse_json_len(const char *json_area, int length, int *end_offset) { json_object *jobj; struct json_tokener *jtok; if (!json_area || length <= 0) return NULL; jtok = json_tokener_new(); if (!jtok) { log_dbg("ERROR: Failed to init json tokener"); return NULL; } jobj = json_tokener_parse_ex(jtok, json_area, length); if (!jobj) log_dbg("ERROR: Failed to parse json data (%d): %s", json_tokener_get_error(jtok), json_tokener_error_desc(json_tokener_get_error(jtok))); else *end_offset = jtok->char_offset; json_tokener_free(jtok); return jobj; }
static int jsonrpc_session_init(struct jsonrpc_session *session, struct lws *wsi) { session->wsi = wsi; session->tok = json_tokener_new(); if (!session->tok) return -1; return 0; }
static gboolean xr_call_unserialize_response_json(xr_call* call, const char* buf, int len) { struct json_tokener* t; struct json_object* r; int i; t = json_tokener_new(); r = json_tokener_parse_ex(t, (char*)buf, len); json_tokener_free(t); if (r == NULL) { xr_call_set_error(call, -1, "Can't parse JSON-RPC response. Invalid JSON object."); return FALSE; } struct json_object* error = json_object_object_get(r, "error"); if (error && !json_object_is_type(error, json_type_null)) { if (json_object_is_type(error, json_type_object)) { struct json_object* code = json_object_object_get(error, "code"); struct json_object* message = json_object_object_get(error, "message"); if (code && message && json_object_is_type(code, json_type_int) && json_object_is_type(message, json_type_string)) xr_call_set_error(call, json_object_get_int(code), "%s", json_object_get_string(message)); else xr_call_set_error(call, -1, "Can't parse JSON-RPC response. Invalid error object."); } else xr_call_set_error(call, -1, "Can't parse JSON-RPC response. Invalid error object."); json_object_put(r); return FALSE; } struct json_object* result = json_object_object_get(r, "result"); if (result == NULL || json_object_is_type(result, json_type_null)) { xr_call_set_error(call, -1, "Can't parse JSON-RPC response. Null result."); json_object_put(r); return FALSE; } xr_value* v = _xr_value_unserialize_json(result); if (v == NULL) { xr_call_set_error(call, -1, "Can't parse JSON-RPC response. Invalid result."); json_object_put(r); return FALSE; } xr_call_set_retval(call, v); json_object_put(r); return TRUE; }
/** * parse a JSON buffer into a value * * @param ctx * @param buf * @param len * @param valp * * @return */ dpl_status_t dpl_cdmi_parse_json_buffer(dpl_ctx_t *ctx, const char *buf, int len, dpl_value_t **valp) { int ret, ret2; json_tokener *tok = NULL; json_object *obj = NULL; dpl_value_t *val = NULL; // write(1, buf, len); tok = json_tokener_new(); if (NULL == tok) { ret = DPL_ENOMEM; goto end; } obj = json_tokener_parse_ex(tok, buf, len); if (NULL == obj) { ret = DPL_FAILURE; goto end; } ret2 = convert_obj_to_value(ctx, obj, 0, &val); if (DPL_SUCCESS != ret2) { ret = ret2; goto end; } if (NULL != valp) { *valp = val; val = NULL; } ret = DPL_SUCCESS; end: if (NULL != val) dpl_value_free(val); if (NULL != obj) json_object_put(obj); if (NULL != tok) json_tokener_free(tok); return ret; }
int bridge_request_init(bridge_request_t *self, bridge_t *bridge, int socket) { if (FCGX_InitRequest(&self->request, socket, FCGI_FAIL_ACCEPT_ON_INTR) != 0) { return EINVAL; } self->tokener = json_tokener_new(); self->bridge = bridge; self->next = 0; self->response = json_object_new_object(); return 0; }
GError* service_info_load_json(const gchar *encoded, struct service_info_s **out, gboolean permissive) { struct json_tokener *tok = json_tokener_new(); struct json_object *obj = json_tokener_parse_ex(tok, encoded, strlen(encoded)); json_tokener_free(tok); GError *err = service_info_load_json_object(obj, out, permissive); json_object_put(obj); return err; }
static struct json_object * compile_json(const gchar *json) { struct json_tokener *tok; struct json_object *jso; tok = json_tokener_new(); jso = json_tokener_parse_ex(tok, json, strlen(json)); assert_true(tok->err == json_tokener_success, "expected to parse input json, but couldn't, json=%s", json); json_tokener_free(tok); return jso; }
int token_parse_json(struct access_token **tokenp, struct evbuffer *buf) { char cbuf[1024]; int removed; int ret; struct access_token *token; struct json_tokener *tokener; enum json_tokener_error jerr; struct json_object *obj; tokener = json_tokener_new(); if (tokener == NULL) { return ENOMEM; } do { removed = evbuffer_remove(buf, cbuf, sizeof(cbuf)); obj = json_tokener_parse_ex(tokener, cbuf, removed); jerr = json_tokener_get_error(tokener); verbose(FIREHOSE, "%s(): Passed %d bytes, result %p (%s), remaining %zd\n", __func__, removed, obj, json_tokener_error_desc(jerr), evbuffer_get_length(buf)); } while (obj == NULL && jerr == json_tokener_continue && evbuffer_get_length(buf) > 0); json_tokener_free(tokener); if (obj != NULL) { token = malloc(sizeof(*token)); if (token == NULL) { ret = ENOMEM; } else { memset(token, 0, sizeof(*token)); ret = build_token_into(token, obj); if (ret != 0) { token_free(token); } } } else { verbose(FIREHOSE, "%s(): json tokener reported: %s\n", __func__, json_tokener_error_desc(jerr)); } json_object_put(obj); if (ret == 0) { *tokenp = token; } return ret; }
static tlog_grc tlog_es_reader_init(struct tlog_reader *reader, va_list ap) { struct tlog_es_reader *es_reader = (struct tlog_es_reader*)reader; const char *base_url = va_arg(ap, const char *); const char *query = va_arg(ap, const char *); size_t size = va_arg(ap, size_t); CURLcode rc; tlog_grc grc; assert(tlog_es_reader_base_url_is_valid(base_url)); assert(query != NULL); assert(size >= TLOG_ES_READER_SIZE_MIN); /* Create and initialize CURL handle */ es_reader->curl = curl_easy_init(); if (es_reader->curl == NULL) { grc = TLOG_RC_ES_READER_CURL_INIT_FAILED; goto error; } rc = curl_easy_setopt(es_reader->curl, CURLOPT_WRITEFUNCTION, tlog_es_reader_write_func); if (rc != CURLE_OK) { grc = TLOG_GRC_FROM(curl, rc); goto error; } /* Format URL prefix */ grc = tlog_es_reader_format_url_pfx(&es_reader->url_pfx, es_reader->curl, base_url, query, size); if (grc != TLOG_RC_OK) { goto error; } /* Set request size */ es_reader->size = size; /* Create JSON tokener */ es_reader->tok = json_tokener_new(); if (es_reader->tok == NULL) { grc = TLOG_GRC_ERRNO; goto error; } return TLOG_RC_OK; error: tlog_es_reader_cleanup(reader); return grc; }
int noit_check_stats_from_json_str(noit_check_t *check, stats_t *s, const char *json_str, int len) { int rv = -1; struct json_tokener *tok = NULL; struct json_object *root = NULL; tok = json_tokener_new(); root = json_tokener_parse_ex(tok, json_str, len); if(root) rv = populate_stats_from_resmon_formatted_json(check, s, root, NULL); if(tok) json_tokener_free(tok); if(root) json_object_put(root); return rv; }
bool JSON_parser(Variant &return_value, const char *data, int data_len, bool assoc, int depth, int64_t options) { json_tokener *tok; json_object *new_obj; bool retval = false; #if JSON_C_MINOR_VERSION >= 11 tok = json_tokener_new_ex(depth); #else tok = json_tokener_new(); #endif if (!tok) { return retval; } //if (!(options & k_JSON_FB_LOOSE)) { // json_tokener_set_flags(tok, JSON_TOKENER_STRICT); //} bool const stable_maps = options & k_JSON_FB_STABLE_MAPS; bool const collections = stable_maps || options & k_JSON_FB_COLLECTIONS; new_obj = json_tokener_parse_ex(tok, data, data_len); if (json_tokener_get_error(tok)==json_tokener_continue) { new_obj = json_tokener_parse_ex(tok, "", -1); } if (new_obj) { return_value = json_object_to_variant(new_obj, assoc, stable_maps, collections); json_object_put(new_obj); retval = true; } else { switch (json_tokener_get_error(tok)) { case json_tokener_success: retval = true; break; case json_tokener_error_depth: json_set_last_error_code(json_error_codes::JSON_ERROR_DEPTH); break; default: json_set_last_error_code(json_error_codes::JSON_ERROR_SYNTAX, json_tokener_get_error(tok)); } } json_tokener_free(tok); return retval; }
static tlog_grc tlog_journal_json_reader_init(struct tlog_json_reader *reader, va_list ap) { struct tlog_journal_json_reader *journal_json_reader = (struct tlog_journal_json_reader*)reader; uint64_t since = va_arg(ap, uint64_t); uint64_t until = va_arg(ap, uint64_t); const char * const *match_sym_list = va_arg(ap, const char * const *); int sd_rc; tlog_grc grc; /* Create JSON tokener */ journal_json_reader->tok = json_tokener_new(); if (journal_json_reader->tok == NULL) { grc = TLOG_GRC_ERRNO; goto error; } /* Open journal */ sd_rc = sd_journal_open(&journal_json_reader->journal, 0); if (sd_rc < 0) { grc = TLOG_GRC_FROM(systemd, sd_rc); goto error; } /* Add matches */ sd_rc = tlog_journal_add_match_sym_list(journal_json_reader->journal, match_sym_list); if (sd_rc < 0) { grc = TLOG_GRC_FROM(systemd, sd_rc); goto error; } /* Seek to "since" timestamp */ sd_rc = sd_journal_seek_realtime_usec(journal_json_reader->journal, since); if (sd_rc < 0) { grc = TLOG_GRC_FROM(systemd, sd_rc); goto error; } /* Store "until" timestamp */ journal_json_reader->until = until; return TLOG_RC_OK; error: tlog_journal_json_reader_cleanup(reader); return grc; }
vde_sobj *vde_sobj_from_string(const char *str) { struct json_tokener *tok; struct json_object *obj; tok = json_tokener_new(); if (tok == NULL) { errno = ENOMEM; return NULL; } obj = json_tokener_parse_ex(tok, str, -1); json_tokener_free(tok); return obj; }
static gboolean xr_call_unserialize_request_json(xr_call* call, const char* buf, int len) { struct json_tokener* t; struct json_object* r; int i; t = json_tokener_new(); r = json_tokener_parse_ex(t, (char*)buf, len); json_tokener_free(t); if (r == NULL) { xr_call_set_error(call, -1, "Can't parse JSON-RPC request. Invalid JSON object."); return FALSE; } call->method = g_strdup(json_object_get_string(json_object_object_get(r, "method"))); if (call->method == NULL) { xr_call_set_error(call, -1, "Can't parse JSON-RPC request. Missing method."); json_object_put(r); return FALSE; } struct json_object* params = json_object_object_get(r, "params"); if (params == NULL || !json_object_is_type(params, json_type_array)) { xr_call_set_error(call, -1, "Can't parse JSON-RPC request. Invalid params."); json_object_put(r); return FALSE; } const int params_count = json_object_array_length(params); for (i = 0; i < params_count; i++) { xr_value* v = _xr_value_unserialize_json(json_object_array_get_idx(params, i)); if (v == NULL) { xr_call_set_error(call, -1, "Can't parse JSON-RPC request. Failed to unserialize parameter %d.", i); json_object_put(r); return FALSE; } xr_call_add_param(call, v); } json_object_put(r); return TRUE; }
static JCrusherData * jcrusher_data_alloc() { JCrusherData *data; data = (JCrusherData *) TSmalloc(sizeof(JCrusherData)); data->state = STATE_BUFFER_DATA; data->downstream_vio = NULL; data->downstream_buffer = NULL; data->downstream_reader = NULL; data->json_tok = json_tokener_new(); data->json_obj = NULL; return data; }
iot_json_t *iot_json_string_to_object(const char *s, int len) { if (parser == NULL) { parser = json_tokener_new(); if (parser == NULL) return NULL; } else json_tokener_reset(parser); if (len < 0) len = strlen(s); return json_tokener_parse_ex(parser, s, len); }
static void netifd_parse_script_handler(const char *name, script_dump_cb cb) { struct json_tokener *tok = NULL; json_object *obj; static char buf[512]; char *start, *cmd; FILE *f; int len; #define DUMP_SUFFIX " '' dump" cmd = alloca(strlen(name) + 1 + sizeof(DUMP_SUFFIX)); sprintf(cmd, "%s" DUMP_SUFFIX, name); f = popen(cmd, "r"); if (!f) return; do { start = fgets(buf, sizeof(buf), f); if (!start) continue; len = strlen(start); if (!tok) tok = json_tokener_new(); obj = json_tokener_parse_ex(tok, start, len); if (!is_error(obj)) { netifd_init_script_handler(name, obj, cb); json_object_put(obj); json_tokener_free(tok); tok = NULL; } else if (start[len - 1] == '\n') { json_tokener_free(tok); tok = NULL; } } while (!feof(f) && !ferror(f)); if (tok) json_tokener_free(tok); pclose(f); }
/** Query a Couchbase design document view * * Setup and execute a Couchbase view request and wait for the result. * * @param instance Couchbase connection instance. * @param cookie Couchbase cookie for returning information from callbacks. * @param path The fully qualified view path including the design document and view name. * @param post The post payload (NULL for none). * @return Couchbase error object. */ lcb_error_t couchbase_query_view(lcb_t instance, const void *cookie, const char *path, const char *post) { cookie_u cu; /* union of const and non const pointers */ cu.cdata = cookie; /* set const union member to cookie passed from couchbase */ cookie_t *c = (cookie_t *) cu.data; /* set our cookie struct using non-const member */ lcb_error_t error; /* couchbase command return */ lcb_http_cmd_t cmd; /* http command struct */ const lcb_http_cmd_t *commands; /* http commands array */ commands = &cmd; memset(&cmd, 0, sizeof(cmd)); /* populate command struct */ cmd.v.v0.path = path; cmd.v.v0.npath = strlen(cmd.v.v0.path); cmd.v.v0.body = post; cmd.v.v0.nbody = post ? strlen(post) : 0; cmd.v.v0.method = post ? LCB_HTTP_METHOD_POST : LCB_HTTP_METHOD_GET; cmd.v.v0.chunked = 1; cmd.v.v0.content_type = "application/json"; /* clear cookie */ memset(c, 0, sizeof(cookie_t)); /* init tokener error */ c->jerr = json_tokener_success; /* create token */ c->jtok = json_tokener_new(); /* debugging */ DEBUG3("rlm_couchbase: fetching view %s", path); /* query the view */ if ((error = lcb_make_http_request(instance, c, LCB_HTTP_TYPE_VIEW, commands, NULL)) == LCB_SUCCESS) { /* enter event loop on success */ lcb_wait(instance); } /* free token */ json_tokener_free(c->jtok); /* return error */ return error; }