Exemplo n.º 1
0
int janssonmod_array_size(struct sip_msg* msg, char* path_in, char* src_in, char* dst)
{
	str src_s;
	str path_s;
	pv_spec_t *dst_pv;
	pv_value_t dst_val;

	if (fixup_get_svalue(msg, (gparam_p)src_in, &src_s) != 0) {
		ERR("cannot get json string value\n");
		return -1;
	}

	if (fixup_get_svalue(msg, (gparam_p)path_in, &path_s) != 0) {
		ERR("cannot get path string value\n");
		return -1;
	}

	dst_pv = (pv_spec_t *)dst;

	json_t* json = NULL;
	json_error_t parsing_error;

	json = json_loads(src_s.s, JSON_REJECT_DUPLICATES, &parsing_error);

	if(!json) {
		ERR("json error at line %d: %s\n",
				parsing_error.line, parsing_error.text);
		goto fail;
	}

	char* path = path_s.s;

	json_t* v = json_path_get(json, path);
	if(!v) {
		ERR("failed to find %s in json\n", path);
		goto fail;
	}

	if(!json_is_array(v)) {
		ERR("value at %s is not an array\n", path);
		goto fail;
	}

	int size = json_array_size(v);
	dst_val.ri = size;
	dst_val.flags = PV_TYPE_INT|PV_VAL_INT;

	dst_pv->setf(msg, &dst_pv->pvp, (int)EQ_T, &dst_val);

	json_decref(json);
	return 1;

fail:
	json_decref(json);
	return -1;
}
Exemplo n.º 2
0
int janssonmod_get(struct sip_msg* msg, char* path_in, char* src_in, char* dst)
{
	str src_s;
	str path_s;
	pv_spec_t *dst_pv;
	pv_value_t dst_val;

	if (fixup_get_svalue(msg, (gparam_p)src_in, &src_s) != 0) {
		ERR("cannot get json string value\n");
		return -1;
	}

	if (fixup_get_svalue(msg, (gparam_p)path_in, &path_s) != 0) {
		ERR("cannot get path string value\n");
		return -1;
	}

	dst_pv = (pv_spec_t *)dst;

	json_t* json = NULL;
	json_error_t parsing_error;

	json = json_loads(src_s.s, JSON_REJECT_DUPLICATES, &parsing_error);

	if(!json) {
		ERR("failed to parse: %.*s\n", src_s.len, src_s.s);
		ERR("json error at line %d: %s\n",
				parsing_error.line, parsing_error.text);
		goto fail;
	}

	char* path = path_s.s;

	json_t* v = json_path_get(json, path);
	if(!v) {
		goto fail;
	}

	char* freeme = NULL;

	if(jansson_to_val(&dst_val, &freeme, v)<0) goto fail;

	dst_pv->setf(msg, &dst_pv->pvp, (int)EQ_T, &dst_val);

	if(freeme!=NULL) {
		free(freeme);
	}

	json_decref(json);
	return 1;

fail:
	json_decref(json);
	return -1;
}
Exemplo n.º 3
0
esdm_config_backend_t* esdm_config_get_metadata_coordinator(esdm_instance_t* esdm)
{
	DEBUG_ENTER;

	json_t *root = (json_t*) esdm->config->json;

	json_t *elem = json_path_get(root, "$.esdm.metadata.type");
	DEBUG("json_path_get (metadata backend) => %p -> %s\n",  elem, json_string_value(elem));
	#ifndef NDEBUG
	print_json(elem);
	#endif


	esdm_config_backend_t *config_backend = (esdm_config_backend_t*) malloc(sizeof(esdm_config_backend_t));
	config_backend->type = json_string_value(elem);
	config_backend->esdm = root;
	config_backend->backend = json_path_get(root, "$.esdm.metadata");

	elem = json_object_get(config_backend->backend, "id");
	config_backend->id = json_string_value(elem);
	elem = json_object_get(config_backend->backend, "target");
	config_backend->target = json_string_value(elem);

	elem = json_object_get(config_backend->backend, "accessibility");
	if (elem != NULL){
		const char * str = json_string_value(elem);
		if (strcasecmp(str, "global") == 0){
			config_backend->data_accessibility = ESDM_ACCESSIBILITY_GLOBAL;
		}else if (strcasecmp(str, "local") == 0){
			config_backend->data_accessibility = ESDM_ACCESSIBILITY_NODELOCAL;
		}else{
			ESDM_ERROR("Unknown accessibility!");
		}
	} else
		config_backend->data_accessibility = ESDM_ACCESSIBILITY_GLOBAL;
	return config_backend;
}
Exemplo n.º 4
0
void BEJSON::json_path_query ( const std::string& json_path, fmx::Data& results ) {
	
	json_t * node = json_path_get ( document, json_path.c_str() );
		
	if ( node ) {

		std::string string_result;
			
		switch ( json_typeof ( node ) ) {
				
			case JSON_OBJECT:
			case JSON_ARRAY:
				string_result = (char *)json_dumps ( node, 0 );
				SetResult ( string_result, results );
				break;
				
			case JSON_STRING:
				string_result = (char *)json_string_value ( node );
				SetResult ( string_result, results );
				break;
				
			case JSON_INTEGER:
			case JSON_REAL:
				string_result = (char *)json_number_value_as_string ( node );
				SetResult ( string_result, results );
				break;

			case JSON_TRUE:
				SetResult ( true, results );
				break;
				
			case JSON_FALSE:
				SetResult ( false, results );
				break;
				
			case JSON_NULL:
			default:
				break;
		}
		
	} else {
		throw BEJSON_Exception ( kBE_JSON_JSONPathDoesNotExistError );
	}
			
} // json_path_query
Exemplo n.º 5
0
int janssonmod_get_helper(sip_msg_t* msg, str *path_s, str *src_s, pv_spec_t *dst_pv)
{

	pv_value_t dst_val;
	json_t* json = NULL;
	json_error_t parsing_error;

	json = json_loads(src_s->s, JSON_REJECT_DUPLICATES, &parsing_error);

	if(!json) {
		ERR("failed to parse: %.*s\n", src_s->len, src_s->s);
		ERR("json error at line %d: %s\n",
				parsing_error.line, parsing_error.text);
		goto fail;
	}

	char* path = path_s->s;

	json_t* v = json_path_get(json, path);
	if(!v) {
		goto fail;
	}

	char* freeme = NULL;

	if(jansson_to_val(&dst_val, &freeme, v)<0) goto fail;

	dst_pv->setf(msg, &dst_pv->pvp, (int)EQ_T, &dst_val);

	if(freeme!=NULL) {
		free(freeme);
	}

	json_decref(json);
	return 1;

fail:
	json_decref(json);
	return -1;
}
Exemplo n.º 6
0
/**
 *	Fetches backends
 *
 *
 */
