static int s_aws_hash_table_from_json()

in source/kms.c [442:496]


static int s_aws_hash_table_from_json(
    struct aws_allocator *allocator,
    struct json_object *obj,
    struct aws_hash_table *map) {

    AWS_PRECONDITION(aws_allocator_is_valid(allocator));
    AWS_PRECONDITION(obj);
    AWS_PRECONDITION(map);

    if (aws_hash_table_init(
            map,
            allocator,
            json_object_object_length(obj),
            aws_hash_string,
            aws_hash_callback_string_eq,
            aws_hash_callback_string_destroy,
            aws_hash_callback_string_destroy) != AWS_OP_SUCCESS) {
        return AWS_OP_ERR;
    }

    struct json_object_iterator it_end = json_object_iter_end(obj);
    for (struct json_object_iterator it = json_object_iter_begin(obj); !json_object_iter_equal(&it, &it_end);
         json_object_iter_next(&it)) {
        const char *key = json_object_iter_peek_name(&it);
        struct json_object *value = json_object_iter_peek_value(&it);

        if (json_object_get_type(value) != json_type_string) {
            goto clean_up;
        }

        struct aws_string *map_key = aws_string_new_from_c_str(allocator, key);
        if (map_key == NULL) {
            goto clean_up;
        }

        struct aws_string *map_value = s_aws_string_from_json(allocator, value);
        if (map_value == NULL) {
            aws_string_destroy(map_key);
            goto clean_up;
        }

        if (aws_hash_table_put(map, map_key, map_value, NULL) != AWS_OP_SUCCESS) {
            aws_string_destroy(map_key);
            aws_string_destroy(map_value);
            goto clean_up;
        }
    }

    return AWS_OP_SUCCESS;

clean_up:
    aws_hash_table_clean_up(map);

    return AWS_OP_ERR;
}