static json_t* build_compact_array_header(json_t* json) { assert(json_is_array(json)); size_t length = json_array_size(json); json_t* compact_header = json_array(); for (int i = 0; i < length; ++i) { json_t* obj = json_array_get(json, i); assert(json_is_object(obj)); void* iter = json_object_iter(obj); while (iter) { const char* key = json_object_iter_key(iter); if (!string_array_contains(compact_header, key)) { json_array_append_new(compact_header, json_string(key)); } iter = json_object_iter_next(obj, iter); } } return compact_header; }
// method result is expected in following format // { // "result-type" : "<SUCCESS/ERROR>", // "result" : <error string or actual result> // } // return value or error needs to be freed struct data_t *deserialize_result(const char *serialized, char **error) { json_t *root; json_error_t parse_error; root = json_loads(serialized, 0, &parse_error); if(!root || !json_is_object(root)) { if(root) json_decref(root); return NULL; } json_t *result_type = json_object_get(root, RESULT_TYPE_JSON_KEY); json_t *result = json_object_get(root, RESULT_JSON_KEY); if(!result_type || !result) { json_decref(root); return NULL; } if(!json_is_string(result_type)) { json_decref(root); return NULL; } if(0 == strcmp("SUCCESS", json_string_value(result_type))) { struct data_t *data = deserialize_data(result); json_decref(root); return data; } else { if(!json_is_string(result)) { json_decref(root); return NULL; } *error = private_strdup(json_string_value(result)); json_decref(root); } return NULL; }
int json_object_set_new_nocheck(json_t *json, const char *key, json_t *value) { json_object_t *object; if(!value) return -1; if(!key || !json_is_object(json) || json == value) { json_decref(value); return -1; } object = json_to_object(json); if(hashtable_set(&object->hashtable, key, object->serial++, value)) { json_decref(value); return -1; } return 0; }
int add_collection(json_t *object, const char *coll_name, baton_error_t *error) { init_baton_error(error); if (!json_is_object(object)) { set_baton_error(error, -1, "Failed to add collection data: " "target not a JSON object"); goto error; } json_t *coll = json_pack("s", coll_name); if (!coll) { set_baton_error(error, -1, "Failed to pack data object '%s' as JSON", coll_name); goto error; } return json_object_set_new(object, JSON_COLLECTION_KEY, coll); error: return error->code; }
int sendOrder(Parameters& params, std::string direction, double quantity, double price) { if (direction.compare("buy") != 0 && direction.compare("sell") != 0) { *params.logFile << "Error: Neither \"buy\" nor \"sell\" selected" << std::endl; return 0; } double limPrice; // define limit price to be sure to be executed if (direction.compare("buy") == 0) { limPrice = getLimitPrice(params, quantity, false); } else { limPrice = getLimitPrice(params, quantity, true); } *params.logFile << "<Kraken> Trying to send a \"" << direction << "\" limit order: " << quantity << " @ $" << limPrice << "..." << std::endl; std::string pair = "XXBTZUSD"; std::string type = direction; std::string ordertype = "limit"; std::string pricelimit = patch::to_string(limPrice); std::string volume = patch::to_string(quantity); std::string options = "pair=" + pair + "&type=" + type + "&ordertype=" + ordertype + "&price=" + pricelimit + "&volume=" + volume; json_t* res = authRequest(params, "https://api.kraken.com", "/0/private/AddOrder", options); json_t* root = json_object_get(res, "result"); if (json_is_object(root) == 0) { *params.logFile << json_dumps(res, 0) << std::endl; exit(0); } std::string txid = json_string_value(json_array_get(json_object_get(root, "txid"), 0)); int max_id = id_to_transaction->size(); (*id_to_transaction)[max_id] = txid; *params.logFile << "<Kraken> Done (transaction ID: " << txid << ")\n" << std::endl; json_decref(root); return max_id; }
void BinWriter::writeField(json_t *jfield) { writeMemberInfo(jfield); // handle attr json_t *field_attr = json_object_get(jfield, "fieldattributes"); bytes.writeInt((int)json_array_size(field_attr)); for (size_t i = 0; i < json_array_size(field_attr); i++) { bytes.writeInt(poolJString(json_array_get(field_attr, i))); } utString stype = json_string_value(json_object_get(jfield, "type")); if (stype.size() > 0) { bytes.writeBoolean(true); bytes.writeInt(poolString(stype.c_str())); } else { bytes.writeBoolean(false); } // handle template types json_t *ttypes = json_object_get(jfield, "templatetypes"); if (ttypes && json_is_object(ttypes)) { bytes.writeBoolean(true); writeTemplateTypes(ttypes); } else { bytes.writeBoolean(false); } }
int64_t guestfs_impl_disk_virtual_size (guestfs_h *g, const char *filename) { CLEANUP_JSON_T_DECREF json_t *tree = get_json_output (g, filename); json_t *node; if (tree == NULL) return -1; if (!json_is_object (tree)) goto bad_type; node = json_object_get (tree, "virtual-size"); if (!json_is_integer (node)) goto bad_type; return json_integer_value (node); bad_type: error (g, _("qemu-img info: JSON output did not contain ‘virtual-size’ key")); return -1; }
ScriptProps::Property& ScriptProps::add_property(const char* nm, json_t* value) throw (Exception) { if (json_is_object(value) || json_is_array(value) ) { throw_error_v(ErrorImplError, "Only literal value allowed for added property"); } Property* _tmp = new Property(*this, nm, value); if ( _tmp->MacroExpandable::finished() == false ) { throw_error_v(ErrorImplError, "Only literal value allowed for added property"); } if ( _tmp->Evaluable::finished() == false ) { throw_error_v(ErrorImplError, "Only literal value allowed for added property"); } MapIter it = props.find(nm); if ( it != props.end() ) { script.unregist_evaluable(it->second.get()); } props[nm].reset(_tmp); return *_tmp; }
char* send_request(const char *uid, const char *local_req, json_t *recipients, int timeout, const char* payload_type, json_t *msg_payload) { /** * creates a query to the mediator to send a msg * * @param uid that is used to identify the answer to the query * @param list of recipients as json array; msg is always broadcasted, but recipients on this list need to acknowledge the msg * @param timeout after which mediator will stop resending msg * @param payload_type as string that identifies payload * @param payload as json object * * @return the string encoded JSON msg that can be sent directly via zyre. Must be freed by user! Returns NULL if wrong json types are passed in. */ if (!json_is_array(recipients)) { printf("ERROR: Recipients are not a json array! \n"); return NULL; } if (!json_is_object(msg_payload)) { printf("ERROR: Payload is not a json object! \n"); return NULL; } json_t *root; root = json_object(); json_object_set(root, "metamodel", json_string("sherpa_mgs")); json_object_set(root, "model", json_string("http://kul/send_request.json")); json_object_set(root, "type", json_string("send_request")); json_t *payload; payload = json_object(); json_object_set(payload, "UID", json_string(uid)); json_object_set(payload, "local_requester", json_string(local_req)); json_object_set(payload, "recipients", recipients); json_object_set(payload, "timeout", json_integer(timeout)); json_object_set(payload, "payload_type", json_string(payload_type)); json_object_set(payload, "payload", msg_payload); json_object_set(root, "payload", payload); char* ret = json_dumps(root, JSON_ENCODE_ANY); json_decref(root); return ret; }
/** * Convert JSON object to PyObject * @param JSONObject * @return PyObject */ static PyObject* convert(json_t *json) { PyObject* object = NULL; json_t* val; int result = 0; if (json_is_object(json)) { object = PyDict_New(); char* key; json_object_foreach(json, key, val) { if (json_is_object(val) || json_is_array(val)) { result = PyDict_SetItemString(object, key, convert(val)); } else if(json_is_string(val)) { const char *str = json_string_value(val); result = PyDict_SetItemString(object, key, PyString_DecodeEscape(str, str ? strlen(str) : 0, NULL, 0, NULL)); } else if(json_is_boolean(val)) { result = PyDict_SetItemString(object, key, json_is_true(val) ? Py_True : Py_False); } else if(json_is_integer(val)) { result = PyDict_SetItemString(object, key, PyInt_FromLong(json_integer_value(val))); } else if(json_is_real(val)) { result = PyDict_SetItemString(object, key, PyFloat_FromDouble(json_real_value(val))); } else { result = PyDict_SetItemString(object, key, Py_None); } if (result == -1) goto failure; } } else if (json_is_array(json)) {
int load_cfg_http_svr(json_t *root, const char *key, http_svr_cfg *cfg) { json_t *node = json_object_get(root, key); if (!node || !json_is_object(node)) return -__LINE__; json_t *bind = json_object_get(node, "bind"); if (!bind) return -__LINE__; if (json_is_string(bind)) { cfg->bind_count = 1; cfg->bind_arr = malloc(sizeof(nw_svr_bind)); if (nw_sock_cfg_parse(json_string_value(bind), &cfg->bind_arr[0].addr, &cfg->bind_arr[0].sock_type) < 0) return -__LINE__; } else if (json_is_array(bind)) { cfg->bind_count = json_array_size(bind); if (cfg->bind_count == 0) return -__LINE__; cfg->bind_arr = malloc(sizeof(nw_svr_bind) * cfg->bind_count); for (uint32_t i = 0; i < cfg->bind_count; ++i) { json_t *row = json_array_get(bind, i); if (!json_is_string(row)) return -__LINE__; if (nw_sock_cfg_parse(json_string_value(row), &cfg->bind_arr[i].addr, &cfg->bind_arr[i].sock_type) < 0) return -__LINE__; } } else { return -__LINE__; } ERR_RET(read_cfg_uint32(node, "max_pkg_size", &cfg->max_pkg_size, true, 0)); ERR_RET(read_cfg_uint32(node, "buf_limit", &cfg->buf_limit, false, 0)); ERR_RET(read_cfg_uint32(node, "read_mem", &cfg->read_mem, false, 0)); ERR_RET(read_cfg_uint32(node, "write_mem", &cfg->write_mem, false, 0)); ERR_RET(read_cfg_int(node, "keep_alive", &cfg->keep_alive, false, 3600)); return 0; }
char * guestfs_impl_disk_format (guestfs_h *g, const char *filename) { CLEANUP_JSON_T_DECREF json_t *tree = get_json_output (g, filename); json_t *node; if (tree == NULL) return NULL; if (!json_is_object (tree)) goto bad_type; node = json_object_get (tree, "format"); if (!json_is_string (node)) goto bad_type; return safe_strndup (g, json_string_value (node), json_string_length (node)); /* caller frees */ bad_type: error (g, _("qemu-img info: JSON output did not contain ‘format’ key")); return NULL; }
static void parse_memcached(const json_t *obj) { json_t *servers; if (!json_is_object(obj)) { /* No memcached config so don't use it. */ memcached_free(srv.mc); srv.mc = NULL; return; } servers = json_object_get(obj, "servers"); if (json_is_array(servers)) { unsigned int i, size = json_array_size(servers); for (i = 0; i < size; i++) { json_t *server_obj; server_obj = json_array_get(servers, i); parse_memcached_server(server_obj); } } }
/* * parse JSON JWK */ apr_byte_t apr_jwk_parse_json(apr_pool_t *pool, json_t *j_json, const char *s_json, apr_jwk_t **j_jwk) { /* check that we've actually got a JSON value back */ if (j_json == NULL) return FALSE; /* check that the value is a JSON object */ if (!json_is_object(j_json)) return FALSE; /* allocate memory for the JWK */ *j_jwk = apr_pcalloc(pool, sizeof(apr_jwk_t)); apr_jwk_t *jwk = *j_jwk; /* set the raw JSON/string representations */ jwk->value.json = j_json; jwk->value.str = apr_pstrdup(pool, s_json); /* get the key type */ char *kty = NULL; if (apr_jwt_get_string(pool, &jwk->value, "kty", &kty) == FALSE) return FALSE; /* kty is mandatory */ if (kty == NULL) return FALSE; /* parse the key */ if (apr_strnatcmp(kty, "RSA") == 0) return apr_jwk_parse_rsa(pool, jwk); if (apr_strnatcmp(kty, "EC") == 0) return apr_jwk_parse_ec(pool, jwk); return FALSE; }
static void loadConfig(PluginData* data, const char* filename) { const char* viceExe = 0; const char* prgFile = 0; const char* kickAssSymbols = 0; const char* breakpointFile = 0; json_error_t error; setupDefaultConfig(data); json_t* root = json_load_file(filename, 0, &error); if (!root || !json_is_object(root)) return; log_debug("loaded config\n"); json_unpack(root, "{s:s, s:s, s:s, s:s}", "vice_exe", &viceExe, "prg_file", &prgFile, "kickass_symbols", &kickAssSymbols, "breakpoints_file", &breakpointFile); if (viceExe && viceExe[0] != 0) data->config.viceExe = strdup(viceExe); if (prgFile && prgFile[0] != 0) data->config.prgFile = strdup(prgFile); if (breakpointFile && breakpointFile[0] != 0) data->config.breakpointFile = strdup(breakpointFile); if (kickAssSymbols && kickAssSymbols[0] != 0) data->config.kickAssSymbols = strdup(kickAssSymbols); json_decref(root); }
Frame* CreateFramesFromJSON(json_t* root) { if (!json_is_array(root)) { SDL_LogError( SDL_LOG_CATEGORY_APPLICATION, "Frames is not JSON array" ); return NULL; } Frame* frames = NULL; Frame** frameIter = &frames; uint32_t i = 0; for (i = 0; i < json_array_size(root); i++) { json_t* frameNode; frameNode = json_array_get(root, i); if (!json_is_object(frameNode)) { SDL_LogWarn( SDL_LOG_CATEGORY_APPLICATION, "Frame is invalid JSON type. Expected object.\n" ); continue; } while (*frameIter != NULL) frameIter = &((*frameIter)->next); *frameIter = CreateFrameFromJSON(frameNode); } return frames; }
static int parse_metadata( json_t *metadata, struct stat *stbuf ) { if( !json_is_object( metadata ) ) return -1; if( json_is_integer( json_object_get(metadata, "cdmi_size") ) ) stbuf->st_size = json_integer_value( json_object_get(metadata, "cdmi_size") ); if( json_is_string( json_object_get(metadata, "cdmi_ctime") ) ) { stbuf->st_ctime = iso8601_decode( json_string_value( json_object_get(metadata, "cdmi_ctime") ) ); } if( json_is_string( json_object_get(metadata, "cdmi_atime") ) ) { stbuf->st_atime = iso8601_decode( json_string_value( json_object_get(metadata, "cdmi_atime") ) ); } if( json_is_string( json_object_get(metadata, "cdmi_mtime") ) ) { stbuf->st_mtime = iso8601_decode( json_string_value( json_object_get(metadata, "cdmi_mtime") ) ); } if( options.gotmeta && json_is_string( json_object_get(metadata, "cdmifs_mode") ) ) { const char *modestr = json_string_value( json_object_get(metadata, "cdmifs_mode" ) ); sscanf( modestr, "%o", &(stbuf->st_mode) ); } return 0; }
int parse_json(const char *data) { json_error_t error; json_t * root = json_loads(data, 0, &error); if(!root) { fprintf(stderr, "error: on line %d: %s\n", error.line, error.text); return 1; } const char *key; json_t *value; void *iter = json_object_iter(root); while(iter) { key = json_object_iter_key(iter); value = json_object_iter_value(iter); fprintf(stderr, "CHERRY key:%s\n", key); if(json_is_object(value)) { } iter = json_object_iter_next(root, iter); } return 0; }
int json_object_set_new_nocheck(json_t *json, const char *key, json_t *value) { json_object_t *object; object_key_t *k; if(!key || !value) return -1; if(!json_is_object(json) || json == value) { json_decref(value); return -1; } object = json_to_object(json); /* offsetof(...) returns the size of object_key_t without the last, flexible member. This way, the correct amount is allocated. */ k = (object_key_t *) jsonp_malloc(offsetof(object_key_t, key) + strlen(key) + 1); if(!k) { json_decref(value); return -1; } k->serial = object->serial++; strcpy(k->key, key); if(hashtable_set(&object->hashtable, k, value)) { json_decref(value); return -1; } return 0; }
/* * parse JSON object from string in to JWT value */ static apr_byte_t apr_jwt_base64url_decode_object(apr_pool_t *pool, const char *str, apr_jwt_value_t *value) { /* base64url-decode the string representation into value->str */ if (apr_jwt_base64url_decode(pool, &value->str, str, 1) < 0) return FALSE; /* decode the string in to a JSON structure into value->json */ json_error_t json_error; value->json = json_loads(value->str, 0, &json_error); if (value->json == NULL) return FALSE; /* check that we've actually got a JSON value back */ if (value->json == NULL) return FALSE; /* check that the value is a JSON object */ if (!json_is_object(value->json)) return FALSE; return TRUE; }
/** * Parse the JSON array. Called to parse the result of * the LISTSTATUS operation. Thus each element of the JSON array is * a JSON object with the information of a file entry contained * in the folder. * * @param jobj The JSON array to be parsed * @param fileStat The hdfsFileInfo handle used to * store a group of file information * @param numEntries Capture the number of files in the folder * @return 0 for success */ static int parseJsonArrayForFileStatuses(json_t *jobj, hdfsFileInfo **fileStat, int *numEntries) { json_t *jvalue = NULL; int i = 0, ret = 0, arraylen = 0; hdfsFileInfo *fileInfo = NULL; arraylen = (int) json_array_size(jobj); if (arraylen > 0) { fileInfo = calloc(arraylen, sizeof(hdfsFileInfo)); if (!fileInfo) { return ENOMEM; } } for (i = 0; i < arraylen; i++) { //Getting the array element at position i jvalue = json_array_get(jobj, i); if (json_is_object(jvalue)) { ret = parseJsonForFileStatus(jvalue, &fileInfo[i]); if (ret) { goto done; } } else { ret = EIO; goto done; } } done: if (ret) { free(fileInfo); } else { *numEntries = arraylen; *fileStat = fileInfo; } return ret; }
int8_t main(int argc, char **argv) { char *source_file; char *root; if (argc >= 2) { json_error_t error; json_t *root = json_loads(argv[1], 1, &error); if (!root) { printf("{\"error\": \"JSON decode - line %d: %s\"}", error.line, error.text); return 1; } if (json_is_object(root)) { json_t *_type = json_object_get(root, "type"); const char *type; if (json_is_string(_type)) { type = json_string_value(_type); } else { type = "flight"; } if (strcmp(type, "flight") == 0) { _main_flight(root); } else { _main_comp(root); } } json_decref(root); return 1; } else if (argc == 0) { printf("{\"error\": \"Please provide a file\"}"); exit(2); } }
void CouchWorkingMemory::clear(){ // Get the latest revision of the document CouchWorkingMemory::S_Root = NULL; init(); // Update it with an empty WM if( CouchWorkingMemory::S_Root ){ json_object_set_new( CouchWorkingMemory::S_Root, "agenda", json_array() ); json_t *wm = json_object_get( CouchWorkingMemory::S_Root, "wm" ); if( json_is_null( wm ) ){ fprintf( stderr, "Eror: Working Memory is NULL!\n" ); } else if( json_is_object( wm ) ){ // Print current state char *s = json_dumps( wm, 0 ); // printf( "Working Memory:\n%s\n", s ); free( s ); // Clear json_object_set_new( CouchWorkingMemory::S_Root, "wm", json_object() ); update(); } } json_decref( CouchWorkingMemory::S_Root ); }
int add_timestamps(json_t *object, const char *created, const char *modified, const char *replicate, baton_error_t *error) { json_t *iso_created = NULL; // Ref to be stolen by result json_t *iso_modified = NULL; // Ref to be stolen by result json_t *timestamps; init_baton_error(error); if (!json_is_object(object)) { set_baton_error(error, -1, "Failed to add timestamp data: " "target not a JSON object"); goto error; } iso_created = make_timestamp(JSON_CREATED_KEY, created, ISO8601_FORMAT, replicate, error); if (error->code != 0) goto error; iso_modified = make_timestamp(JSON_MODIFIED_KEY, modified, ISO8601_FORMAT, replicate, error); if (error->code != 0) goto error; timestamps = json_pack("[o, o]", iso_created, iso_modified); if (!timestamps) { set_baton_error(error, -1, "Failed to pack timestamp array"); goto error; } return json_object_set_new(object, JSON_TIMESTAMPS_KEY, timestamps); error: if (iso_created) json_decref(iso_created); if (iso_modified) json_decref(iso_modified); return error->code; }
Frame* CreateFrameFromJSON(json_t* root) { if (!json_is_object(root)) { SDL_LogError( SDL_LOG_CATEGORY_APPLICATION, "Frame is invalid JSON type. Expected object.\n" ); return NULL; } json_t* name = NULL; json_t *x,*y,*width,*height; Frame* frame = NULL; name = json_object_get(root, "name"); if (!json_is_string(name)) { SDL_LogError( SDL_LOG_CATEGORY_APPLICATION, "Could not find name for frame.\n" ); return NULL; } frame = CreateFrame(); if (frame == NULL) return NULL; size_t size = strlen(json_string_value(name)); frame->name = malloc(size + 1); strcpy(frame->name, json_string_value(name)); x = json_object_get(root, "x"); if (!json_is_integer(x)) { SDL_LogWarn( SDL_LOG_CATEGORY_APPLICATION, "Could not get x value for frame '%s'", frame->name ); } else frame->rect.x = json_integer_value(x); y = json_object_get(root, "y"); if (!json_is_integer(y)) { SDL_LogWarn( SDL_LOG_CATEGORY_APPLICATION, "Could not get y value for frame '%s'", frame->name ); } else frame->rect.y = json_integer_value(y); width = json_object_get(root, "width"); if (!json_is_integer(width)) { SDL_LogWarn( SDL_LOG_CATEGORY_APPLICATION, "Could not get width value for frame '%s'", frame->name ); } else frame->rect.w = json_integer_value(width); height = json_object_get(root, "height"); if (!json_is_integer(height)) { SDL_LogWarn( SDL_LOG_CATEGORY_APPLICATION, "Could not get height value for frame '%s'", frame->name ); } else frame->rect.h = json_integer_value(height); return frame; }
static int avro_type_from_json_t(json_t *json, avro_type_t *type, st_table *named_schemas, avro_schema_t *named_type) { json_t *json_type; const char *type_str; if (json_is_array(json)) { *type = AVRO_UNION; return 0; } else if (json_is_object(json)) { json_type = json_object_get(json, "type"); } else { json_type = json; } if (!json_is_string(json_type)) { avro_set_error("\"type\" field must be a string"); return EINVAL; } type_str = json_string_value(json_type); if (!type_str) { avro_set_error("\"type\" field must be a string"); return EINVAL; } /* * TODO: gperf/re2c this */ if (strcmp(type_str, "string") == 0) { *type = AVRO_STRING; } else if (strcmp(type_str, "bytes") == 0) { *type = AVRO_BYTES; } else if (strcmp(type_str, "int") == 0) { *type = AVRO_INT32; } else if (strcmp(type_str, "long") == 0) { *type = AVRO_INT64; } else if (strcmp(type_str, "float") == 0) { *type = AVRO_FLOAT; } else if (strcmp(type_str, "double") == 0) { *type = AVRO_DOUBLE; } else if (strcmp(type_str, "boolean") == 0) { *type = AVRO_BOOLEAN; } else if (strcmp(type_str, "null") == 0) { *type = AVRO_NULL; } else if (strcmp(type_str, "record") == 0) { *type = AVRO_RECORD; } else if (strcmp(type_str, "enum") == 0) { *type = AVRO_ENUM; } else if (strcmp(type_str, "array") == 0) { *type = AVRO_ARRAY; } else if (strcmp(type_str, "map") == 0) { *type = AVRO_MAP; } else if (strcmp(type_str, "fixed") == 0) { *type = AVRO_FIXED; } else if ((*named_type = find_named_schemas(type_str, named_schemas))) { *type = AVRO_LINK; } else { avro_set_error("Unknown Avro \"type\": %s", type_str); return EINVAL; } return 0; }
// Parse pools array in json config bool parse_pool_array(json_t *obj) { size_t idx; json_t *p, *val; if (!json_is_array(obj)) return false; // array of objects [ {}, {} ] json_array_foreach(obj, idx, p) { if (!json_is_object(p)) continue; for (int i = 0; i < ARRAY_SIZE(cfg_array_keys); i++) { int opt = -1; char *s = NULL; if (cfg_array_keys[i].cat != CFG_POOL) continue; val = json_object_get(p, cfg_array_keys[i].name); if (!val) continue; for (int k = 0; k < options_count(); k++) { const char *alias = cfg_array_keys[i].longname; if (alias && !strcasecmp(options[k].name, alias)) { opt = k; break; } if (!alias && !strcasecmp(options[k].name, cfg_array_keys[i].name)) { opt = k; break; } } if (opt == -1) continue; if (json_is_string(val)) { s = strdup(json_string_value(val)); if (!s) continue; // applog(LOG_DEBUG, "pool key %s '%s'", options[opt].name, s); parse_arg(options[opt].val, s); free(s); } else { // numeric or bool char buf[32] = { 0 }; double d = 0.; if (json_is_true(val)) d = 1.; else if (json_is_integer(val)) d = 1.0 * json_integer_value(val); else if (json_is_real(val)) d = json_real_value(val); snprintf(buf, sizeof(buf)-1, "%f", d); // applog(LOG_DEBUG, "pool key %s '%f'", options[opt].name, d); parse_arg(options[opt].val, buf); } } } return true; }
/* * register the client with the OP using Dynamic Client Registration */ static apr_byte_t oidc_metadata_client_register(request_rec *r, oidc_cfg *cfg, oidc_provider_t *provider, json_t **j_client, const char **response) { /* get a handle to the directory config */ oidc_dir_cfg *dir_cfg = ap_get_module_config(r->per_dir_config, &auth_openidc_module); /* assemble the JSON registration request */ json_t *data = json_object(); json_object_set_new(data, "client_name", json_string(provider->client_name)); json_object_set_new(data, "redirect_uris", json_pack("[s]", cfg->redirect_uri)); json_t *response_types = json_array(); apr_array_header_t *flows = oidc_proto_supported_flows(r->pool); int i; for (i = 0; i < flows->nelts; i++) { json_array_append_new(response_types, json_string(((const char**) flows->elts)[i])); } json_object_set_new(data, "response_types", response_types); if (provider->client_contact != NULL) { json_object_set_new(data, "contacts", json_pack("[s]", provider->client_contact)); } if (provider->client_jwks_uri) { json_object_set_new(data, "jwks_uri", json_string(provider->client_jwks_uri)); } else if (cfg->public_keys != NULL) { json_object_set_new(data, "jwks_uri", json_string( apr_psprintf(r->pool, "%s?jwks=rsa", cfg->redirect_uri))); } if (provider->id_token_signed_response_alg != NULL) { json_object_set_new(data, "id_token_signed_response_alg", json_string(provider->id_token_signed_response_alg)); } if (provider->id_token_encrypted_response_alg != NULL) { json_object_set_new(data, "id_token_encrypted_response_alg", json_string(provider->id_token_encrypted_response_alg)); } if (provider->id_token_encrypted_response_enc != NULL) { json_object_set_new(data, "id_token_encrypted_response_enc", json_string(provider->id_token_encrypted_response_enc)); } if (provider->userinfo_signed_response_alg != NULL) { json_object_set_new(data, "userinfo_signed_response_alg", json_string(provider->userinfo_signed_response_alg)); } if (provider->userinfo_encrypted_response_alg != NULL) { json_object_set_new(data, "userinfo_encrypted_response_alg", json_string(provider->userinfo_encrypted_response_alg)); } if (provider->userinfo_encrypted_response_enc != NULL) { json_object_set_new(data, "userinfo_encrypted_response_enc", json_string(provider->userinfo_encrypted_response_enc)); } json_object_set_new(data, "initiate_login_uri", json_string(cfg->redirect_uri)); json_object_set_new(data, "logout_uri", json_string( apr_psprintf(r->pool, "%s?logout=%s", cfg->redirect_uri, OIDC_GET_STYLE_LOGOUT_PARAM_VALUE))); if (cfg->default_slo_url != NULL) { json_object_set_new(data, "post_logout_redirect_uris", json_pack("[s]", cfg->default_slo_url)); } if (provider->registration_endpoint_json != NULL) { json_error_t json_error; json_t *json = json_loads(provider->registration_endpoint_json, 0, &json_error); if (json == NULL) { oidc_error(r, "JSON parsing returned an error: %s", json_error.text); } else { if (!json_is_object(json)) { oidc_error(r, "parsed JSON did not contain a JSON object"); } else { const char *key; json_t *value; json_object_foreach(json, key, value) { json_object_set(data, key, value); } } json_decref(json); }
/* Call the simple functions not covered by other tests of the public API */ int main() { json_t *value; value = json_integer(1); if(json_typeof(value) != JSON_INTEGER) fail("json_typeof failed"); if(json_is_object(value)) fail("json_is_object failed"); if(json_is_array(value)) fail("json_is_array failed"); if(json_is_string(value)) fail("json_is_string failed"); if(!json_is_integer(value)) fail("json_is_integer failed"); if(json_is_real(value)) fail("json_is_real failed"); if(!json_is_number(value)) fail("json_is_number failed"); if(json_is_true(value)) fail("json_is_true failed"); if(json_is_false(value)) fail("json_is_false failed"); if(json_is_boolean(value)) fail("json_is_boolean failed"); if(json_is_null(value)) fail("json_is_null failed"); json_decref(value); value = json_string("foo"); if(!value) fail("json_string failed"); if(strcmp(json_string_value(value), "foo")) fail("invalid string value"); if(json_string_set(value, "bar")) fail("json_string_set failed"); if(strcmp(json_string_value(value), "bar")) fail("invalid string value"); json_decref(value); value = json_string(NULL); if(value) fail("json_string(NULL) failed"); /* invalid UTF-8 */ value = json_string("a\xefz"); if(value) fail("json_string(<invalid utf-8>) failed"); value = json_string_nocheck("foo"); if(!value) fail("json_string_nocheck failed"); if(strcmp(json_string_value(value), "foo")) fail("invalid string value"); if(json_string_set_nocheck(value, "bar")) fail("json_string_set_nocheck failed"); if(strcmp(json_string_value(value), "bar")) fail("invalid string value"); json_decref(value); /* invalid UTF-8 */ value = json_string_nocheck("qu\xff"); if(!value) fail("json_string_nocheck failed"); if(strcmp(json_string_value(value), "qu\xff")) fail("invalid string value"); if(json_string_set_nocheck(value, "\xfd\xfe\xff")) fail("json_string_set_nocheck failed"); if(strcmp(json_string_value(value), "\xfd\xfe\xff")) fail("invalid string value"); json_decref(value); value = json_integer(123); if(!value) fail("json_integer failed"); if(json_integer_value(value) != 123) fail("invalid integer value"); if(json_number_value(value) != 123.0) fail("invalid number value"); if(json_integer_set(value, 321)) fail("json_integer_set failed"); if(json_integer_value(value) != 321) fail("invalid integer value"); if(json_number_value(value) != 321.0) fail("invalid number value"); json_decref(value); value = json_real(123.123); if(!value) fail("json_real failed"); if(json_real_value(value) != 123.123) fail("invalid integer value"); if(json_number_value(value) != 123.123) fail("invalid number value"); if(json_real_set(value, 321.321)) fail("json_real_set failed"); if(json_real_value(value) != 321.321) fail("invalid real value"); if(json_number_value(value) != 321.321) fail("invalid number value"); json_decref(value); value = json_true(); if(!value) fail("json_true failed"); json_decref(value); value = json_false(); if(!value) fail("json_false failed"); json_decref(value); value = json_null(); if(!value) fail("json_null failed"); json_decref(value); /* Test reference counting on singletons (true, false, null) */ value = json_true(); if(value->refcount != (size_t)-1) fail("refcounting true works incorrectly"); json_decref(value); if(value->refcount != (size_t)-1) fail("refcounting true works incorrectly"); json_incref(value); if(value->refcount != (size_t)-1) fail("refcounting true works incorrectly"); value = json_false(); if(value->refcount != (size_t)-1) fail("refcounting false works incorrectly"); json_decref(value); if(value->refcount != (size_t)-1) fail("refcounting false works incorrectly"); json_incref(value); if(value->refcount != (size_t)-1) fail("refcounting false works incorrectly"); value = json_null(); if(value->refcount != (size_t)-1) fail("refcounting null works incorrectly"); json_decref(value); if(value->refcount != (size_t)-1) fail("refcounting null works incorrectly"); json_incref(value); if(value->refcount != (size_t)-1) fail("refcounting null works incorrectly"); return 0; }
static int avro_schema_from_json_t(json_t *json, avro_schema_t *schema, st_table *named_schemas) { #ifdef _WIN32 #pragma message("#warning: Bug: '0' is not of type avro_type_t.") #else #warning "Bug: '0' is not of type avro_type_t." #endif /* We should really have an "AVRO_INVALID" type in * avro_type_t. Suppress warning below in which we set type to 0. */ avro_type_t type = (avro_type_t) 0; unsigned int i; avro_schema_t named_type = NULL; if (avro_type_from_json_t(json, &type, named_schemas, &named_type)) { return EINVAL; } switch (type) { case AVRO_LINK: *schema = avro_schema_link(named_type); break; case AVRO_STRING: *schema = avro_schema_string(); break; case AVRO_BYTES: *schema = avro_schema_bytes(); break; case AVRO_INT32: *schema = avro_schema_int(); break; case AVRO_INT64: *schema = avro_schema_long(); break; case AVRO_FLOAT: *schema = avro_schema_float(); break; case AVRO_DOUBLE: *schema = avro_schema_double(); break; case AVRO_BOOLEAN: *schema = avro_schema_boolean(); break; case AVRO_NULL: *schema = avro_schema_null(); break; case AVRO_RECORD: { json_t *json_name = json_object_get(json, "name"); json_t *json_namespace = json_object_get(json, "namespace"); json_t *json_fields = json_object_get(json, "fields"); unsigned int num_fields; const char *record_name; const char *record_namespace; if (!json_is_string(json_name)) { avro_set_error("Record type must have a \"name\""); return EINVAL; } if (!json_is_array(json_fields)) { avro_set_error("Record type must have \"fields\""); return EINVAL; } num_fields = json_array_size(json_fields); if (num_fields == 0) { avro_set_error("Record type must have at least one field"); return EINVAL; } record_name = json_string_value(json_name); if (!record_name) { avro_set_error("Record type must have a \"name\""); return EINVAL; } if (json_is_string(json_namespace)) { record_namespace = json_string_value(json_namespace); } else { record_namespace = NULL; } *schema = avro_schema_record(record_name, record_namespace); if (save_named_schemas(record_name, *schema, named_schemas)) { avro_set_error("Cannot save record schema"); return ENOMEM; } for (i = 0; i < num_fields; i++) { json_t *json_field = json_array_get(json_fields, i); json_t *json_field_name; json_t *json_field_type; json_t *json_default_value; avro_schema_t json_field_type_schema; int field_rval; if (!json_is_object(json_field)) { avro_set_error("Record field %d must be an array", i); avro_schema_decref(*schema); return EINVAL; } json_field_name = json_object_get(json_field, "name"); if (!json_field_name) { avro_set_error("Record field %d must have a \"name\"", i); avro_schema_decref(*schema); return EINVAL; } json_field_type = json_object_get(json_field, "type"); if (!json_field_type) { avro_set_error("Record field %d must have a \"type\"", i); avro_schema_decref(*schema); return EINVAL; } field_rval = avro_schema_from_json_t(json_field_type, &json_field_type_schema, named_schemas); if (field_rval) { avro_schema_decref(*schema); return field_rval; } json_default_value = json_object_get(json_field, "default"); avro_datum_t default_value = NULL; if (json_default_value) { avro_schema_t default_schema = json_field_type_schema; if (json_field_type_schema->type == AVRO_UNION) { // From the spec: "Default values for union fields correspond // to the first schema in the union." default_schema = avro_schema_union_branch(json_field_type_schema, 0); } default_value = json_t_to_avro_value(default_schema, json_default_value); if (default_value == NULL) { avro_schema_decref(*schema); return EINVAL; } } field_rval = avro_schema_record_field_append(*schema, json_string_value (json_field_name), json_field_type_schema, default_value); avro_schema_decref(json_field_type_schema); if (field_rval != 0) { avro_schema_decref(*schema); return field_rval; } } } break; case AVRO_ENUM: { json_t *json_name = json_object_get(json, "name"); json_t *json_symbols = json_object_get(json, "symbols"); const char *name; unsigned int num_symbols; if (!json_is_string(json_name)) { avro_set_error("Enum type must have a \"name\""); return EINVAL; } if (!json_is_array(json_symbols)) { avro_set_error("Enum type must have \"symbols\""); return EINVAL; } name = json_string_value(json_name); if (!name) { avro_set_error("Enum type must have a \"name\""); return EINVAL; } num_symbols = json_array_size(json_symbols); if (num_symbols == 0) { avro_set_error("Enum type must have at least one symbol"); return EINVAL; } *schema = avro_schema_enum(name); if (save_named_schemas(name, *schema, named_schemas)) { avro_set_error("Cannot save enum schema"); return ENOMEM; } for (i = 0; i < num_symbols; i++) { int enum_rval; json_t *json_symbol = json_array_get(json_symbols, i); const char *symbol; if (!json_is_string(json_symbol)) { avro_set_error("Enum symbol %d must be a string", i); avro_schema_decref(*schema); return EINVAL; } symbol = json_string_value(json_symbol); enum_rval = avro_schema_enum_symbol_append(*schema, symbol); if (enum_rval != 0) { avro_schema_decref(*schema); return enum_rval; } } } break; case AVRO_ARRAY: { int items_rval; json_t *json_items = json_object_get(json, "items"); avro_schema_t items_schema; if (!json_items) { avro_set_error("Array type must have \"items\""); return EINVAL; } items_rval = avro_schema_from_json_t(json_items, &items_schema, named_schemas); if (items_rval) { return items_rval; } *schema = avro_schema_array(items_schema); avro_schema_decref(items_schema); } break; case AVRO_MAP: { int values_rval; json_t *json_values = json_object_get(json, "values"); avro_schema_t values_schema; if (!json_values) { avro_set_error("Map type must have \"values\""); return EINVAL; } values_rval = avro_schema_from_json_t(json_values, &values_schema, named_schemas); if (values_rval) { return values_rval; } *schema = avro_schema_map(values_schema); avro_schema_decref(values_schema); } break; case AVRO_UNION: { unsigned int num_schemas = json_array_size(json); avro_schema_t s; if (num_schemas == 0) { avro_set_error("Union type must have at least one branch"); return EINVAL; } *schema = avro_schema_union(); for (i = 0; i < num_schemas; i++) { int schema_rval; json_t *schema_json = json_array_get(json, i); if (!schema_json) { avro_set_error("Cannot retrieve branch JSON"); return EINVAL; } schema_rval = avro_schema_from_json_t(schema_json, &s, named_schemas); if (schema_rval != 0) { avro_schema_decref(*schema); return schema_rval; } schema_rval = avro_schema_union_append(*schema, s); avro_schema_decref(s); if (schema_rval != 0) { avro_schema_decref(*schema); return schema_rval; } } } break; case AVRO_FIXED: { json_t *json_size = json_object_get(json, "size"); json_t *json_name = json_object_get(json, "name"); json_int_t size; const char *name; if (!json_is_integer(json_size)) { avro_set_error("Fixed type must have a \"size\""); return EINVAL; } if (!json_is_string(json_name)) { avro_set_error("Fixed type must have a \"name\""); return EINVAL; } size = json_integer_value(json_size); name = json_string_value(json_name); *schema = avro_schema_fixed(name, (int64_t) size); if (save_named_schemas(name, *schema, named_schemas)) { avro_set_error("Cannot save fixed schema"); return ENOMEM; } } break; default: avro_set_error("Unknown schema type"); return EINVAL; } return 0; }