esdm_config_backends_t* esdm_config_get_backends(esdm_instance_t* esdm)
{
	ESDM_DEBUG(__func__);

	json_t *root = (json_t*) esdm->config->json;

	// fetch configured backends
	json_t *element = json_path_get(root, "$.esdm.backends");

	esdm_config_backends_t* config_backends = (esdm_config_backends_t*) malloc(sizeof(esdm_config_backends_t));

	if (element)
	{
		if (json_typeof(element) == JSON_ARRAY)
		{
			// Element is array, therefor may contain valid backend configurations
			size_t size = json_array_size(element);

			esdm_config_backend_t* backends;
			backends = (esdm_config_backend_t*) malloc(sizeof(esdm_config_backend_t)*size);

			//printf("JSON Array of %ld element%s:\n", size, json_plural(size));

			size_t i, j;
			for (i = 0; i < size; i++) {
				//print_json_aux(json_array_get(element, i), 0);

				json_t *backend = json_array_get(element, i);
				json_t *elem = NULL;

				elem = json_object_get(backend, "type");
				backends[i].type = json_string_value(elem);

				elem = json_object_get(backend, "id");
				backends[i].id = json_string_value(elem);
				for (j = 0; j < i; j++) {
					if(strcmp(backends[i].id, backends[j].id) == 0){
						printf("ERROR two backends with the same ID found: %s\n", backends[i].id);
						ESDM_ERROR("Aborting!");
					}
				}

				elem = json_object_get(backend, "target");
				backends[i].target = json_string_value(elem);
				backends[i].performance_model = json_object_get(backend, "performance-model");
				DEBUG("type=%s id = %s target=%s\n", backends[i].type,
					backends[i].id,
					backends[i].target);

				elem = json_object_get(backend, "max-threads-per-node");
				if (elem == NULL){
					backends[i].max_threads_per_node = 0;
				}else{
					backends[i].max_threads_per_node = json_integer_value(elem);
				}

				elem = json_object_get(backend, "max-global-threads");
				if (elem == NULL){
					backends[i].max_global_threads = 0;
				}else{
					backends[i].max_global_threads = json_integer_value(elem);
				}

				elem = json_object_get(backend, "accessibility");
				if (elem != NULL){
					const char * str = json_string_value(elem);
					if (strcasecmp(str, "global") == 0){
						backends[i].data_accessibility = ESDM_ACCESSIBILITY_GLOBAL;
					}else if (strcasecmp(str, "local") == 0){
						backends[i].data_accessibility = ESDM_ACCESSIBILITY_NODELOCAL;
					}else{
						ESDM_ERROR("Unknown accessibility!");
					}
				} else
					backends[i].data_accessibility = ESDM_ACCESSIBILITY_GLOBAL;

				elem = json_object_get(backend, "max-fragment-size");
				if (elem == NULL){
					backends[i].max_fragment_size = 10*1024*1024;
				}else{
					backends[i].max_fragment_size = json_integer_value(elem);
				}

				backends[i].esdm = root;
				backends[i].backend = backend;
			}

			config_backends->count = size;
			config_backends->backends = backends;

		}
	} else {
		ESDM_ERROR("Invalid configuration! /esdm/backends is not an array.");
	}


	return config_backends;
}
std::vector<std::unique_ptr<model::Game>>
JSONGameListParser::parseGameList(std::basic_istream<gkbyte_t> & is)
{
	json_t * root = nullptr;
	json_error_t error;
	std::vector<std::unique_ptr<model::Game>> games;

	{
		std::string jsonTree = utils::String::createFromStream(is);
		root = json_loads(jsonTree.c_str(), 0, &error);
	}

	if(root)
	{
		json_t * gamesList = json_path_get(root, this->data->gamesListPath.c_str());
		if(json_is_array(gamesList))
		{
			for(size_t i = 0; i < json_array_size(gamesList); i++)
			{
				json_t * gameNode = json_array_get(gamesList, i);
				std::unique_ptr<model::GenericGame> game = std::make_unique<model::GenericGame>();
				game->setId(json_string_value(json_path_get(gameNode, this->data->gameIdPath.c_str())));
				game->setName(json_string_value(json_path_get(gameNode, this->data->gameNamePath.c_str())));
				if(!this->data->gameDescriptionPath.empty())
				{
					game->setDescription(json_string_value(json_path_get(gameNode, this->data->gameDescriptionPath.c_str())));
				}
				if(!this->data->gameHomepagePath.empty())
				{
					game->setHomepage(json_string_value(json_path_get(gameNode, this->data->gameHomepagePath.c_str())));
				}

				json_t * gamePlatforms = json_path_get(gameNode, this->data->gamePlatformsPath.c_str());

				std::set<model::Platform> platforms;
				for(size_t p = 0; p < json_array_size(gamePlatforms); p++)
				{
					std::string platform = json_string_value(json_path_get(json_array_get(gamePlatforms, p), "$"));
					if(platform == this->data->platformWin32Id)
					{
						platforms.insert(model::Platform::WIN_32);
					}
					else if(platform == this->data->platformMac32Id)
					{
						platforms.insert(model::Platform::MAC_32);
					}
					else if(platform == this->data->platformLin32Id)
					{
						platforms.insert(model::Platform::LIN_32);
					}
					else if(platform == this->data->platformLin64Id)
					{
						platforms.insert(model::Platform::LIN_64);
					}
				}
				game->setPlatforms(std::move(platforms));

				games.push_back(std::move(game));
			}
		}
	}
	json_decref(root);
	return games;
}