Exemple #1
0
/**
 * json_reader_count_elements:
 * @reader: a #JsonReader
 *
 * Counts the elements of the current position, if @reader is
 * positioned on an array
 *
 * Return value: the number of elements, or -1. In case of failure
 *   the #JsonReader is set in an error state
 *
 * Since: 0.12
 */
gint
json_reader_count_elements (JsonReader *reader)
{
  JsonReaderPrivate *priv;

  g_return_val_if_fail (JSON_IS_READER (reader), -1);

  priv = reader->priv;

  if (priv->current_node == NULL)
    {
      json_reader_set_error (reader, JSON_READER_ERROR_INVALID_NODE,
                             _("No node available at the current position"));
      return -1;
    }

  if (!JSON_NODE_HOLDS_ARRAY (priv->current_node))
    {
      json_reader_set_error (reader, JSON_READER_ERROR_NO_ARRAY,
                             _("The current position holds a '%s' and not an array"),
                             json_node_type_get_name (JSON_NODE_TYPE (priv->current_node)));
      return -1;
    }

  return json_array_get_length (json_node_get_array (priv->current_node));
}
Exemple #2
0
/**
 * json_node_hash:
 * @key: (type JsonNode): a JSON node to hash
 *
 * Calculate a hash value for the given @key (a #JsonNode).
 *
 * The hash is calculated over the node and its value, recursively. If the node
 * is immutable, this is a fast operation; otherwise, it scales proportionally
 * with the size of the node’s value (for example, with the number of members
 * in the #JsonObject if this node contains an object).
 *
 * Returns: hash value for @key
 * Since: 1.2
 */
