Example #1
0
gchar *
get_value (gchar *data, gchar *key)
{
    JsonParser *parser = json_parser_new ();
    JsonReader *reader = json_reader_new (NULL);
    GError *error = NULL;
    gchar *val = NULL;

    json_parser_load_from_data (parser, data, -1, &error);
    if (error) {
        g_error_free (error);
        goto out;
    }

    json_reader_set_root (reader, json_parser_get_root (parser));
    if (!json_reader_is_object (reader)) {
        goto out;
    }

    json_reader_read_member (reader, key);
    val = g_strdup (json_reader_get_string_value (reader));
    json_reader_end_member (reader);
out:
    g_object_unref (parser);
    g_object_unref (reader);

    return val;
}
/*!
 * Read the expected QMP welcome message.
 *
 * \param socket \c GSocket to use.
 *
 * \return \c true on success, else \c false.
 */
static gboolean
clr_oci_qmp_check_welcome (GSocket *socket)
{
	GError      *error = NULL;
	JsonParser  *parser = NULL;
	JsonReader  *reader = NULL;
	GSList      *msgs = NULL;
	gsize        msg_count = 0;
	gboolean     ret;
	GString     *msg = NULL;

	g_assert (socket);

	ret = clr_oci_qmp_msg_recv (socket, 1, &msgs, &msg_count);
	if (! ret) {
		goto out;
	}

	msg = g_slist_nth_data (msgs, 0);
	g_assert (msg);

	parser = json_parser_new ();
	reader = json_reader_new (NULL);

	ret = json_parser_load_from_data (parser, msg->str, (gssize)msg->len, &error);
	if (! ret) {
		g_critical ("failed to parse json: %s", error->message);
		g_error_free (error);
		goto out;
	}

	json_reader_set_root (reader, json_parser_get_root (parser));

	/* FIXME: perform more checks on the data received */
	ret = json_reader_read_member (reader, "QMP");
	if (! ret) {
		g_critical ("unexpected json data");
		json_reader_end_member (reader);
		goto out;
	}

	json_reader_end_member (reader);

out:
	if (reader) {
		g_object_unref (reader);
	}
	if (parser) {
		g_object_unref (parser);
	}
	if (msgs) {
		clr_oci_net_msgs_free_all (msgs);
	}

	g_debug ("handled qmp welcome");

	return true;
}
/*!
 * Check a QMP "execute" response message.
 *
 * \param result Response from server.
 * \param bytes Size of \p result.
 * \param expect_empty \c true if the result is expected to be an
 *   empty json message, else \c false.
 *
 * \warning FIXME: no validation performed on non-empty QMP messages yet.
 *
 * \return \c true on success, else \c false.
 */
static gboolean
clr_oci_qmp_check_result (const char *result, gsize bytes,
		gboolean expect_empty)
{
	gboolean      ret;
	JsonParser   *parser = NULL;
	JsonReader   *reader = NULL;
	GError       *error = NULL;
	gint          count = 0;

	g_assert (result);

	parser = json_parser_new ();
	reader = json_reader_new (NULL);

	ret = json_parser_load_from_data (parser, result,
			(gssize)bytes, &error);
	if (! ret) {
		g_critical ("failed to check qmp response: %s",
				error->message);
		g_error_free (error);

		goto out;
	}

	json_reader_set_root (reader, json_parser_get_root (parser));

	ret = json_reader_read_member (reader, "return");
	if (! ret) {
		goto out;
	}

	ret = json_reader_is_object (reader);
	if (! ret) {
		goto out;
	}

	count = json_reader_count_members (reader);
	if (count && expect_empty) {
		g_critical ("expected empty object denoting success, "
				"but found %d members", count);
		goto out;
	}

	ret = true;

out:
	if (reader) {
		json_reader_end_member (reader);
		g_object_unref (reader);
	}

	if (parser) {
		g_object_unref (parser);
	}

	return ret;
}
Example #4
0
GList *
get_file_list (gchar *data)
{
    JsonParser *parser = json_parser_new ();
    JsonReader *reader = json_reader_new (NULL);
    GError *error = NULL;
    GList *val = NULL;
    gint count, i;
    simple_file *sf;

    json_parser_load_from_data (parser, data, -1, &error);
    if (error) {
        g_error_free (error);
        goto out;
    }

    json_reader_set_root (reader, json_parser_get_root (parser));
    if (!json_reader_is_object (reader)) {
        goto out;
    }

    json_reader_read_member (reader, "files");
    count = json_reader_count_elements (reader);
    for (i = 0; i < count; i++) {
        json_reader_read_element (reader, i);
        sf = malloc(sizeof (simple_file));

        json_reader_read_member (reader, "name");
        sf->name = g_strdup (json_reader_get_string_value (reader));
        json_reader_end_member (reader);

        json_reader_read_member (reader, "type");
        sf->type = g_strdup (json_reader_get_string_value (reader));
        json_reader_end_member (reader);
        val = g_list_prepend (val, sf);

        json_reader_end_member (reader);
    }

    json_reader_end_member (reader);    //files
out:
    g_object_unref (parser);
    g_object_unref (reader);

    return val;
}
static void
json_reader_set_property (GObject      *gobject,
                          guint         prop_id,
                          const GValue *value,
                          GParamSpec   *pspec)
{
  switch (prop_id)
    {
    case PROP_ROOT:
      json_reader_set_root (JSON_READER (gobject), g_value_get_boxed (value));
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
      break;
    }
}