コード例 #1
0
ファイル: json.c プロジェクト: huangjingpei/asterisk
int ast_json_to_ast_variables(struct ast_json *json_variables, struct ast_variable **variables)
{
	struct ast_json_iter *it_json_var;

	*variables = NULL;

	for (it_json_var = ast_json_object_iter(json_variables); it_json_var;
		 it_json_var = ast_json_object_iter_next(json_variables, it_json_var)) {
		struct ast_variable *new_var;
		const char *key = ast_json_object_iter_key(it_json_var);

		if (ast_strlen_zero(key)) {
			continue;
		}

		new_var = ast_variable_new(key,
		                           ast_json_string_get(ast_json_object_iter_value(it_json_var)),
		                           "");
		if (!new_var) {
			ast_variables_destroy(*variables);
			*variables = NULL;
			return -1;
		}

		ast_variable_list_append(variables, new_var);
	}

	return 0;
}
コード例 #2
0
ファイル: json.c プロジェクト: huangjingpei/asterisk
int ast_json_object_update_missing(struct ast_json *object, struct ast_json *other)
{
#if JANSSON_VERSION_HEX >= 0x020300
	return json_object_update_missing((json_t *)object, (json_t *)other);
#else
	struct ast_json_iter *iter = ast_json_object_iter(other);
	int ret = 0;

	if (object == NULL || other == NULL) {
		return -1;
	}

	while (iter != NULL && ret == 0) {
		const char *key = ast_json_object_iter_key(iter);

		if (ast_json_object_get(object, key) == NULL) {
			struct ast_json *value = ast_json_object_iter_value(iter);

			if (!value || ast_json_object_set(object, key, ast_json_ref(value))) {
				ret = -1;
			}
		}
		iter = ast_json_object_iter_next(other, iter);
	}
	return ret;
#endif
}
コード例 #3
0
static int json_to_ast_variables(struct ast_json *json_variables, struct ast_variable **variables)
{
    struct ast_variable *current = NULL;
    struct ast_json_iter *it_json_var;

    for (it_json_var = ast_json_object_iter(json_variables); it_json_var;
            it_json_var = ast_json_object_iter_next(json_variables, it_json_var)) {
        struct ast_variable *new_var;

        new_var = ast_variable_new(ast_json_object_iter_key(it_json_var),
                                   ast_json_string_get(ast_json_object_iter_value(it_json_var)),
                                   "");
        if (!new_var) {
            ast_variables_destroy(*variables);
            *variables = NULL;
            return 1;
        }

        if (!current) {
            *variables = new_var;
            current = *variables;
        } else {
            current->next = new_var;
            current = new_var;
        }
    }

    return 0;
}
コード例 #4
0
/*! \brief Helper function which compares two json objects and sees if they are equal, but only looks at the criteria provided */
static int sorcery_json_equal(struct ast_json *object, struct ast_json *criteria)
{
	struct ast_json_iter *field;

	for (field = ast_json_object_iter(criteria); field; field = ast_json_object_iter_next(criteria, field)) {
		struct ast_json *object_field = ast_json_object_get(object, ast_json_object_iter_key(field));

		if (!object_field || !ast_json_equal(object_field, ast_json_object_iter_value(field))) {
			return 0;
		}
	}

	return 1;
}
コード例 #5
0
ファイル: json.c プロジェクト: MattheusNiels/BackupCerberus
enum ast_json_to_ast_vars_code ast_json_to_ast_variables(struct ast_json *json_variables, struct ast_variable **variables)
{
	struct ast_json_iter *it_json_var;

	*variables = NULL;

	for (it_json_var = ast_json_object_iter(json_variables); it_json_var;
		it_json_var = ast_json_object_iter_next(json_variables, it_json_var)) {
		struct ast_variable *new_var;
		const char *key = ast_json_object_iter_key(it_json_var);
		const char *value;
		struct ast_json *json_value;

		if (ast_strlen_zero(key)) {
			continue;
		}

		json_value = ast_json_object_iter_value(it_json_var);
		if (ast_json_typeof(json_value) != AST_JSON_STRING) {
			/* Error: Only strings allowed */
			ast_variables_destroy(*variables);
			*variables = NULL;
			return AST_JSON_TO_AST_VARS_CODE_INVALID_TYPE;
		}
		value = ast_json_string_get(json_value);
		/* Should never be NULL.  Otherwise, how could it be a string type? */
		ast_assert(value != NULL);
		if (!value) {
			/* To be safe. */
			continue;
		}
		new_var = ast_variable_new(key, value, "");
		if (!new_var) {
			/* Error: OOM */
			ast_variables_destroy(*variables);
			*variables = NULL;
			return AST_JSON_TO_AST_VARS_CODE_OOM;
		}

		ast_variable_list_append(variables, new_var);
	}

	return AST_JSON_TO_AST_VARS_CODE_SUCCESS;
}
コード例 #6
0
/*! \brief Helper function which converts a json object to a sorcery object set */
static struct ast_variable *sorcery_json_to_objectset(struct ast_json *json)
{
	struct ast_json_iter *field;
	struct ast_variable *objset = NULL;

	for (field = ast_json_object_iter(json); field; field = ast_json_object_iter_next(json, field)) {
		struct ast_json *value = ast_json_object_iter_value(field);
		struct ast_variable *variable = ast_variable_new(ast_json_object_iter_key(field), ast_json_string_get(value), "");

		if (!variable) {
			ast_variables_destroy(objset);
			return NULL;
		}

		variable->next = objset;
		objset = variable;
	}

	return objset;
}
コード例 #7
0
ファイル: resource_channels.c プロジェクト: vzar/asterisk
static int ari_channels_set_channel_vars(struct ast_channel *chan,
        struct ast_json *variables, struct ast_ari_response *response)
{
    struct ast_json_iter *i;

    if (!variables) {
        /* nothing to do */
        return 0;
    }

    for (i = ast_json_object_iter(variables); i;
            i = ast_json_object_iter_next(variables, i)) {
        if (ari_channels_set_channel_var(
                    chan, ast_json_object_iter_key(i),
                    ast_json_string_get(ast_json_object_iter_value(i)),
                    response)) {
            /* response filled in by called function */
            return -1;
        }
    }

    return 0;
}