guint
json_node_hash (gconstpointer key)
{
  JsonNode *node;  /* unowned */

  /* These are all randomly generated and arbitrary. */
  const guint value_hash = 0xc19e75ad;
  const guint array_hash = 0x865acfc2;
  const guint object_hash = 0x3c8f3135;

  node = (JsonNode *) key;

  /* XOR the hash values with a (constant) random number depending on the node’s
   * type so that empty values, arrays and objects do not all collide at the
   * hash value 0. */
  switch (node->type)
    {
    case JSON_NODE_NULL:
      return 0;
    case JSON_NODE_VALUE:
      return value_hash ^ json_value_hash (node->data.value);
    case JSON_NODE_ARRAY:
      return array_hash ^ json_array_hash (json_node_get_array (node));
    case JSON_NODE_OBJECT:
      return object_hash ^ json_object_hash (json_node_get_object (node));
    default:
      g_assert_not_reached ();
    }
}
static void
skypeweb_got_authrequests(SkypeWebAccount *sa, JsonNode *node, gpointer user_data)
{
	JsonArray *requests;
	guint index, length;
	time_t latest_timestamp = 0;
	
	requests = json_node_get_array(node);
	length = json_array_get_length(requests);
	for(index = 0; index < length; index++)
	{
		JsonObject *request = json_array_get_object_element(requests, index);
		const gchar *event_time_iso = json_object_get_string_member(request, "event_time_iso");
		const gchar *sender = json_object_get_string_member(request, "sender");
		const gchar *greeting = json_object_get_string_member(request, "greeting");
		time_t event_timestamp = purple_str_to_time(event_time_iso, TRUE, NULL, NULL, NULL);
		
		latest_timestamp = MAX(latest_timestamp, event_timestamp);
		if (sa->last_authrequest && event_timestamp <= sa->last_authrequest)
			continue;
		
		purple_account_request_authorization(
				sa->account, sender, NULL,
				NULL, greeting, FALSE,
				skypeweb_auth_accept_cb, skypeweb_auth_reject_cb, purple_buddy_new(sa->account, sender, NULL));
		
	}
	
	sa->last_authrequest = latest_timestamp;
}
static void
process_results (RBPodcastSearchITunes *search, JsonParser *parser)
{
	JsonObject *container;
	JsonArray *results;
	guint i;

	container = json_node_get_object (json_parser_get_root (parser));
	results = json_node_get_array (json_object_get_member (container, "results"));

	for (i = 0; i < json_array_get_length (results); i++) {
		JsonObject *feed;
		RBPodcastChannel *channel;

		feed = json_array_get_object_element (results, i);

		/* check wrapperType==track, kind==podcast ? */

		channel = g_new0 (RBPodcastChannel, 1);

		channel->url = g_strdup (json_object_get_string_member (feed, "collectionViewUrl"));
		channel->title = g_strdup (json_object_get_string_member (feed, "collectionName"));
		channel->author = g_strdup (json_object_get_string_member (feed, "artistName"));
		channel->img = g_strdup (json_object_get_string_member (feed, "artworkUrl100"));	/* 100? */
		channel->is_opml = FALSE;

		channel->num_posts = json_object_get_int_member (feed, "trackCount");

		rb_debug ("got result %s (%s)", channel->title, channel->url);
		rb_podcast_search_result (RB_PODCAST_SEARCH (search), channel);
		rb_podcast_parse_channel_free (channel);
	}
}
Exemple #5
0
JsonNode*
cometd_msg_extract_connect(JsonNode* payload)
{
    JsonNode* connect = NULL;
    JsonArray* arr = json_node_get_array(payload);
    GList* msgs = json_array_get_elements(arr);

    gint i;
    gint index = -1;

    GList* item;

    for (i = 0, item = msgs; item; item = g_list_next(item), ++i)
    {
        JsonNode* msg = item->data;
        char* channel = cometd_msg_channel(msg);

        if (strcmp(COMETD_CHANNEL_META_CONNECT, channel) == 0)
            index = i;

        free(channel);
    }

    if (index > -1)
    {
        connect = json_array_dup_element(arr, index);
        json_array_remove_element(arr, index);
    }

    g_list_free(msgs);

    return connect;
}
static gboolean
parse_reviews (GsPlugin *plugin, JsonParser *parser, GsApp *app, GCancellable *cancellable, GError **error)
{
	GsAuth *auth;
	JsonArray *array;
	const gchar *consumer_key = NULL;
	guint i;

	auth = gs_plugin_get_auth_by_id (plugin, "ubuntuone");
	if (auth != NULL)
		consumer_key = gs_auth_get_metadata_item (auth, "consumer-key");

	if (!JSON_NODE_HOLDS_ARRAY (json_parser_get_root (parser)))
		return FALSE;
	array = json_node_get_array (json_parser_get_root (parser));
	for (i = 0; i < json_array_get_length (array); i++) {
		g_autoptr(AsReview) review = NULL;

		/* Read in from JSON... (skip bad entries) */
		review = as_review_new ();
		if (parse_review (review, consumer_key, json_array_get_element (array, i)))
			gs_app_add_review (app, review);
	}

	return TRUE;
}
Exemple #7
0
static gboolean
parse_emojione_aliases_ascii (JsonNode  *node,
                              EmojiData *data)
{
    JsonArray *aliases_ascii;
    guint i, length;

    if (json_node_get_node_type (node) != JSON_NODE_ARRAY) {
        g_warning ("'aliases_ascii' element is not array");
        return FALSE;
    }

    aliases_ascii = json_node_get_array (node);
    length = json_array_get_length (aliases_ascii);
    for (i = 0; i < length; i++) {
        const gchar *alias = json_array_get_string_element (aliases_ascii, i);
        GSList *duplicated = g_slist_find_custom (data->annotations,
                                                  alias,
                                                  (GCompareFunc) g_strcmp0);
        if (duplicated == NULL) {
            data->annotations = g_slist_prepend (data->annotations,
                                                 g_strdup (alias));
        }
    }

    return TRUE;
}
Exemple #8
0
static gboolean
parse_emojione_keywords (JsonNode  *node,
                         EmojiData *data)
{
#if 0
    JsonArray *keywords;
    guint i, length;

    if (json_node_get_node_type (node) != JSON_NODE_ARRAY) {
        g_warning ("'keywords' element is not array");
        return FALSE;
    }

    keywords = json_node_get_array (node);
    length = json_array_get_length (keywords);
    for (i = 0; i < length; i++) {
        const gchar *keyword = json_array_get_string_element (keywords, i);
        GSList *duplicated = g_slist_find_custom (data->annotations,
                                                  keyword,
                                                  (GCompareFunc) g_strcmp0);
        if (duplicated == NULL) {
            data->annotations = g_slist_prepend (data->annotations,
                                                 g_strdup (keyword));
        }
    }

#endif
    return TRUE;
}
Exemple #9
0
/**
 * wbl_string_set_union_dependencies:
 * @set: (transfer floating): a #WblStringSet
 * @dependencies: a JSON object mapping property names to arrays of property
 *    names (or to object instances)
 *
 * Calculate the transitive union of the dependencies of the elements of an
 * initial @set and return it as a new #WblStringSet. @dependencies is treated
 * as a map of property names to dependencies; the function essentially
 * calculates
 *    output = set ∪ ⋃_{d ϵ output} dependencies(d)
 *
 * Complexity: O(S * D) in the size S of @set and number D of @dependencies keys
 * Returns: the transitive dependency set of @set
 * Since: 0.2.0
 */
