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; }
/** * Process a Keystone authentication response. * This parses the response and saves copies of the interesting service endpoint URLs. */ static size_t process_keystone_response(void *ptr, size_t size, size_t nmemb, void *userdata) { keystone_context_t *context = (keystone_context_t *) userdata; const char *body = (const char *) ptr; size_t len = size * nmemb; struct json_object *jobj; enum json_tokener_error json_err; assert(context->pvt.json_tokeniser != NULL); jobj = json_tokener_parse_ex(context->pvt.json_tokeniser, body, len); json_err = json_tokener_get_error(context->pvt.json_tokeniser); if (json_tokener_success == json_err) { enum keystone_error sc_err = process_keystone_json(context, jobj); if (sc_err != KSERR_SUCCESS) { return 0; /* Failed to process JSON. Inform libcurl no data 'handled' */ } } else if (json_tokener_continue == json_err) { /* Complete JSON response not yet received; continue */ } else { context->json_error("json_tokener_parse_ex", json_err); context->keystone_error("failed to parse response", KSERR_PARSE); return 0; /* Apparent JSON parsing problem. Inform libcurl no data 'handled' */ } return len; /* Inform libcurl that all data were 'handled' */ }
static int jsonrpc_session_receive(struct jsonrpc_session *session, void *in, size_t len) { struct json_tokener *tok = session->tok; struct json_object *request = NULL, *response = NULL; unsigned char *reply = NULL; size_t reply_len = 0; request = json_tokener_parse_ex(tok, (const char *)in, (int)len); if (!request) { enum json_tokener_error err = json_tokener_get_error(tok); if (err == json_tokener_continue) return 0; return -1; } jsonrpc_call(session, request, &response); reply_len = LWS_PRE + strlen(json_object_get_string(response)); reply = (unsigned char *)calloc(1, reply_len + 1); strcpy((char *)(&reply[LWS_PRE]), json_object_get_string(response)); lws_write(session->wsi, &reply[LWS_PRE], reply_len - LWS_PRE, LWS_WRITE_TEXT); free(reply); json_object_put(request); json_object_put(response); return 0; }
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; }
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; }
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 int json_parse(lua_State *L) { size_t len; const char *json = luaL_checklstring(L, 1, &len); struct json_state s = { .tok = json_tokener_new() }; if (!s.tok) return 0; s.obj = json_tokener_parse_ex(s.tok, json, len); s.err = json_tokener_get_error(s.tok); if (s.obj) { _json_to_lua(L, s.obj); json_object_put(s.obj); } else { lua_pushnil(L); } if (s.err == json_tokener_continue) s.err = json_tokener_error_parse_eof; if (s.err) lua_pushstring(L, json_tokener_error_desc(s.err)); json_tokener_free(s.tok); return (1 + !!s.err); }
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); }
int bridge_request_handle(bridge_request_t *self) { int ret; char *buffer; struct json_object *in_json; if ((ret = bridge_request_getinput(self, &buffer)) != 0) { bridge_request_fatal(self); goto out; } in_json = json_tokener_parse_ex(self->tokener, buffer, -1); if (!in_json) { bridge_request_fatal(self); ret = -1; goto cleanup; } ret = bridge_request_call_json_dbus(self, in_json); cleanup: json_object_put(in_json); json_tokener_reset(self->tokener); free(buffer); out: if (ret != 0) FCGX_Finish_r(&self->request); return ret; }
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); }
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; }
/* * 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 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; }
/** * 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; }
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); }
/** * Parse the data retrieved by CURL into a JSON object - to be supplied to * curl_easy_setopt with CURLOPT_WRITEFUNCTION and called by * curl_easy_perform. * * @param ptr Pointer to the retrieved (piece of) data that should be * parsed. * @param size Size of each retrieved data chunk. * @param nmemb Number of retrieved data chunks. * @param userdata The data supplied to curl_easy_setopt as * CURLOPT_WRITEDATA, must be struct * tlog_es_reader_write_data. * * @return Number of bytes processed, signals error if different from * size*nmemb, can be CURL_WRITEFUNC_PAUSE to signal transfer pause. */ static size_t tlog_es_reader_write_func(char *ptr, size_t size, size_t nmemb, void *userdata) { struct tlog_es_reader_write_data *data = (struct tlog_es_reader_write_data *)userdata; size_t len = size * nmemb; assert(ptr != NULL || len == 0); assert(data != NULL); if (len == 0) { return len; } if (data->obj != NULL) { return !len; } data->obj = json_tokener_parse_ex(data->tok, ptr, (int)len); if (data->obj == NULL) { data->rc = json_tokener_get_error(data->tok); if (data->rc != json_tokener_continue) { return !len; } } return len; }
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; }
struct json_object* json_parse(const char *str,int len,enum json_tokener_error *status) { struct json_tokener* tok; struct json_object* obj; tok = json_tokener_new(); obj = json_tokener_parse_ex(tok, str, len); if( tok-> err == json_tokener_continue ) obj = json_tokener_parse_ex(tok, "", -1); if(tok->err != json_tokener_success) { obj = NULL; if (status) *status = tok->err; } json_tokener_free(tok); return obj; }
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; }
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; }
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; }
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; }
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; }
/** Couchbase callback for get (read) operations * * @param instance Couchbase connection instance. * @param cookie Couchbase cookie for returning information from callbacks. * @param error Couchbase error object. * @param resp Couchbase get operation response object. */ void couchbase_get_callback(lcb_t instance, const void *cookie, lcb_error_t error, const lcb_get_resp_t *resp) { 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 */ const char *bytes = resp->v.v0.bytes; /* the payload of this chunk */ lcb_size_t nbytes = resp->v.v0.nbytes; /* length of this data chunk */ /* check error */ switch (error) { case LCB_SUCCESS: /* check for valid bytes */ if (bytes && nbytes > 1) { /* debug */ DEBUG("rlm_couchbase: (get_callback) got %zu bytes", nbytes); /* parse string to json object */ c->jobj = json_tokener_parse_ex(c->jtok, bytes, nbytes); /* switch on tokener error */ switch ((c->jerr = json_tokener_get_error(c->jtok))) { case json_tokener_continue: /* check object - should be null */ if (c->jobj != NULL) { ERROR("rlm_couchbase: (get_callback) object not null on continue!"); } break; case json_tokener_success: /* do nothing */ break; default: /* log error */ ERROR("rlm_couchbase: (get_callback) json parsing error: %s", json_tokener_error_desc(c->jerr)); break; } } break; case LCB_KEY_ENOENT: /* ignored */ DEBUG("rlm_couchbase: (get_callback) key does not exist"); break; default: /* log error */ ERROR("rlm_couchbase: (get_callback) %s (0x%x)", lcb_strerror(instance, error), error); break; } }
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); }
static size_t CPLJSONWriteFunction(void *pBuffer, size_t nSize, size_t nMemb, void *pUserData) { size_t nLength = nSize * nMemb; JsonContextL ctx = static_cast<JsonContextL>(pUserData); ctx->pObject = json_tokener_parse_ex(ctx->pTokener, static_cast<const char*>(pBuffer), static_cast<int>(nLength)); ctx->nDataLen = static_cast<int>(nLength); switch (json_tokener_get_error(ctx->pTokener)) { case json_tokener_continue: case json_tokener_success: return nLength; default: return 0; /* error: interrupt the transfer */ } }
struct json_object* json_tokener_parse_verbose(const char *str, enum json_tokener_error *error) { struct json_tokener* tok; struct json_object* obj; tok = json_tokener_new(); if (!tok) return NULL; obj = json_tokener_parse_ex(tok, str, -1); *error = tok->err; if(tok->err != json_tokener_success) { if (obj != NULL) json_object_put(obj); obj = NULL; } json_tokener_free(tok); return obj; }