GList* searpc_client_fret__objlist (GType gtype, char *data, size_t len, GError **error) { json_t *object = NULL; GList *ret = NULL; if (handle_ret_common(data, len, &object, error) == 0) { const json_t *array = json_object_get (object, "ret"); if (json_is_null(array)) { json_decref(object); return NULL; } g_assert (array); int i; for (i = 0; i < json_array_size(array); i++) { json_t *member = json_array_get (array, i); GObject *obj = json_gobject_deserialize(gtype, member); if (obj == NULL) { g_set_error (error, DFT_DOMAIN, 503, "Invalid data: object list contains null"); clean_objlist(ret); json_decref(object); return NULL; } ret = g_list_prepend (ret, obj); } json_decref(object); return g_list_reverse(ret); } return NULL; }
/** * gfbgraph_connectable_default_parse_connected_data: * @self: a #GFBGraphConnectable. * @payload: a const #gchar with the response string from the Facebook Graph API. * @error: (allow-none): a #GError or %NULL. * * In most cases, #GFBGraphConnectable implementers can use this function in order to parse * the response when a gfbgraph_node_get_connection_nodes() is executed and the * gfbgraph_connectable_parse_connected_data() was called. * * Normally, Facebook Graph API returns the connections in the same way, using JSON objects, * with a root object called "data". * * Returns: (element-type GFBGraphNode) (transfer full): a newly-allocated #GList of #GFBGraphNode with the same #GType as @self. **/ GList* gfbgraph_connectable_default_parse_connected_data (GFBGraphConnectable *self, const gchar *payload, GError **error) { GList *nodes_list = NULL; JsonParser *jparser; GType node_type; node_type = G_OBJECT_TYPE (self); jparser = json_parser_new (); if (json_parser_load_from_data (jparser, payload, -1, error)) { JsonNode *root_jnode; JsonObject *main_jobject; JsonArray *nodes_jarray; int i = 0; root_jnode = json_parser_get_root (jparser); main_jobject = json_node_get_object (root_jnode); nodes_jarray = json_object_get_array_member (main_jobject, "data"); for (i = 0; i < json_array_get_length (nodes_jarray); i++) { JsonNode *jnode; GFBGraphNode *node; jnode = json_array_get_element (nodes_jarray, i); node = GFBGRAPH_NODE (json_gobject_deserialize (node_type, jnode)); nodes_list = g_list_append (nodes_list, node); } } g_clear_object (&jparser); return nodes_list; }
static void picasa_web_service_get_user_info_ready_cb (SoupSession *session, SoupMessage *msg, gpointer user_data) { PicasaWebService *self = user_data; GSimpleAsyncResult *result; GError *error = NULL; JsonNode *node; result = _web_service_get_result (WEB_SERVICE (self)); if (picasa_web_utils_parse_json_response (msg, &node, &error)) { OAuthAccount *account; account = (OAuthAccount *) json_gobject_deserialize (OAUTH_TYPE_ACCOUNT, node); g_object_set (account, "token", self->priv->access_token, "token-secret", self->priv->refresh_token, NULL); g_simple_async_result_set_op_res_gpointer (result, g_object_ref (account), (GDestroyNotify) g_object_unref); _g_object_unref (account); json_node_free (node); } else g_simple_async_result_set_from_error (result, error); g_simple_async_result_complete_in_idle (result); }
BuilderSource * builder_source_from_json (JsonNode *node) { JsonObject *object = json_node_get_object (node); const gchar *type; type = json_object_get_string_member (object, "type"); if (type == NULL) g_warning ("Missing source type"); else if (strcmp (type, "archive") == 0) return (BuilderSource *) json_gobject_deserialize (BUILDER_TYPE_SOURCE_ARCHIVE, node); else if (strcmp (type, "file") == 0) return (BuilderSource *) json_gobject_deserialize (BUILDER_TYPE_SOURCE_FILE, node); else if (strcmp (type, "script") == 0) return (BuilderSource *) json_gobject_deserialize (BUILDER_TYPE_SOURCE_SCRIPT, node); else if (strcmp (type, "shell") == 0) return (BuilderSource *) json_gobject_deserialize (BUILDER_TYPE_SOURCE_SHELL, node); else if (strcmp (type, "patch") == 0) return (BuilderSource *) json_gobject_deserialize (BUILDER_TYPE_SOURCE_PATCH, node); else if (strcmp (type, "git") == 0) return (BuilderSource *) json_gobject_deserialize (BUILDER_TYPE_SOURCE_GIT, node); else if (strcmp (type, "bzr") == 0) return (BuilderSource *) json_gobject_deserialize (BUILDER_TYPE_SOURCE_BZR, node); else g_warning ("Unknown source type %s", type); 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); }
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); }
/** * json_gobject_from_data: * @gtype: the #GType of object to construct * @data: a JSON data stream * @length: length of the data stream, or -1 if it is NUL-terminated * @error: return location for a #GError, or %NULL * * Deserializes a JSON data stream and creates the corresponding * #GObject class. If @gtype implements the #JsonSerializableIface * interface, it will be asked to deserialize all the JSON members * into the respective properties; otherwise, the default implementation * will be used to translate the compatible JSON native types. * * Note: the JSON data stream must be an object declaration. * * Return value: (transfer full): a #GObject or %NULL * * Since: 0.10 */ GObject * json_gobject_from_data (GType gtype, const gchar *data, gssize length, GError **error) { JsonParser *parser; JsonNode *root; GError *parse_error; GObject *retval; g_return_val_if_fail (gtype != G_TYPE_INVALID, NULL); g_return_val_if_fail (data != NULL, NULL); if (length < 0) length = strlen (data); parser = json_parser_new (); parse_error = NULL; json_parser_load_from_data (parser, data, length, &parse_error); if (parse_error) { g_propagate_error (error, parse_error); g_object_unref (parser); return NULL; } root = json_parser_get_root (parser); if (root == NULL || JSON_NODE_TYPE (root) != JSON_NODE_OBJECT) { /* translators: the %s is the name of the data structure */ g_set_error (error, JSON_PARSER_ERROR, JSON_PARSER_ERROR_PARSE, _("Expecting a JSON object, but the root node is of type `%s'"), json_node_type_name (root)); g_object_unref (parser); return NULL; } retval = json_gobject_deserialize (gtype, root); g_object_unref (parser); return retval; }
void gt_favourites_manager_load_from_file(GtFavouritesManager* self) { GtFavouritesManagerPrivate* priv = gt_favourites_manager_get_instance_private(self); JsonParser* parse = json_parser_new(); JsonNode* root; JsonArray* jarr; gchar* fp = FAV_CHANNELS_FILE; GError* err = NULL; if (!g_file_test(fp, G_FILE_TEST_EXISTS)) goto finish; json_parser_load_from_file(parse, fp, &err); if (err) { g_warning("{GtFavouritesManager} Error loading favourite channels '%s'", err->message); goto finish; } 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)); self->favourite_channels = g_list_append(self->favourite_channels, chan); g_signal_handlers_block_by_func(chan, channel_favourited_cb, self); g_object_set(chan, "auto-update", TRUE, "favourited", TRUE, NULL); gt_channel_update(chan); g_signal_handlers_unblock_by_func(chan, channel_favourited_cb, self); g_signal_connect(chan, "notify::updating", G_CALLBACK(oneshot_updating_cb), self); } finish: g_object_unref(parse); g_free(fp); }
static void gb_project_format_real_open_cb (GObject *object, GAsyncResult *result, gpointer user_data) { GSimpleAsyncResult *simple = user_data; const gchar *directory; JsonParser *parser = (JsonParser *)object; JsonNode *root; GObject *project; GError *error = NULL; ENTRY; g_assert(JSON_IS_PARSER(parser)); g_assert(G_IS_SIMPLE_ASYNC_RESULT(simple)); if (!json_parser_load_from_stream_finish(parser, result, &error)) { g_simple_async_result_take_error(simple, error); GOTO(failure); } root = json_parser_get_root(parser); g_assert(root); if (!(project = json_gobject_deserialize(GB_TYPE_PROJECT, root))) { g_simple_async_result_set_error(simple, GB_PROJECT_FORMAT_ERROR, GB_PROJECT_FORMAT_ERROR_INVALID_JSON, _("Failed to deserialize from JSON.")); GOTO(failure); } directory = g_object_get_data(G_OBJECT(simple), "directory"); g_object_set(project, "directory", directory, NULL); g_simple_async_result_set_op_res_gpointer(simple, project, g_object_unref); failure: g_simple_async_result_complete_in_idle(simple); g_object_unref(simple); EXIT; }
GObject* searpc_client_fret__object (GType gtype, char *data, size_t len, GError **error) { json_t *object = NULL; GObject *ret = NULL; json_t *member; if (handle_ret_common(data, len, &object, error) == 0) { member = json_object_get (object, "ret"); if (json_is_null(member)) { json_decref(object); return NULL; } ret = json_gobject_deserialize(gtype, member); json_decref(object); return ret; } return NULL; }
static gboolean builder_options_deserialize_property (JsonSerializable *serializable, const gchar *property_name, GValue *value, GParamSpec *pspec, JsonNode *property_node) { if (strcmp (property_name, "arch") == 0) { if (JSON_NODE_TYPE (property_node) == JSON_NODE_NULL) { g_value_set_boxed (value, NULL); return TRUE; } else if (JSON_NODE_TYPE (property_node) == JSON_NODE_OBJECT) { JsonObject *object = json_node_get_object (property_node); g_autoptr(GHashTable) hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_object_unref); g_autoptr(GList) members = NULL; GList *l; members = json_object_get_members (object); for (l = members; l != NULL; l = l->next) { const char *member_name = l->data; JsonNode *val; GObject *option; val = json_object_get_member (object, member_name); option = json_gobject_deserialize (BUILDER_TYPE_OPTIONS, val); if (option == NULL) return FALSE; g_hash_table_insert (hash, g_strdup (member_name), option); } g_value_set_boxed (value, hash); return TRUE; } return FALSE; } else if (strcmp (property_name, "env") == 0) { if (JSON_NODE_TYPE (property_node) == JSON_NODE_NULL) { g_value_set_boxed (value, NULL); return TRUE; } else if (JSON_NODE_TYPE (property_node) == JSON_NODE_OBJECT) { JsonObject *object = json_node_get_object (property_node); g_autoptr(GPtrArray) env = g_ptr_array_new_with_free_func (g_free); g_autoptr(GList) members = NULL; GList *l; members = json_object_get_members (object); for (l = members; l != NULL; l = l->next) { const char *member_name = l->data; JsonNode *val; const char *val_str; val = json_object_get_member (object, member_name); val_str = json_node_get_string (val); if (val_str == NULL) return FALSE; g_ptr_array_add (env, g_strdup_printf ("%s=%s", member_name, val_str)); } g_ptr_array_add (env, NULL); g_value_set_boxed (value, g_ptr_array_free (g_steal_pointer (&env), FALSE)); return TRUE; } return FALSE; } else return json_serializable_default_deserialize_property (serializable, property_name, value, pspec, property_node); }
static gboolean json_deserialize_pspec (GValue *value, GParamSpec *pspec, json_t *node) { switch (json_typeof(node)) { case JSON_OBJECT: if (g_type_is_a (G_VALUE_TYPE (value), G_TYPE_OBJECT)) { GObject *object; object = json_gobject_deserialize (G_VALUE_TYPE (value), node); if (object) g_value_take_object (value, object); else g_value_set_object (value, NULL); return TRUE; } break; case JSON_STRING: if (G_TYPE_FUNDAMENTAL (G_VALUE_TYPE (value)) == G_TYPE_STRING) { g_value_set_string(value, json_string_value(node)); return TRUE; } break; case JSON_INTEGER: { json_int_t int_value = json_integer_value (node); switch (G_TYPE_FUNDAMENTAL (G_VALUE_TYPE (value))) { case G_TYPE_CHAR: g_value_set_schar(value, (gchar)int_value); return TRUE; case G_TYPE_UCHAR: g_value_set_uchar (value, (guchar)int_value); return TRUE; case G_TYPE_INT: g_value_set_int (value, (gint)int_value); return TRUE; case G_TYPE_UINT: g_value_set_uint(value, (guint)int_value); return TRUE; case G_TYPE_LONG: g_value_set_long(value, (glong)int_value); return TRUE; case G_TYPE_ULONG: g_value_set_ulong(value, (gulong)int_value); return TRUE; case G_TYPE_INT64: g_value_set_int64(value,(gint64)int_value); return TRUE; case G_TYPE_ENUM: g_value_set_enum(value,(gint64)int_value); return TRUE; case G_TYPE_FLAGS: g_value_set_flags(value,(gint64)int_value); return TRUE; } } break; case JSON_REAL: { double real_value = json_real_value(node); switch (G_TYPE_FUNDAMENTAL (G_VALUE_TYPE (value))) { case G_TYPE_FLOAT: g_value_set_float(value,(gfloat)real_value); return TRUE; case G_TYPE_DOUBLE: g_value_set_double(value,(gdouble)real_value); return TRUE; } } break; case JSON_TRUE: case JSON_FALSE: if (G_TYPE_FUNDAMENTAL (G_VALUE_TYPE (value)) == G_TYPE_BOOLEAN) { g_value_set_boolean(value,(gboolean)json_is_true(node)); return TRUE; } break; case JSON_NULL: if (G_TYPE_FUNDAMENTAL (G_VALUE_TYPE (value)) == G_TYPE_STRING) { g_value_set_string (value, NULL); return TRUE; } else if (G_TYPE_FUNDAMENTAL (G_VALUE_TYPE (value)) == G_TYPE_OBJECT) { g_value_set_object (value, NULL); return TRUE; } break; case JSON_ARRAY: return FALSE; break; default: return FALSE; } return FALSE; }