WblStringSet *
wbl_string_set_union_dependencies (WblStringSet  *set,
                                   JsonObject    *dependencies)
{
	WblStringSet *output = NULL;
	guint old_output_size;

	g_return_val_if_fail (_wbl_string_set_is_valid (set), NULL);
	g_return_val_if_fail (dependencies != NULL, NULL);

	output = _wbl_string_set_new ();

	wbl_string_set_ref_sink (set);

	do {
		GHashTableIter iter;
		const gchar *property_name;

		old_output_size = g_hash_table_size (output->set);

		g_hash_table_iter_init (&iter, set->set);

		while (g_hash_table_iter_next (&iter, (gpointer *) &property_name, NULL)) {
			JsonNode *dependency_value;
			guint i, len;

			/* Add the dependency key. */
			_wbl_string_set_add (output, property_name);

			/* See if there are any values associated with the
			 * dependency key. */
			dependency_value = json_object_get_member (dependencies,
			                                           property_name);

			/* Ignore schema dependencies; we only care about
			 * property dependencies, for which we add all
			 * dependent properties. */
			if (dependency_value != NULL &&
			    JSON_NODE_HOLDS_ARRAY (dependency_value)) {
				JsonArray *array;

				array = json_node_get_array (dependency_value);

				for (i = 0, len = json_array_get_length (array);
				     i < len;
				     i++) {
					_wbl_string_set_add (output,
					                     json_array_get_string_element (array, i));
				}
			}
		}
	} while (old_output_size != g_hash_table_size (output->set));

	wbl_string_set_unref (set);

	output->state |= STATE_IMMUTABLE;

	return output;
}
/* Called by RPC transport. */
gchar* 
searpc_server_call_function (const char *svc_name,
                             gchar *func, gsize len, gsize *ret_len)
{
    SearpcService *service;
    JsonParser *parser;
    JsonNode *root;
    JsonArray *array;
    gchar* ret;
    GError *error = NULL;
#ifdef PROFILE
    struct timeval start, end, intv;

    gettimeofday(&start, NULL);
#endif

    service = g_hash_table_lookup (service_table, svc_name);
    if (!service) {
        char buf[256];
        snprintf (buf, 255, "cannot find service %s.", svc_name);
        return error_to_json (501, buf, ret_len);
    }
          
    parser = json_parser_new ();
    
    if (!json_parser_load_from_data (parser, func, len, &error)) {
        char buf[512];
        snprintf (buf, 511, "failed to parse RPC call: %s\n", error->message);
        g_object_unref (parser);        
        return error_to_json (511, buf, ret_len);
    }

    root = json_parser_get_root (parser);
    array = json_node_get_array (root);

    const char *fname = json_array_get_string_element(array, 0);
    FuncItem *fitem = g_hash_table_lookup(service->func_table, fname);
    if (!fitem) {
        char buf[256];
        snprintf (buf, 255, "cannot find function %s.", fname);
        g_object_unref (parser);
        return error_to_json (500, buf, ret_len);
    }

    ret = fitem->marshal->mfunc (fitem->func, array, ret_len);

#ifdef PROFILE
    gettimeofday(&end, NULL);
    timersub(&end, &start, &intv);
    g_debug ("[searpc] Time spend in call %s: %ds %dus\n",
             fname, intv.tv_sec, intv.tv_usec);
#endif

    g_object_unref (parser);

    return ret;
}
Exemple #11
0
static GVariant *
parse_json_tuple (JsonNode *node,
                  const GVariantType *child_type,
                  GError **error)
{
  GVariant *result = NULL;
  GPtrArray *children;
  GVariant *value;
  JsonArray *array;
  guint length;
  guint i;

  children = g_ptr_array_new ();

  if (!check_type (node, JSON_NODE_ARRAY, 0, error))
    goto out;

  array = json_node_get_array (node);
  length = json_array_get_length (array);

  for (i = 0; i < length; i++)
    {
      value = NULL;
      if (child_type == NULL)
        {
          g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_DATA,
                       "Too many values in tuple/struct");
        }
      else
        {
          value = parse_json (json_array_get_element (array, i),
                              child_type, error);
        }

      if (!value)
        goto out;

      g_ptr_array_add (children, value);
      child_type = g_variant_type_next (child_type);
    }

  if (child_type)
    {
      g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_DATA,
                   "Too few values in tuple/struct");
      goto out;
    }

  result = g_variant_new_tuple ((GVariant *const *)children->pdata,
                                children->len);
  children->len = 0;

