sprint_message(
    message_t *message)
{
    int i;
    static int first_message = 1;
    GString *result;

    char *json_file;
    char *json_process;
    char *json_running_on;
    char *json_component;
    char *json_module;
    char *json_msg;

    if (message == NULL)
	return NULL;

    message_indent = 4;
    json_file = ammessage_encode_json(message->file);
    json_process = ammessage_encode_json(message->process);
    json_running_on = ammessage_encode_json(message->running_on);
    json_component = ammessage_encode_json(message->component);
    json_module = ammessage_encode_json(message->module);

    result = g_string_sized_new(512);
    if (first_message) {
	first_message = 0;
    } else {
	g_string_append_printf(result, ",\n");
    }
    g_string_append_printf(result,
        "  {\n" \
        "    \"source_filename\" : \"%s\",\n" \
        "    \"source_line\" : \"%d\",\n" \
        "    \"severity\" : \"%s\",\n" \
        "    \"process\" : \"%s\",\n" \
        "    \"running_on\" : \"%s\",\n" \
        "    \"component\" : \"%s\",\n" \
        "    \"module\" : \"%s\",\n" \
        "    \"code\" : \"%d\",\n" \
        , json_file, message->line, severity_name(message->severity), json_process, json_running_on, json_component, json_module, message->code);
    for (i = 0; message->arg_array[i].key != NULL; i++) {
	char *json_key = ammessage_encode_json(message->arg_array[i].key);
	char *result_value = sprint_message_value(&message->arg_array[i].value);
	g_string_append_printf(result,
	    "    \"%s\" : %s,\n", json_key, result_value);
	g_free(json_key);
	g_free(result_value);
    }
    if (!message->msg) {
	set_message(message, 0);
    }
    json_msg = ammessage_encode_json(message->msg);
    g_string_append_printf(result,
        "    \"message\" : \"%s\"" \
        , json_msg);
    if (message->hint) {
	char *json_hint = ammessage_encode_json(message->hint);
	g_string_append_printf(result,
			",\n    \"hint\" : \"%s\"" \
		        , message->hint);
	g_free(json_hint);
    }
    g_string_append_printf(result,
	"\n  }");

    g_free(json_file);
    g_free(json_process);
    g_free(json_running_on);
    g_free(json_component);
    g_free(json_module);
    g_free(json_msg);

    return g_string_free(result, FALSE);
}
sprint_message(
    message_t *message)
{
    int i;
    static int first_message = 1;
    GString *result;

    char *json_file;
    char *json_process;
    char *json_running_on;
    char *json_component;
    char *json_module;
    char *json_msg;

    if (message == NULL)
	return NULL;

    message_indent = 4;
    json_file = ammessage_encode_json(message->file);
    json_process = ammessage_encode_json(message->process);
    json_running_on = ammessage_encode_json(message->running_on);
    json_component = ammessage_encode_json(message->component);
    json_module = ammessage_encode_json(message->module);

    result = g_string_sized_new(512);
    if (first_message) {
	first_message = 0;
    } else {
	g_string_append_printf(result, ",\n");
    }
    g_string_append_printf(result,
        "  {\n" \
        "    \"source_filename\" : \"%s\",\n" \
        "    \"source_line\" : \"%d\",\n" \
        "    \"severity\" : \"%s\",\n" \
        "    \"process\" : \"%s\",\n" \
        "    \"running_on\" : \"%s\",\n" \
        "    \"component\" : \"%s\",\n" \
        "    \"module\" : \"%s\",\n" \
        "    \"code\" : \"%d\",\n" \
        , json_file, message->line, severity_name(message->severity), json_process, json_running_on, json_component, json_module, message->code);
    for (i = 0; message->arg_array[i].key != NULL; i++) {
	char *json_key = ammessage_encode_json(message->arg_array[i].key);
	char *result_value = sprint_message_value(&message->arg_array[i].value);
	g_string_append_printf(result,
	    "    \"%s\" : %s,\n", json_key, result_value);
	g_free(json_key);
	g_free(result_value);
    }
    if (!message->msg) {
	set_message(message, 0);
    }
    json_msg = ammessage_encode_json(message->msg);
    g_string_append_printf(result,
        "    \"message\" : \"%s\"" \
        , json_msg);
    if (message->hint) {
	char *json_hint = ammessage_encode_json(message->hint);
	g_string_append_printf(result,
			",\n    \"hint\" : \"%s\"" \
		        , message->hint);
	g_free(json_hint);
    }
    g_string_append_printf(result,
	"\n  }");

    g_free(json_file);
    g_free(json_process);
    g_free(json_running_on);
    g_free(json_component);
    g_free(json_module);
    g_free(json_msg);

    return g_string_free(result, FALSE);
}