out:
  g_ptr_array_foreach (children, (GFunc)g_variant_unref, NULL);
  g_ptr_array_free (children, TRUE);
  return result;
}
/*!
 * Recursive function that handles converging \c JsonNode's to \c
 * GNode's.
 *
 * \param root \c Root JsonNode to convert.
 * \param node \c GNode.
 * \param parsing_array \c true if handling an array, else \c false.
 */
static void
clr_oci_json_parse_aux(JsonNode* root, GNode* node, bool parsing_array) {
	guint i;

	g_assert (root);
	g_assert (node);

	if (JSON_NODE_TYPE(root) == JSON_NODE_OBJECT) {
		JsonObject *object = json_node_get_object(root);

		if (object) {
			guint j;
			guint size;
			GList* keys, *key = NULL;
			GList* values, *value = NULL;

			size = json_object_get_size(object);
			keys = json_object_get_members(object);
			values = json_object_get_values(object);
			node = g_node_append(node, g_node_new(NULL));

			for (j = 0, key = keys, value = values; j < size; j++) {
				if (key) {
					node = g_node_append(node->parent, g_node_new(g_strdup(key->data)));
				}
				if (value) {
					clr_oci_json_parse_aux(value->data, node, false);
				}

				key = g_list_next(key);
				value = g_list_next(value);
			}

			if (keys) {
				g_list_free(keys);
			}
			if (values) {
				g_list_free(values);
			}
		}
	} else if (JSON_NODE_TYPE(root) == JSON_NODE_ARRAY) {
		JsonArray* array = json_node_get_array(root);
		guint array_size = json_array_get_length (array);
		JsonNode *array_element;

		for (i = 0; i < array_size; i++) {
			array_element = json_array_get_element(array, i);
			clr_oci_json_parse_aux(array_element, node, true);
		}
	} else if (JSON_NODE_TYPE(root) == JSON_NODE_VALUE) {
		node = g_node_append(node, g_node_new(clr_oci_json_string(root)));

		if (parsing_array) {
			node = g_node_append(node, g_node_new(NULL));
		}
	}
}
JsonArray *trg_prefs_get_array(TrgPrefs * p, const gchar * key, int flags)
{
    JsonNode *node = trg_prefs_get_value(p, key, JSON_NODE_ARRAY, flags);

    if (node)
        return json_node_get_array(node);
    else
        return NULL;
}
/* This function is synchronous! Blocking once at startup seems pretty
 * reasonable and allows us to avoid any complexity re. races
 */
static void
mex_queue_model_load (MexQueueModel *model)
{
  JsonParser *parser;
  gchar *filename;
  GError *error = NULL;
  JsonNode *root;
  JsonArray *array;
  gint i = 0;

  filename = _queue_file_name ();

  if (!g_file_test (filename, G_FILE_TEST_EXISTS))
    {
      g_free (filename);

      return;
    }

  parser = json_parser_new ();
  if (!json_parser_load_from_file (parser, filename, &error))
    {
      g_warning (G_STRLOC ": error populating from file: %s",
                 error->message);
      g_clear_error (&error);
      goto out;
    }

  root = json_parser_get_root (parser);

  if (!JSON_NODE_HOLDS_ARRAY (root))
    {
      g_warning (G_STRLOC ": JSON data not of expected format!");

      goto out;
    }

  array = json_node_get_array (root);

  for (i = 0; i < json_array_get_length (array); i++)
    {
      MexContent *content;
      JsonNode *node;

      node = json_array_get_element (array, i);
      content = (MexContent *)json_gobject_deserialize (MEX_TYPE_PROGRAM,
                                                        node);

      mex_model_add_content (MEX_MODEL (model), content);
    }

out:
  g_free (filename);
  g_object_unref (parser);
}
Exemple #15
0
static void
skypeweb_got_friend_profiles(SkypeWebAccount *sa, JsonNode *node, gpointer user_data)
{
	JsonArray *contacts;
	PurpleBuddy *buddy;
	SkypeWebBuddy *sbuddy;
	gint index, length;
	
	if (node == NULL || json_node_get_node_type(node) != JSON_NODE_ARRAY)
		return;
	contacts = json_node_get_array(node);
	length = json_array_get_length(contacts);
	
	for(index = 0; index < length; index++)
	{
		JsonObject *contact = json_array_get_object_element(contacts, index);
		
		const gchar *username = json_object_get_string_member(contact, "username");
		const gchar *new_avatar;
		
		buddy = purple_find_buddy(sa->account, username);
		if (!buddy)
			continue;
		sbuddy = purple_buddy_get_protocol_data(buddy);
		if (sbuddy == NULL) {
			sbuddy = g_new0(SkypeWebBuddy, 1);
			purple_buddy_set_protocol_data(buddy, sbuddy);
			sbuddy->skypename = g_strdup(username);
			sbuddy->sa = sa;
		}
		
		g_free(sbuddy->display_name); sbuddy->display_name = g_strdup(json_object_get_string_member(contact, "displayname"));
		purple_serv_got_alias(sa->pc, username, sbuddy->display_name);
		if (json_object_has_member(contact, "lastname")) {
			gchar *fullname = g_strconcat(json_object_get_string_member(contact, "firstname"), " ", json_object_get_string_member(contact, "lastname"), NULL);
			
			purple_blist_server_alias_buddy(buddy, fullname);
			
			g_free(fullname);
		} else {
			purple_blist_server_alias_buddy(buddy, json_object_get_string_member(contact, "firstname"));
		}
		
		new_avatar = json_object_get_string_member(contact, "avatarUrl");
		if (new_avatar && *new_avatar && (!sbuddy->avatar_url || !g_str_equal(sbuddy->avatar_url, new_avatar))) {
			g_free(sbuddy->avatar_url);
			sbuddy->avatar_url = g_strdup(new_avatar);			
			skypeweb_get_icon(buddy);
		}
		
		g_free(sbuddy->mood); sbuddy->mood = g_strdup(json_object_get_string_member(contact, "mood"));
	}
}
void network_checking(void)
{
    int fd;
    
	http_down("/files.json", "http://lab.rt-thread.org/realboard/lpc4088/files.json");
    fd = open("/files.json", O_RDONLY, 0);
    if (fd >= 0)
    {
        int length;
        char *ptr;
        
        length = lseek(fd, 0, SEEK_END);
        if (length > 0)
        {
            lseek(fd, 0, SEEK_SET);
            
            ptr = (char*)rt_malloc(length);
            if (ptr != RT_NULL)
            {
                struct json_tree* tree;
                
                length = read(fd, ptr, length);
                tree = json_tree_parse(ptr, length);
                if (tree != RT_NULL)
                {
                    int index = 0;
                    struct json_node* node;

                    while (1)
                    {
                        const char *url;
                        const char *local_url;
                        
                        node = json_node_get_array(&(tree->root), index, RT_NULL);
                        if (node == RT_NULL) break;
                        index ++;
                        
                        url = json_node_get_string(node, "url", RT_NULL);
                        local_url = json_node_get_string(node, "local_url", RT_NULL);
                        
                        rt_kprintf("%s=>%s\n", local_url, url);
                    }
                    
                    json_tree_destroy(tree);
                }
                
                rt_free(ptr);
            }
        }
        
        close(fd);
    }
}
Exemple #17
0
void
cometd_inbox_push(cometd_inbox* inbox, JsonNode* payload)
{
  JsonArray* arr = json_node_get_array(payload);
  GList* msgs = json_array_get_elements(arr);

  GList* msg;
  for (msg = msgs; msg; msg = g_list_next(msg))
    cometd_inbox_push_msg(inbox, msg->data);

  g_list_free(msgs);
}
static void
move_local_favourites_cb(GtkInfoBar* bar,
                         gint res,
                         gpointer udata)
{
    GtFavouritesManager* self = GT_FAVOURITES_MANAGER(udata);
    gchar* fp = FAV_CHANNELS_FILE;
    gchar* new_fp = g_strconcat(fp, ".bak", NULL);

    if (res == GTK_RESPONSE_YES)
    {
        JsonParser* parse = json_parser_new();
        JsonNode* root;
        JsonArray* jarr;
        gchar* fp = FAV_CHANNELS_FILE;
        GError* err = NULL;

        gt_channel_free_list(self->favourite_channels);
        self->favourite_channels = NULL;
        g_signal_emit(self, sigs[SIG_FINISHED_LOADING_FAVOURITES], 0); //TODO: Add a LOADING_FAVOURITES signal

        json_parser_load_from_file(parse, fp, &err);

        if (err)
        {
            g_warning("{GtFavouritesManager} Error move local favourite channels to twitch '%s'", err->message);
            return;
        }

        root = json_parser_get_root(parse);
        jarr = json_node_get_array(root);

        for (GList* l = json_array_get_elements(jarr); l != NULL; l = l->next)
        {
            GtChannel* chan = GT_CHANNEL(json_gobject_deserialize(GT_TYPE_CHANNEL, l->data));
            //TODO: Error handling
            gt_twitch_follow_channel_async(main_app->twitch, gt_channel_get_name(chan), NULL, NULL);
            g_object_unref(chan);
        }

        g_object_unref(parse);
        g_free(fp);

        gt_favourites_manager_load_from_twitch(self);
    }

    g_rename(fp, new_fp);

    g_free(fp);
    g_free(new_fp);
}
Exemple #19
0
void
cockpit_package_dump (void)
{
  GHashTable *listing;
  GHashTable *by_name;
  GHashTableIter iter;
  CockpitPackage *package;
  GList *names, *l;
  const gchar *prefix;
  JsonArray *array;
  guint i;

  listing = cockpit_package_listing (NULL);
  by_name = g_hash_table_new (g_str_hash, g_str_equal);

  g_hash_table_iter_init (&iter, listing);
  while (g_hash_table_iter_next (&iter, NULL, (gpointer *)&package))
    g_hash_table_replace (by_name, package->name, package);

  names = g_hash_table_get_keys (by_name);
  names = g_list_sort (names, (GCompareFunc)strcmp);
  for (l = names; l != NULL; l = g_list_next (l))
    {
      package = g_hash_table_lookup (by_name, l->data);
      g_print ("%s: %s\n", package->name, package->directory);
      if (package->checksum)
        g_print ("    checksum: %s\n", package->checksum);

      if (package->alias)
        {
          prefix = "    alias: ";
          if (JSON_NODE_HOLDS_ARRAY (package->alias))
            {
              array = json_node_get_array (package->alias);
              for (i = 0; i < json_array_get_length (array); i++)
                {
                  g_print ("%s%s\n", prefix, json_array_get_string_element (array, i));
                  prefix = "           ";
                }
            }
          else
            {
              g_print ("%s%s\n", prefix, json_node_get_string (package->alias));
            }
        }
    }

  g_list_free (names);
  g_hash_table_unref (by_name);
  g_hash_table_unref (listing);
}
Exemple #20
0
static void
reedah_item_callback (JsonNode *node, itemPtr item)
{
	JsonNode	*canonical, *categories;
	GList		*elements, *iter;

	/* Determine link: path is "canonical[0]/@href" */
	canonical = json_get_node (node, "canonical");
	if (canonical && JSON_NODE_TYPE (canonical) == JSON_NODE_ARRAY) {
		iter = elements = json_array_get_elements (json_node_get_array (canonical));
		while (iter) {
			const gchar *href = json_get_string ((JsonNode *)iter->data, "href");
			if (href) {
				item_set_source (item, href);
				break;
			}
			iter = g_list_next (iter);
		}

		g_list_free (elements);
	}

	/* Determine read state: check for category with ".*state/com.google/read" */
	categories = json_get_node (node, "categories");
	if (categories && JSON_NODE_TYPE (categories) == JSON_NODE_ARRAY) {
		iter = elements = json_array_get_elements (json_node_get_array (canonical));
		while (iter) {
			const gchar *category = json_node_get_string ((JsonNode *)iter->data);
			if (category) {
				item->readStatus = (strstr (category, "state\\/com.google\\/read") != NULL);
				break;
			}
			iter = g_list_next (iter);
		}

		g_list_free (elements);	
	}
}
Exemple #21
0
static void
setup_parser()
{
    JsonParser* parse;
    JsonNode* root;

    anitomy = anitomy_new();

    parse = json_parser_new();
    json_parser_load_from_data(parse, JSON_SAMPLE_DATA, -1, NULL);

    root = json_parser_get_root(parse);
    sample_array = json_node_get_array(root);
}
Exemple #22
0
/**
 * json_object_get_array_member:
 * @object: a #JsonObject
 * @member_name: the name of the member
 *
 * Convenience function that retrieves the array
 * stored in @member_name of @object
 *
 * See also: json_object_get_member()
 *
 * Return value: the array inside the object's member
 *
 * Since: 0.8
 */
JsonArray *
json_object_get_array_member (JsonObject  *object,
                              const gchar *member_name)
{
  JsonNode *node;

  g_return_val_if_fail (object != NULL, NULL);
  g_return_val_if_fail (member_name != NULL, NULL);

  node = object_get_member_internal (object, member_name);
  g_return_val_if_fail (node != NULL, NULL);
  g_return_val_if_fail (JSON_NODE_TYPE (node) == JSON_NODE_ARRAY, NULL);

  return json_node_get_array (node);
}
Exemple #23
0
int
cometd_impl_process_sync(const cometd* h, JsonNode* root)
{
  JsonArray* arr = json_node_get_array(root);
  GList* msgs = json_array_get_elements(arr);

  GList* item;
  for (item = msgs; item; item = g_list_next(item))
    cometd_process_msg(h, item->data);

  g_list_free(msgs);

  // TODO: What happens if cometd_fire_listeners blows up?
  return COMETD_SUCCESS;
}
Exemple #24
0
static void
snra_json_node_into_val (JsonNode *element_node, GValue *v)
{
  if (JSON_NODE_HOLDS_OBJECT (element_node)) {
    GstStructure *child = snra_json_to_gst_structure (element_node);
    g_value_init (v, GST_TYPE_STRUCTURE);
    gst_value_set_structure (v, child);
  } else if (JSON_NODE_HOLDS_ARRAY (element_node)) {
    JsonArray *arr = json_node_get_array (element_node);
    g_value_init (v, GST_TYPE_ARRAY);
    json_array_foreach_element (arr,
        (JsonArrayForeach) snra_json_array_add_to_val, v);
  } else {
    json_node_get_value (element_node, v);
  }
}
/**
 * @brief get_spotify_artist_albums Get the number of artist albums on Spotify
 * @param spotify_uri Spotify uri for the artist
 * @return Number of number of artist albums on Spotify
 */
guint get_spotify_artist_albums(const gchar *spotify_uri){
  GString *url = g_string_new(ARTIST_LOOKUP_URI);
  url = g_string_insert(url, 41, spotify_uri);

  GError *error = NULL;
  JsonParser *parser = json_parser_new(); 
  GFile * file = g_file_new_for_uri(url->str);

  g_print("Opening %s for reading\n", url->str);
  GInputStream * fis = (GInputStream*)g_file_read (file, NULL, &error);
  g_print("Opened!\n");

  if (error){
    g_debug("** ERROR **: %s (domain: %s, code: %d) at %d (in get_spotify_artist_albums)\n", \
	     error->message, g_quark_to_string (error->domain), error->code, \
	     __LINE__);
    g_object_unref(file);
    g_object_unref(fis);
    g_object_unref (parser);
    g_string_free(url, TRUE);
    return 0;
  }

  json_parser_load_from_stream(parser, fis, NULL, &error);
  if (error){
    g_debug("Unable to parse `%s': %s\n", url->str, error->message);
    g_object_unref(file);
    g_object_unref(fis);
    g_object_unref (parser);
    g_string_free(url, TRUE);
    return 0;
  }

  JsonNode *root = json_parser_get_root(parser);
  JsonObject * content = json_node_get_object(root);
  JsonNode * node = json_object_get_member(content, "artist");
  content = json_node_get_object(node);
  node = json_object_get_member(content, "albums");
  JsonArray * AlbumsArray = json_node_get_array(node);
  guint AlbumsArrayLength = json_array_get_length(AlbumsArray);

  g_object_unref(file);
  g_object_unref(fis);
  g_object_unref (parser);
  g_string_free(url, TRUE);
  return AlbumsArrayLength;
}
static void
process_results (RBPodcastSearchMiroGuide *search, JsonParser *parser)
{
	JsonArray *results;
	guint i;

	results = json_node_get_array (json_parser_get_root (parser));

	for (i = 0; i < json_array_get_length (results); i++) {
		JsonObject *feed;
		JsonArray *items;
		RBPodcastChannel *channel;
		int j;

		feed = json_array_get_object_element (results, i);

		channel = g_new0 (RBPodcastChannel, 1);
		channel->url = g_strdup (json_object_get_string_member (feed, "url"));
		channel->title = g_strdup (json_object_get_string_member (feed, "name"));
		channel->author = g_strdup (json_object_get_string_member (feed, "publisher"));		/* hrm */
		channel->img = g_strdup (json_object_get_string_member (feed, "thumbnail_url"));
		channel->is_opml = FALSE;
		rb_debug ("feed %d: url %s, name \"%s\"", i, channel->url, channel->title);

		items = json_object_get_array_member (feed, "item");
		for (j = 0; j < json_array_get_length (items); j++) {
			JsonObject *episode = json_array_get_object_element (items, j);
			RBPodcastItem *item;

			item = g_new0 (RBPodcastItem, 1);
			item->title = g_strdup (json_object_get_string_member (episode, "name"));
			item->url = g_strdup (json_object_get_string_member (episode, "url"));
			item->description = g_strdup (json_object_get_string_member (episode, "description"));
			item->pub_date = totem_pl_parser_parse_date (json_object_get_string_member (episode, "date"), FALSE);
			item->filesize = json_object_get_int_member (episode, "size");
			rb_debug ("item %d: title \"%s\", url %s", j, item->title, item->url);

			channel->posts = g_list_prepend (channel->posts, item);
		}
		channel->posts = g_list_reverse (channel->posts);
		rb_debug ("finished parsing items");

		rb_podcast_search_result (RB_PODCAST_SEARCH (search), channel);
		rb_podcast_parse_channel_free (channel);
	}
}
/**
 * json_reader_count_elements:
 * @reader: a #JsonReader
 *
 * Counts the elements of the current position, if @reader is
 * positioned on an array
 *
 * Return value: the number of elements, or -1. In case of failure
 *   the #JsonReader is set in an error state
 *
 * Since: 0.12
 */
gint
json_reader_count_elements (JsonReader *reader)
{
  JsonReaderPrivate *priv;

  g_return_val_if_fail (JSON_IS_READER (reader), -1);

  priv = reader->priv;

  if (priv->current_node == NULL)
    return -1;

  if (!JSON_NODE_HOLDS_ARRAY (priv->current_node))
    return -1;

  return json_array_get_length (json_node_get_array (priv->current_node));
}
Exemple #28
0
int rm_json_cache_read(RmTrie *file_trie, const char *json_path) {
#if !HAVE_JSON_GLIB
    (void)file_trie;
    (void)json_path;

    rm_log_info_line(_("caching is not supported due to missing json-glib library."));
    return EXIT_FAILURE;
#else
    rm_assert_gentle(file_trie);
    rm_assert_gentle(json_path);

    int result = EXIT_FAILURE;
    GError *error = NULL;
    size_t keys_in_table = rm_trie_size(file_trie);
    JsonParser *parser = json_parser_new();

    rm_log_info_line(_("Loading json-cache `%s'"), json_path);

    if(!json_parser_load_from_file(parser, json_path, &error)) {
        rm_log_warning_line(_("FAILED: %s\n"), error->message);
        g_error_free(error);
        goto failure;
    }

    JsonNode *root = json_parser_get_root(parser);
    if(JSON_NODE_TYPE(root) != JSON_NODE_ARRAY) {
        rm_log_warning_line(_("No valid json cache (no array in /)"));
        goto failure;
    }

    /* Iterate over all objects in it */
    json_array_foreach_element(json_node_get_array(root),
                               (JsonArrayForeach)rm_json_cache_parse_entry,
                               file_trie);

    /* check if some entries were added */
    result = (keys_in_table >= rm_trie_size(file_trie));

failure:
    if(parser) {
        g_object_unref(parser);
    }
    return result;
#endif
}
Exemple #29
0
/**
 * json_object_get_array_member:
 * @object: a #JsonObject
 * @member_name: the name of the member
 *
 * Convenience function that retrieves the array
 * stored in @member_name of @object
 *
 * See also: json_object_get_member()
 *
 * Return value: (transfer none): the array inside the object's member
 *
 * Since: 0.8
 */
JsonArray *
json_object_get_array_member (JsonObject  *object,
                              const gchar *member_name)
{
  JsonNode *node;

  g_return_val_if_fail (object != NULL, NULL);
  g_return_val_if_fail (member_name != NULL, NULL);

  node = object_get_member_internal (object, member_name);
  g_return_val_if_fail (node != NULL, NULL);
  g_return_val_if_fail (JSON_NODE_HOLDS_ARRAY (node) || JSON_NODE_HOLDS_NULL (node), NULL);

  if (JSON_NODE_HOLDS_NULL (node))
    return NULL;

  return json_node_get_array (node);
}
/**
 * json_array_get_array_element:
 * @array: a #JsonArray
 * @index_: the index of the element to retrieve
 *
 * Conveniently retrieves the array from the element at @index_
 * inside @array
 *
 * See also: json_array_get_element(), json_node_get_array()
 *
 * Return value: (transfer none): the array
 *
 * Since: 0.8
 */
JsonArray *
json_array_get_array_element (JsonArray *array,
                              guint      index_)
{
  JsonNode *node;

  g_return_val_if_fail (array != NULL, NULL);
  g_return_val_if_fail (index_ < array->elements->len, NULL);

  node = g_ptr_array_index (array->elements, index_);
  g_return_val_if_fail (node != NULL, NULL);
  g_return_val_if_fail (JSON_NODE_HOLDS_ARRAY (node) || JSON_NODE_HOLDS_NULL (node), NULL);

  if (JSON_NODE_HOLDS_NULL (node))
    return NULL;

  return json_node_get_array (node);
}