void _clutter_paint_node_dump_tree (ClutterPaintNode *node) { #ifdef CLUTTER_ENABLE_DEBUG JsonGenerator *gen = json_generator_new (); char *str; gsize len; json_generator_set_root (gen, clutter_paint_node_to_json (node)); str = json_generator_to_data (gen, &len); g_print ("Render tree starting from %p:\n%s\n", node, str); g_free (str); #endif /* CLUTTER_ENABLE_DEBUG */ }
static void postal_http_reply_devices (PostalHttp *http, SoupMessage *message, guint status, GPtrArray *devices) { JsonGenerator *g; PostalDevice *device; JsonArray *ar; JsonNode *node; JsonNode *child; gchar *json_buf; gsize length; guint i; g_assert(SOUP_IS_MESSAGE(message)); g_assert(devices); ar = json_array_new(); node = json_node_new(JSON_NODE_ARRAY); for (i = 0; i < devices->len; i++) { device = g_ptr_array_index(devices, i); if ((child = postal_device_save_to_json(device, NULL))) { json_array_add_element(ar, child); } } json_node_set_array(node, ar); json_array_unref(ar); g = json_generator_new(); json_generator_set_root(g, node); json_node_free(node); json_generator_set_indent(g, 2); json_generator_set_pretty(g, TRUE); json_buf = json_generator_to_data(g, &length); g_object_unref(g); soup_message_set_response(message, "application/json", SOUP_MEMORY_TAKE, json_buf, length); soup_message_set_status(message, status ?: SOUP_STATUS_OK); soup_server_unpause_message(http->priv->server, message); }
gchar * json_ghashtable_serialize_data (GHashTable *table, gsize *length) { JsonGenerator *generator; JsonNode *root; gchar *serial; generator = json_generator_new (); root = json_ghashtable_serialize (table); json_generator_set_root (generator, root); serial = json_generator_to_data (generator, length); json_node_free (root); g_object_unref (generator); return serial; }
static void postal_http_reply_device (PostalHttp *http, SoupMessage *message, guint status, PostalDevice *device) { PostalHttpPrivate *priv; JsonGenerator *g; JsonNode *node; GError *error = NULL; gchar *json_buf; gsize length; ENTRY; g_assert(SOUP_IS_MESSAGE(message)); g_assert(POSTAL_IS_DEVICE(device)); g_assert(POSTAL_IS_HTTP(http)); priv = http->priv; if (!(node = postal_device_save_to_json(device, &error))) { postal_http_reply_error(http, message, error); soup_server_unpause_message(priv->server, message); g_error_free(error); EXIT; } g = json_generator_new(); json_generator_set_indent(g, 2); json_generator_set_pretty(g, TRUE); json_generator_set_root(g, node); json_node_free(node); if ((json_buf = json_generator_to_data(g, &length))) { soup_message_set_response(message, "application/json", SOUP_MEMORY_TAKE, json_buf, length); } soup_message_set_status(message, status); soup_server_unpause_message(priv->server, message); g_object_unref(g); EXIT; }
static gchar* do_get_action(IpcamIAjax *iajax, GList *item_list) { JsonBuilder *builder; JsonNode *res_node = NULL; GList *item; JsonGenerator *generator; builder = json_builder_new(); generator = json_generator_new(); json_builder_begin_object(builder); json_builder_set_member_name(builder, "items"); json_builder_begin_object(builder); for (item = g_list_first(item_list); item; item = g_list_next(item)) { const gchar *name = item->data; GVariant *value = NULL; gchar *key = NULL; asprintf(&key, "day_night_mode:%s", name); value = ipcam_iajax_get_configuration(iajax, key); if (value) { add_value(builder, name, value); g_variant_unref(value); } g_free(key); } json_builder_end_object(builder); json_builder_end_object(builder); res_node = json_builder_get_root(builder); json_generator_set_root(generator, res_node); json_generator_set_pretty(generator, TRUE); gchar *result = json_generator_to_data(generator, NULL); json_node_free(res_node); g_object_unref(G_OBJECT(builder)); g_object_unref(G_OBJECT(generator)); return result; }
static gboolean jsonify_variant (GVariant *variant, NPVariant *result) { gboolean ret; GVariant *real_value; JsonNode *root; JsonGenerator *generator; gsize json_length; gchar *json; gchar *buffer; ret = TRUE; /* DBus methods can return multiple values, * but we're only interested in the first. */ g_variant_get (variant, "(@*)", &real_value); root = json_gvariant_serialize (real_value); generator = json_generator_new (); json_generator_set_root (generator, root); json = json_generator_to_data (generator, &json_length); buffer = funcs.memalloc (json_length + 1); if (!buffer) { ret = FALSE; goto out; } strcpy (buffer, json); STRINGN_TO_NPVARIANT (buffer, json_length, *result); out: g_variant_unref (variant); g_variant_unref (real_value); json_node_free (root); g_free (json); return ret; }
static void manager_send_msg_to_client (SnraManager * manager, SnraServerClient * client, gint send_to_mask, GstStructure * msg) { JsonGenerator *gen; JsonNode *root; gchar *body; gsize len; root = snra_json_from_gst_structure (msg); gst_structure_free (msg); gen = json_generator_new (); json_generator_set_root (gen, root); body = json_generator_to_data (gen, &len); g_object_unref (gen); json_node_free (root); if (client) { snra_server_client_send_message (client, body, len); } else { /* client == NULL - send to all clients */ GList *cur; if (send_to_mask & SEND_MSG_TO_PLAYERS) { for (cur = manager->player_info; cur != NULL; cur = g_list_next (cur)) { SnraPlayerInfo *info = (SnraPlayerInfo *) (cur->data); if (info->conn) snra_server_client_send_message (info->conn, body, len); } } if (send_to_mask & SEND_MSG_TO_CONTROLLERS) { for (cur = manager->ctrl_clients; cur != NULL; cur = g_list_next (cur)) { client = (SnraServerClient *) (cur->data); snra_server_client_send_message (client, body, len); } } } g_free (body); }
static void debug_print_json_node (char * msg, JsonNode * node) { g_assert (node != NULL); gchar * buffer; if (json_node_get_node_type (node) == JSON_NODE_VALUE) { buffer = g_strdup ( json_node_get_string (node) ); /* we should check number, boolean too */ } else { JsonGenerator *gen = json_generator_new(); json_generator_set_root (gen, node); buffer = json_generator_to_data (gen,NULL); g_object_unref (gen); } g_message("%s - Json Node of type %d: %s\n",msg, (gint)json_node_get_node_type (node), buffer); g_free (buffer); }
void gt_favourites_manager_save(GtFavouritesManager* self) { GtFavouritesManagerPrivate* priv = gt_favourites_manager_get_instance_private(self); if (g_list_length(self->favourite_channels) == 0) return; JsonArray* jarr = json_array_new(); JsonGenerator* gen = json_generator_new(); JsonNode* final = json_node_new(JSON_NODE_ARRAY); gchar* fp = FAV_CHANNELS_FILE; for (GList* l = self->favourite_channels; l != NULL; l = l->next) { JsonNode* node = json_gobject_serialize(l->data); json_array_add_element(jarr, node); } final = json_node_init_array(final, jarr);
gchar * skypeweb_jsonobj_to_string(JsonObject *jsonobj) { JsonGenerator *generator; JsonNode *root; gchar *string; root = json_node_new(JSON_NODE_OBJECT); json_node_set_object(root, jsonobj); generator = json_generator_new(); json_generator_set_root(generator, root); string = json_generator_to_data(generator, NULL); g_object_unref(generator); json_node_free(root); return string; }
static gboolean output_json(MuMsg *msg, MuMsgIter *iter, MuConfig *opts, GError **err) { JsonBuilder *builder; JsonNode *root; JsonGenerator *gen; gchar *json_str; builder = get_message_json (msg); gen = json_generator_new (); root = json_builder_get_root (builder); json_generator_set_root (gen, root); json_str = json_generator_to_data (gen, NULL); g_print ("%s, ", json_str); g_free(json_str); json_node_free (root); g_object_unref (gen); g_object_unref (builder); return TRUE; }
/* Helpers */ static gchar * melo_jsonrpc_node_to_string (JsonNode *node) { JsonGenerator *gen; gchar *str; /* Create a new generator */ gen = json_generator_new (); if (!gen) return NULL; /* Set root node */ json_generator_set_root (gen, node); /* Generate string */ str = json_generator_to_data (gen, NULL); /* Free generator */ g_object_unref (gen); return str; }
static void postal_http_reply_error (PostalHttp *http, SoupMessage *message, const GError *error) { JsonGenerator *g; JsonObject *obj; JsonNode *node; gchar *json_buf; gsize length; g_assert(SOUP_IS_MESSAGE(message)); g_assert(error); obj = json_object_new(); json_object_set_string_member(obj, "message", error->message); json_object_set_string_member(obj, "domain", g_quark_to_string(error->domain)); json_object_set_int_member(obj, "code", error->code); node = json_node_new(JSON_NODE_OBJECT); json_node_set_object(node, obj); json_object_unref(obj); g = json_generator_new(); json_generator_set_indent(g, 2); json_generator_set_pretty(g, TRUE); json_generator_set_root(g, node); json_node_free(node); json_buf = json_generator_to_data(g, &length); g_object_unref(g); soup_message_set_response(message, "application/json", SOUP_MEMORY_TAKE, json_buf, length); soup_message_set_status(message, get_status_code(error)); soup_server_unpause_message(http->priv->server, message); }
static gchar * build_save_items_request (GList *file_list) { GList *l; JsonBuilder *json_builder = json_builder_new (); json_builder_begin_object (json_builder); json_builder_set_member_name (json_builder, "items"); json_builder_begin_array (json_builder); for (l = file_list; l; l = l->next) { gchar *path = l->data; json_builder_begin_object (json_builder); json_builder_set_member_name (json_builder, "itemType"); json_builder_add_string_value (json_builder, "attachment"); json_builder_set_member_name (json_builder, "path"); json_builder_add_string_value (json_builder, path); json_builder_set_member_name (json_builder, "attachments"); json_builder_begin_array (json_builder); /* empty (but required for each item) */ json_builder_end_array (json_builder); json_builder_end_object (json_builder); } json_builder_end_array (json_builder); json_builder_set_member_name (json_builder, "attachmentMode"); json_builder_add_string_value (json_builder, "file"); json_builder_end_object (json_builder); JsonGenerator *json_gen = json_generator_new (); JsonNode *root = json_builder_get_root (json_builder); json_generator_set_root (json_gen, root); gchar *save_items_request = json_generator_to_data (json_gen, NULL); json_node_free (root); g_object_unref (json_gen); g_object_unref (json_builder); return save_items_request; }
gchar * error_to_json (int code, const char *msg, gsize *len) { JsonObject *object = json_object_new (); JsonNode *root = json_node_new (JSON_NODE_OBJECT); JsonGenerator *generator = json_generator_new (); gchar *data; json_object_set_int_member (object, "err_code", code); json_object_set_string_or_null_member (object, "err_msg", msg); json_node_take_object (root, object); json_generator_set_root (generator, root); g_object_set (generator, "pretty", FALSE, NULL); data = json_generator_to_data (generator, len); json_node_free (root); g_object_unref (generator); return data; }
gboolean trg_prefs_save(TrgPrefs * p) { TrgPrefsPrivate *priv = p->priv; JsonGenerator *gen = json_generator_new(); gchar *dirName; gboolean success = TRUE; gboolean isNew = TRUE; dirName = g_path_get_dirname(priv->file); if (!g_file_test(dirName, G_FILE_TEST_IS_DIR)) { success = g_mkdir_with_parents(dirName, TRG_PREFS_DEFAULT_DIR_MODE) == 0; } else if (g_file_test(priv->file, G_FILE_TEST_IS_REGULAR)) { isNew = FALSE; } g_free(dirName); if (!success) { g_error ("Problem creating parent directory (permissions?) for: %s\n", priv->file); return success; } g_object_set(G_OBJECT(gen), "pretty", TRUE, NULL); json_generator_set_root(gen, priv->user); success = json_generator_to_file(gen, priv->file, NULL); if (!success) g_error("Problem writing configuration file (permissions?) to: %s", priv->file); else if (isNew) g_chmod(priv->file, 384); g_object_unref(gen); return success; }
/** * couchdb_struct_field_to_string: * @sf: A #CouchdbStructField object * * Convert a #CouchdbStructField to a JSON string. * * Return value: A string representing the contents of the given #CouchdbStructField * object in JSON format. */ char * couchdb_struct_field_to_string (CouchdbStructField *sf) { JsonNode *node; JsonGenerator *generator; gsize size; char *str = NULL; g_return_val_if_fail (sf != NULL, NULL); node = json_node_new (JSON_NODE_OBJECT); json_node_set_object (node, sf->json_object); generator = json_generator_new (); json_generator_set_root (generator, node); str = json_generator_to_data (generator, &size); g_object_unref (G_OBJECT (generator)); json_node_free (node); return str; }
static void glide_window_save_document_real (GlideWindow *w, const gchar *filename) { JsonNode *node; JsonGenerator *gen; node = glide_document_serialize (w->priv->document); gen = json_generator_new (); g_object_set (gen, "pretty", TRUE, NULL); json_generator_set_root (gen, node); // TODO: Error json_generator_to_file (gen, filename, NULL); glide_document_set_dirty (w->priv->document, FALSE); glide_document_set_path (w->priv->document, filename); // Maybe gets called twice? glide_window_update_title (w); }
gchar * searpc_marshal_set_ret_common (JsonObject *object, gsize *len, GError *error) { JsonNode *root = json_node_new (JSON_NODE_OBJECT); JsonGenerator *generator = json_generator_new (); gchar *data; if (error) { json_object_set_int_member (object, "err_code", error->code); json_object_set_string_or_null_member (object, "err_msg", error->message); g_error_free (error); } json_node_take_object (root, object); json_generator_set_root (generator, root); g_object_set (generator, "pretty", FALSE, NULL); data = json_generator_to_data (generator, len); json_node_free (root); g_object_unref (generator); return data; }
static void save_account_info(dt_storage_gphoto_gui_data_t *ui, dt_gphoto_account_info_t *accountinfo) { dt_gphoto_context_t *ctx = ui->gphoto_api; g_return_if_fail(ctx != NULL); /// serialize data; JsonBuilder *builder = json_builder_new(); json_builder_begin_object(builder); json_builder_set_member_name(builder, "username"); json_builder_add_string_value(builder, accountinfo->username); json_builder_set_member_name(builder, "userid"); json_builder_add_string_value(builder, accountinfo->id); json_builder_set_member_name(builder, "token"); json_builder_add_string_value(builder, accountinfo->token); json_builder_set_member_name(builder, "refresh_token"); json_builder_add_string_value(builder, accountinfo->refresh_token); json_builder_end_object(builder); JsonNode *node = json_builder_get_root(builder); JsonGenerator *generator = json_generator_new(); json_generator_set_root(generator, node); #if JSON_CHECK_VERSION(0, 14, 0) json_generator_set_pretty(generator, FALSE); #endif gchar *data = json_generator_to_data(generator, NULL); json_node_free(node); g_object_unref(generator); g_object_unref(builder); GHashTable *table = dt_pwstorage_get(GPHOTO_STORAGE); g_hash_table_insert(table, g_strdup(accountinfo->id), data); dt_pwstorage_set(GPHOTO_STORAGE, table); g_hash_table_destroy(table); }
gchar * test_generate_json (JsonObject *object, gboolean take_ownership) { JsonGenerator *generator; JsonNode *node; gchar *generated; generator = json_generator_new (); node = json_node_new (JSON_NODE_OBJECT); if (take_ownership) json_node_take_object (node, object); else json_node_set_object (node, object); json_generator_set_root (generator, node); json_generator_set_pretty (generator, TRUE); generated = json_generator_to_data (generator, NULL); g_object_unref (generator); json_node_free (node); return generated; }
static GPtrArray * gs_plugin_odrs_fetch_for_app (GsPlugin *plugin, GsApp *app, GError **error) { GsPluginData *priv = gs_plugin_get_data (plugin); const gchar *version; guint status_code; g_autofree gchar *cachefn_basename = NULL; g_autofree gchar *cachefn = NULL; g_autofree gchar *data = NULL; g_autofree gchar *uri = NULL; g_autoptr(GFile) cachefn_file = NULL; g_autoptr(GPtrArray) reviews = NULL; g_autoptr(JsonBuilder) builder = NULL; g_autoptr(JsonGenerator) json_generator = NULL; g_autoptr(JsonNode) json_root = NULL; g_autoptr(SoupMessage) msg = NULL; /* look in the cache */ cachefn_basename = g_strdup_printf ("%s.json", gs_app_get_id (app)); cachefn = gs_utils_get_cache_filename ("reviews", cachefn_basename, GS_UTILS_CACHE_FLAG_WRITEABLE, error); if (cachefn == NULL) return NULL; cachefn_file = g_file_new_for_path (cachefn); if (gs_utils_get_file_age (cachefn_file) < ODRS_REVIEW_CACHE_AGE_MAX) { g_autofree gchar *json_data = NULL; if (!g_file_get_contents (cachefn, &json_data, NULL, error)) return NULL; g_debug ("got review data for %s from %s", gs_app_get_id (app), cachefn); return gs_plugin_odrs_parse_reviews (plugin, json_data, -1, error); } /* not always available */ version = gs_app_get_version (app); if (version == NULL) version = "unknown"; /* create object with review data */ builder = json_builder_new (); json_builder_begin_object (builder); json_builder_set_member_name (builder, "user_hash"); json_builder_add_string_value (builder, priv->user_hash); json_builder_set_member_name (builder, "app_id"); json_builder_add_string_value (builder, gs_app_get_id (app)); json_builder_set_member_name (builder, "locale"); json_builder_add_string_value (builder, gs_plugin_get_locale (plugin)); json_builder_set_member_name (builder, "distro"); json_builder_add_string_value (builder, priv->distro); json_builder_set_member_name (builder, "version"); json_builder_add_string_value (builder, version); json_builder_set_member_name (builder, "limit"); json_builder_add_int_value (builder, ODRS_REVIEW_NUMBER_RESULTS_MAX); json_builder_end_object (builder); /* export as a string */ json_root = json_builder_get_root (builder); json_generator = json_generator_new (); json_generator_set_pretty (json_generator, TRUE); json_generator_set_root (json_generator, json_root); data = json_generator_to_data (json_generator, NULL); if (data == NULL) return NULL; uri = g_strdup_printf ("%s/fetch", priv->review_server); msg = soup_message_new (SOUP_METHOD_POST, uri); soup_message_set_request (msg, "application/json; charset=utf-8", SOUP_MEMORY_COPY, data, strlen (data)); status_code = soup_session_send_message (gs_plugin_get_soup_session (plugin), msg); if (status_code != SOUP_STATUS_OK) { if (!gs_plugin_odrs_parse_success (msg->response_body->data, msg->response_body->length, error)) return NULL; /* not sure what to do here */ g_set_error_literal (error, GS_PLUGIN_ERROR, GS_PLUGIN_ERROR_DOWNLOAD_FAILED, "status code invalid"); gs_utils_error_add_unique_id (error, priv->cached_origin); return NULL; } reviews = gs_plugin_odrs_parse_reviews (plugin, msg->response_body->data, msg->response_body->length, error); if (reviews == NULL) return NULL; g_debug ("odrs returned: %s", msg->response_body->data); /* save to the cache */ if (!g_file_set_contents (cachefn, msg->response_body->data, msg->response_body->length, error)) return NULL; /* success */ return g_steal_pointer (&reviews); }
static gboolean handler (GThreadedSocketService *service, GSocketConnection *connection, GSocketListener *listener, gpointer user_data) { GLibJsonRpcServerPrivate *jsonrpc_server = (GLibJsonRpcServerPrivate*)user_data; GError *error = NULL; // Check if it is a local connection. - This doesn't work! GSocketAddress *sockaddr = g_socket_connection_get_remote_address(connection, &error); GInetAddress *addr = g_inet_socket_address_get_address(G_INET_SOCKET_ADDRESS(sockaddr)); if (!jsonrpc_server->allow_non_loopback_connections) { #if 0 // Why doesn't this work? if (!g_inet_address_get_is_loopback(addr)) return TRUE; // just fail #endif gchar *addr_string = g_inet_address_to_string(addr); gboolean is_local = g_strstr_len(addr_string, -1, "127.0.0.1") != NULL; g_free(addr_string); if (!is_local) return TRUE; // silently fail } GOutputStream *out; GInputStream *in; char buffer[1024]; gssize size; out = g_io_stream_get_output_stream (G_IO_STREAM (connection)); in = g_io_stream_get_input_stream (G_IO_STREAM (connection)); // Read the http header gboolean skip_header = TRUE; JsonParser *parser = json_parser_new(); GString *json_string = g_string_new(""); while (0 < (size = g_input_stream_read (in, buffer, sizeof buffer, NULL, NULL))) { int header_size = 0; if (skip_header) { gchar *head_end = g_strstr_len(buffer, size, "\r\n\r\n"); if (head_end > 0) header_size = head_end - buffer; else continue; } g_string_append_len(json_string, buffer+header_size, size-header_size); if (json_parser_load_from_data(parser, json_string->str, -1, &error)) break; else g_error_free(error); } // TBD: raise error if there was a syntax error g_string_free(json_string, TRUE); // Get params object without the reader JsonNode *root = json_parser_get_root(parser); JsonObject *root_object = json_node_get_object(root); JsonNode* params = json_object_get_member(root_object, "params"); // Use reader for method and id JsonReader *reader = json_reader_new(json_parser_get_root(parser)); json_reader_read_member (reader, "method"); const gchar *method = json_reader_get_string_value (reader); json_reader_end_member (reader); json_reader_read_member (reader, "id"); gint64 id = json_reader_get_int_value(reader); json_reader_end_member (reader); // Build the response which is either a response object or an error object JsonNode *response = NULL; /* Call the callback */ command_hash_value_t *command_val = NULL; if (method) command_val = g_hash_table_lookup(jsonrpc_server->command_hash, method); if (!command_val) response = create_fault_msg_response(-2, "No such method!",id); else if (command_val->async_callback) { if (jsonrpc_server->async_busy) response = create_fault_msg_response(-2, "Busy!",id); else { // With the embedding of the mutex in the query we should // be able to handle more than one connection, so there // is no need to protect against a busy state. // jsonrpc_server->async_busy = TRUE; GLibJsonRpcAsyncQueryPrivate *query = glib_jsonrpc_server_query_new((GLibJsonRpcServer*)jsonrpc_server); // Create a secondary main loop (*command_val->async_callback)((GLibJsonRpcServer*)jsonrpc_server, (GLibJsonRpcAsyncQuery*)query, method, params, command_val->user_data); // Lock on a mutex g_mutex_lock(&query->mutex); g_cond_wait(&query->cond, &query->mutex); g_mutex_unlock(&query->mutex); if (query->error_num != 0) response = create_fault_value_response(query->error_num,query->reply,id); else response = create_response(query->reply, id); jsonrpc_server->async_busy = FALSE; // Erase the query glib_jsonrpc_async_query_free(query); } } else { JsonNode *reply; int ret = (*command_val->callback)((GLibJsonRpcServer*)jsonrpc_server, method, params, &reply, command_val->user_data); if (ret == 0) response = create_response(reply,id); else // For faults expect a string response containing the error response = create_fault_value_response(ret, reply,id); if (reply) json_node_free(reply); } if (response) { GString *response_string = g_string_new(""); // Serialize response into content_string JsonGenerator *gen = json_generator_new (); gsize len; json_generator_set_root (gen, response); json_node_free(response); gchar *content_string = json_generator_to_data(gen, &len); g_object_unref (gen); g_string_append_printf(response_string, "HTTP/1.1 200 OK\r\n" "Connection: close\r\n" "Content-Length: %d\r\n" "Content-Type: text/xml\r\n" "Date: Fri, 1 Jan 2000 00:00:00 GMT\r\n" "Server: GlibJsonRPC server\r\n" "\r\n" "%s", (int)len, content_string ); g_free(content_string); g_output_stream_write (out, response_string->str, response_string->len, NULL,NULL); g_string_free(response_string, TRUE); } g_object_unref(parser); return TRUE; }
void cb_tweet_load_from_json (CbTweet *tweet, JsonNode *status_node, gint64 account_id, GDateTime *now) { JsonObject *status; JsonObject *user; gboolean has_media; g_return_if_fail (CB_IS_TWEET (tweet)); g_return_if_fail (status_node != NULL); g_return_if_fail (now != NULL); status = json_node_get_object (status_node); user = json_object_get_object_member (status, "user"); tweet->id = json_object_get_int_member (status, "id"); tweet->retweet_count = (guint) json_object_get_int_member (status, "retweet_count"); tweet->favorite_count = (guint) json_object_get_int_member (status, "favorite_count"); cb_mini_tweet_parse (&tweet->source_tweet, status); has_media = json_array_size (json_object_get_object_member (status, "entities"), "media") > 0; if (json_object_has_member (status, "retweeted_status")) { JsonObject *rt = json_object_get_object_member (status, "retweeted_status"); JsonObject *rt_user = json_object_get_object_member (rt, "user"); tweet->retweeted_tweet = g_malloc (sizeof(CbMiniTweet)); cb_mini_tweet_init (tweet->retweeted_tweet); cb_mini_tweet_parse (tweet->retweeted_tweet, rt); cb_mini_tweet_parse_entities (tweet->retweeted_tweet, rt); tweet->avatar_url = g_strdup (json_object_get_string_member (rt_user, "profile_image_url")); if (json_object_get_boolean_member (rt_user, "protected")) tweet->state |= CB_TWEET_STATE_PROTECTED; if (json_object_get_boolean_member (rt_user, "verified")) tweet->state |= CB_TWEET_STATE_VERIFIED; if (usable_json_value (rt, "possibly_sensitive") && json_object_get_boolean_member (rt, "possibly_sensitive")) tweet->state |= CB_TWEET_STATE_NSFW; } else { cb_mini_tweet_parse_entities (&tweet->source_tweet, status); tweet->avatar_url = g_strdup (json_object_get_string_member (user, "profile_image_url")); if (json_object_get_boolean_member (user, "protected")) tweet->state |= CB_TWEET_STATE_PROTECTED; if (json_object_get_boolean_member (user, "verified")) tweet->state |= CB_TWEET_STATE_VERIFIED; if (usable_json_value (status, "possibly_sensitive") && json_object_get_boolean_member (status, "possibly_sensitive")) tweet->state |= CB_TWEET_STATE_NSFW; } if (json_object_has_member (status, "quoted_status") && !has_media) { JsonObject *quote = json_object_get_object_member (status, "quoted_status"); tweet->quoted_tweet = g_malloc (sizeof (CbMiniTweet)); cb_mini_tweet_init (tweet->quoted_tweet); cb_mini_tweet_parse (tweet->quoted_tweet, quote); cb_mini_tweet_parse_entities (tweet->quoted_tweet, quote); if (usable_json_value (quote, "possibly_sensitive") && json_object_get_boolean_member (quote, "possibly_sensitive")) tweet->state |= CB_TWEET_STATE_NSFW; else tweet->state &= ~CB_TWEET_STATE_NSFW; } else if (tweet->retweeted_tweet != NULL && tweet->retweeted_tweet->n_medias == 0 && json_object_has_member (json_object_get_object_member (status, "retweeted_status"), "quoted_status")) { JsonObject *quote = json_object_get_object_member (json_object_get_object_member (status, "retweeted_status"), "quoted_status"); tweet->quoted_tweet = g_malloc (sizeof (CbMiniTweet)); cb_mini_tweet_init (tweet->quoted_tweet); cb_mini_tweet_parse (tweet->quoted_tweet, quote); cb_mini_tweet_parse_entities (tweet->quoted_tweet, quote); if (usable_json_value (quote, "possibly_sensitive") && json_object_get_boolean_member (quote, "possibly_sensitive")) tweet->state |= CB_TWEET_STATE_NSFW; else tweet->state &= ~CB_TWEET_STATE_NSFW; } if (json_object_get_boolean_member (status, "favorited")) tweet->state |= CB_TWEET_STATE_FAVORITED; if (json_object_has_member (status, "current_user_retweet")) { JsonObject *cur_rt = json_object_get_object_member (status, "current_user_retweet"); tweet->my_retweet = json_object_get_int_member (cur_rt, "id"); tweet->state |= CB_TWEET_STATE_RETWEETED; } else if (json_object_get_boolean_member (status, "retweeted") || (tweet->retweeted_tweet != NULL && tweet->source_tweet.author.id == account_id)) { /* The 'retweeted' flag is not reliable so we additionally check if the tweet is authored by the authenticating user */ tweet->my_retweet = tweet->id; tweet->state |= CB_TWEET_STATE_RETWEETED; } #ifdef DEBUG { JsonGenerator *generator = json_generator_new (); json_generator_set_root (generator, status_node); json_generator_set_pretty (generator, TRUE); tweet->json_data = json_generator_to_data (generator, NULL); g_object_unref (generator); } #endif }
static gchar* do_get_action(IpcamIAjax *iajax, GList *item_list) { JsonBuilder *builder = NULL; JsonNode *res_node = NULL; GList *item; JsonGenerator *generator; GList *user; GList *users = ipcam_iajax_get_users(iajax); users = g_list_sort(users, (GCompareFunc)g_strcmp0); builder = json_builder_new(); json_builder_begin_object(builder); json_builder_set_member_name(builder, "items"); json_builder_begin_array(builder); for (user = g_list_first(users); user; user = g_list_next(user)) { json_builder_begin_object(builder); json_builder_set_member_name(builder, "username"); json_builder_add_string_value(builder, user->data); if (item_list) { for (item = g_list_first(item_list); item; item = g_list_next(item)) { if (g_str_equal(item->data, "password")) { gchar *pwd = ipcam_iajax_get_user_pwd(iajax, user->data); json_builder_set_member_name(builder, "password"); json_builder_add_string_value(builder, pwd); } else if (g_str_equal(item->data, "role")) { gchar *role = ipcam_iajax_get_user_role(iajax, user->data); json_builder_set_member_name(builder, "role"); json_builder_add_string_value(builder, role); g_free(role); } else { g_warn_if_reached(); } } } json_builder_end_object(builder); } g_list_free_full(users, g_free); json_builder_end_array(builder); json_builder_end_object(builder); generator = json_generator_new(); res_node = json_builder_get_root(builder); json_generator_set_root(generator, res_node); json_generator_set_pretty(generator, TRUE); gchar *result = json_generator_to_data(generator, NULL);; json_node_free(res_node); g_object_unref(G_OBJECT(builder)); g_object_unref(G_OBJECT(generator)); return result; }
/** * Do all tests according to the testing sequence * @param test Test details * @testpath Base path of tests * @return TRUE when all tests were verified ok */ gboolean tests_conduct_tests(testcase* test, gchar* testpath) { if(!test || !testpath) return FALSE; gboolean rval = TRUE; // Go through the test sequence for(gint testidx = 0; testidx < g_slist_length(test_sequence); testidx++) { // Get the item in test sequence gchar* searchparam = g_slist_nth_data(test_sequence,testidx); #ifdef G_MESSAGES_DEBUG g_print("Press enter to continue with test \"%s\" file id=\"%s\"",test->name,searchparam); gchar c = '0'; while(c != '\n') c = getc(stdin); #endif // Get the data with the searchparameter from hash table testfile* tfile = (testfile*)g_hash_table_find(test->files, (GHRFunc)find_from_hash_table, searchparam); // Read any other file except Empty.json if(g_strcmp0(tfile->file,"Empty.json") != 0) { // Create path for the file to be read gchar* filepath = g_strjoin("/",testpath,tfile->file,NULL); JsonParser *parser = json_parser_new(); // Read json detailed by this data (stucture) if(!load_json_from_file(parser, filepath)) return FALSE; // Do this only for files that are sent if(tests_file_sending_method(tfile->method)) tests_check_fields_from_loaded_testfile(parser, tfile, testpath); // Establish a generator to get the character representation JsonGenerator *generator = json_generator_new(); json_generator_set_root(generator, json_parser_get_root(parser)); // Create new jsonreply and set it to contain json as string data tfile->send = jsonreply_initialize(); tfile->send->data = json_generator_to_data(generator,&(tfile->send->length)); g_object_unref(generator); g_object_unref(parser); } // If path contains {id} it needs to be replaced with case id if(g_strrstr(tfile->path,"{id}")) { // Get case file testfile* temp = (testfile*)g_hash_table_find(test->files, (GHRFunc)find_from_hash_table, "0"); // Get case id gchar* caseid = get_value_of_member(temp->recv,"guid",NULL); if(caseid) { // Tokenize path gchar** split_path = g_strsplit(tfile->path,"/",5); // Go through the tokens and replace {id} with case id for(gint splitidx = 0; split_path[splitidx] ; splitidx++) { if(g_strcmp0(split_path[splitidx],"{id}") == 0) { g_free(split_path[splitidx]); split_path[splitidx] = caseid; } } // Replace the path with new g_free(tfile->path); tfile->path = g_strjoinv("/",split_path); g_strfreev(split_path); } } // Create url gchar* url = g_strjoin("/",test->URL,tfile->path,NULL); #ifdef G_MESSAGES_DEBUG g_print("Conducting test id \"%s\"\n",test->name); #endif // First is login, it is always first in the list if(testidx == 0) { tfile->recv = http_post(url,tfile->send,tfile->method); if(tfile->recv) { gchar* token = get_value_of_member(tfile->recv,"token",NULL); set_token(token); g_free(token); } else rval = FALSE; } // Case creation is second else if(testidx == 1) { tfile->recv = http_post(url,tfile->send,tfile->method); if(tfile->recv && verify_server_response(tfile->send,tfile->recv)) { g_print ("Case added correctly\n\n\n"); } else rval = FALSE; } // From third start the tests, here the required fields are checked and replaced else { gint index = 0; // Go through all fields having {parent} as value // Do this only for files that are sent if(tests_file_sending_method(tfile->method)) { for(index = 0; index < g_slist_length(tfile->required); index++) { //replace_required_member(test->files,tfile,index); // Use new function just to add member-new value pairs to hash table add_required_member_value_to_list(test->files,tfile,index); } // Go through the list of items requiring more info for(gint index = 0; index < g_slist_length(tfile->moreinfo); index++) { //replace_getinfo_member(tfile,index,test->URL); // Use new function just to add member-new value pairs to hash table add_getinfo_member_value_to_list(tfile,index,test->URL); } // Replace all values in the jsonreply_t data using the member-value // pairs in the replace hash table set_values_of_all_members(tfile->send, tfile->replace); } tfile->recv = http_post(url,tfile->send,tfile->method); // If there is something to verify if(tfile->send) { g_print("Verifying test id \"%s\" (file: %s):\n",tfile->id,tfile->file); if(verify_server_response(tfile->send,tfile->recv)) { g_print ("Test id \"%s\" was added correctly\n",tfile->id); } else { g_print("Test id \"%s\" was not added correctly\n", tfile->id); rval = FALSE; } g_print("\n\n"); } } g_free(url); } return rval; }
/** * Checks the fields from the file loaded in parser for presence of {parent} and * {getinfo} values. These member names containing such values are added to the * lists of the test files to be replaced later. * * @param parser - Parser where file is loaded * @param tfile - testfile containing details * @param testpath - Base path to tests */ void tests_check_fields_from_loaded_testfile(JsonParser *parser,testfile *tfile, gchar* testpath) { gchar* filepath = g_strjoin("/",testpath,tfile->file,NULL); JsonReader *reader = json_reader_new (json_parser_get_root (parser)); gchar** members = json_reader_list_members(reader); // Go through all members of this json for(gint membidx = 0; members[membidx] != NULL; membidx++) { // Get the value of current member gchar* membstring = get_json_member_string(reader,members[membidx]); // Requires information from other file if(g_strcmp0(membstring,"{parent}") == 0) { // Add member name to list tfile->required = g_slist_append(tfile->required,g_strdup(members[membidx])); JsonParser *par_parser = json_parser_new(); // Create file offering more information gchar* par_infopath = g_strjoin(".",filepath,"info",members[membidx],"json",NULL); if(load_json_from_file(par_parser,par_infopath)) { JsonGenerator *par_generator = json_generator_new(); json_generator_set_root(par_generator, json_parser_get_root(par_parser)); // Initialize struct for the new json and store json jsonreply* info = jsonreply_initialize(); info->data = json_generator_to_data(par_generator,&(info->length)); // To verify that this item is in correct position in the list // and corresponds to the member string location gint add_position = g_slist_length(tfile->required) - 1; tfile->reqinfo = g_slist_insert(tfile->reqinfo,info,add_position); g_object_unref(par_generator); } g_free(par_infopath); g_object_unref(par_parser); } // Requires more information from the server if(g_strcmp0(membstring,"{getinfo}") == 0) { // Add member name to list tfile->moreinfo = g_slist_append(tfile->moreinfo,g_strdup(members[membidx])); JsonParser *info_parser = json_parser_new(); // Create path to the file offering more information gchar* infopath = g_strjoin(".",filepath,"getinfo",members[membidx],"json",NULL); // Load the json file if(load_json_from_file(info_parser,infopath)) { JsonGenerator *info_generator = json_generator_new(); json_generator_set_root(info_generator, json_parser_get_root(info_parser)); // Initialize struct for the new json and store json jsonreply* info = jsonreply_initialize(); info->data = json_generator_to_data(info_generator,&(info->length)); // To verify that this item is in correct position in the list // and corresponds to the member string location gint add_position = g_slist_length(tfile->moreinfo) - 1; tfile->infosend = g_slist_insert(tfile->infosend,info,add_position); g_object_unref(info_generator); } g_free(infopath); g_object_unref(info_parser); } g_free(membstring); } g_strfreev(members); g_free(filepath); g_object_unref(reader); }
gboolean gs_plugin_review_submit (GsPlugin *plugin, GsApp *app, AsReview *review, GCancellable *cancellable, GError **error) { GsPluginData *priv = gs_plugin_get_data (plugin); g_autofree gchar *data = NULL; g_autofree gchar *uri = NULL; g_autofree gchar *version = NULL; g_autoptr(JsonBuilder) builder = NULL; g_autoptr(JsonGenerator) json_generator = NULL; g_autoptr(JsonNode) json_root = NULL; /* save as we don't re-request the review from the server */ as_review_set_reviewer_name (review, g_get_real_name ()); as_review_add_metadata (review, "app_id", gs_app_get_id (app)); as_review_add_metadata (review, "user_skey", gs_app_get_metadata_item (app, "ODRS::user_skey")); /* create object with review data */ builder = json_builder_new (); json_builder_begin_object (builder); json_builder_set_member_name (builder, "user_hash"); json_builder_add_string_value (builder, priv->user_hash); json_builder_set_member_name (builder, "user_skey"); json_builder_add_string_value (builder, as_review_get_metadata_item (review, "user_skey")); json_builder_set_member_name (builder, "app_id"); json_builder_add_string_value (builder, as_review_get_metadata_item (review, "app_id")); json_builder_set_member_name (builder, "locale"); json_builder_add_string_value (builder, gs_plugin_get_locale (plugin)); json_builder_set_member_name (builder, "distro"); json_builder_add_string_value (builder, priv->distro); json_builder_set_member_name (builder, "version"); version = gs_plugin_odrs_sanitize_version (as_review_get_version (review)); json_builder_add_string_value (builder, version); json_builder_set_member_name (builder, "user_display"); json_builder_add_string_value (builder, as_review_get_reviewer_name (review)); json_builder_set_member_name (builder, "summary"); json_builder_add_string_value (builder, as_review_get_summary (review)); json_builder_set_member_name (builder, "description"); json_builder_add_string_value (builder, as_review_get_description (review)); json_builder_set_member_name (builder, "rating"); json_builder_add_int_value (builder, as_review_get_rating (review)); json_builder_end_object (builder); /* export as a string */ json_root = json_builder_get_root (builder); json_generator = json_generator_new (); json_generator_set_pretty (json_generator, TRUE); json_generator_set_root (json_generator, json_root); data = json_generator_to_data (json_generator, NULL); /* clear cache */ if (!gs_plugin_odrs_invalidate_cache (review, error)) return FALSE; /* POST */ uri = g_strdup_printf ("%s/submit", priv->review_server); return gs_plugin_odrs_json_post (gs_plugin_get_soup_session (plugin), uri, data, error); }
/** * push_gcm_client_deliver_async: * @client: (in): A #PushGcmClient. * @identities: (element-type PushGcmIdentity*): A #GList of #PushGcmIdentity. * @message: A #PushGcmMessage. * @cancellable: (allow-none): A #GCancellable or %NULL. * @callback: A #GAsyncReadyCallback. * @user_data: User data for @callback. * * Asynchronously deliver a #PushGcmMessage to one or more GCM enabled * devices. */ void push_gcm_client_deliver_async (PushGcmClient *client, GList *identities, PushGcmMessage *message, GCancellable *cancellable, GAsyncReadyCallback callback, gpointer user_data) { PushGcmClientPrivate *priv; GSimpleAsyncResult *simple; SoupMessage *request; const gchar *registration_id; const gchar *collapse_key; JsonGenerator *g; JsonObject *obj; JsonObject *data; JsonObject *mdata; JsonArray *ar; JsonNode *node; GList *iter; GList *list; gchar *str; gsize length; guint time_to_live; ENTRY; g_return_if_fail(PUSH_IS_GCM_CLIENT(client)); g_return_if_fail(identities); g_return_if_fail(PUSH_IS_GCM_MESSAGE(message)); g_return_if_fail(!cancellable || G_IS_CANCELLABLE(cancellable)); g_return_if_fail(callback); priv = client->priv; request = soup_message_new("POST", PUSH_GCM_CLIENT_URL); ar = json_array_new(); for (iter = identities; iter; iter = iter->next) { g_assert(PUSH_IS_GCM_IDENTITY(iter->data)); registration_id = push_gcm_identity_get_registration_id(iter->data); json_array_add_string_element(ar, registration_id); } str = g_strdup_printf("key=%s", priv->auth_token); soup_message_headers_append(request->request_headers, "Authorization", str); g_free(str); soup_message_headers_append(request->request_headers, "Accept", "application/json"); data = json_object_new(); if ((collapse_key = push_gcm_message_get_collapse_key(message))) { json_object_set_string_member(data, "collapse_key", collapse_key); } json_object_set_boolean_member(data, "delay_while_idle", push_gcm_message_get_delay_while_idle(message)); json_object_set_boolean_member(data, "dry_run", push_gcm_message_get_dry_run(message)); if ((time_to_live = push_gcm_message_get_time_to_live(message))) { json_object_set_int_member(data, "time_to_live", time_to_live); } if ((mdata = push_gcm_message_get_data(message))) { json_object_set_object_member(data, "data", mdata); } obj = json_object_new(); json_object_set_array_member(obj, "registration_ids", ar); json_object_set_object_member(obj, "data", data); node = json_node_new(JSON_NODE_OBJECT); json_node_set_object(node, obj); json_object_unref(obj); g = json_generator_new(); json_generator_set_pretty(g, TRUE); json_generator_set_indent(g, 2); json_generator_set_root(g, node); str = json_generator_to_data(g, &length); json_node_free(node); g_object_unref(g); g_print("REQUEST: \"%s\"\n", str); soup_message_set_request(request, "application/json", SOUP_MEMORY_TAKE, str, length); simple = g_simple_async_result_new(G_OBJECT(client), callback, user_data, push_gcm_client_deliver_async); /* * Keep the list of identities around until we receive our result. * We need them to key with the resulting array. */ list = g_list_copy(identities); g_list_foreach(list, (GFunc)g_object_ref, NULL); g_object_set_data_full(G_OBJECT(simple), "identities", list, _push_gcm_identities_free); soup_session_queue_message(SOUP_SESSION(client), request, push_gcm_client_deliver_cb, simple); EXIT; }
/** * Save keymap for given input mode. * * @param input_mode input mode */ void kkc_user_rule_write (KkcUserRule* self, KkcInputMode input_mode, GError** error) { GEnumClass* enum_class = NULL; GTypeClass* _tmp0_ = NULL; gchar* keymap_name = NULL; KkcInputMode _tmp1_ = 0; GEnumValue* _tmp2_ = NULL; const gchar* _tmp3_ = NULL; gchar* _tmp4_ = NULL; gchar* keymap_path = NULL; const gchar* _tmp5_ = NULL; gchar* _tmp6_ = NULL; JsonGenerator* generator = NULL; JsonGenerator* _tmp7_ = NULL; JsonBuilder* builder = NULL; KkcRuleMetadata* _tmp8_ = NULL; KkcInputMode _tmp9_ = 0; KkcKeymap* _tmp10_ = NULL; KkcKeymap* _tmp11_ = NULL; JsonBuilder* _tmp12_ = NULL; JsonBuilder* _tmp13_ = NULL; JsonNode* _tmp14_ = NULL; JsonNode* _tmp15_ = NULL; gchar* filename = NULL; gchar* _tmp16_ = NULL; gchar* _tmp17_ = NULL; gchar* _tmp18_ = NULL; gchar* _tmp19_ = NULL; GError * _inner_error_ = NULL; g_return_if_fail (self != NULL); _tmp0_ = g_type_class_ref (KKC_TYPE_INPUT_MODE); enum_class = (GEnumClass*) _tmp0_; _tmp1_ = input_mode; _tmp2_ = g_enum_get_value (enum_class, (gint) _tmp1_); _tmp3_ = (*_tmp2_).value_nick; _tmp4_ = g_strdup (_tmp3_); keymap_name = _tmp4_; _tmp5_ = self->priv->path; _tmp6_ = g_build_filename (_tmp5_, "keymap", NULL); keymap_path = _tmp6_; g_mkdir_with_parents (keymap_path, 448); _tmp7_ = json_generator_new (); generator = _tmp7_; json_generator_set_pretty (generator, TRUE); _tmp8_ = self->priv->parent; _tmp9_ = input_mode; _tmp10_ = kkc_rule_get_keymap ((KkcRule*) self, _tmp9_); _tmp11_ = _tmp10_; _tmp12_ = kkc_user_rule_create_keymap (_tmp8_, keymap_name, _tmp11_); _tmp13_ = _tmp12_; _g_object_unref0 (_tmp11_); builder = _tmp13_; _tmp14_ = json_builder_get_root (builder); _tmp15_ = _tmp14_; json_generator_set_root (generator, _tmp15_); __vala_JsonNode_free0 (_tmp15_); _tmp16_ = g_strdup_printf ("%s.json", keymap_name); _tmp17_ = _tmp16_; _tmp18_ = g_build_filename (keymap_path, _tmp17_, NULL); _tmp19_ = _tmp18_; _g_free0 (_tmp17_); filename = _tmp19_; json_generator_to_file (generator, filename, &_inner_error_); if (G_UNLIKELY (_inner_error_ != NULL)) { g_propagate_error (error, _inner_error_); _g_free0 (filename); _g_object_unref0 (builder); _g_object_unref0 (generator); _g_free0 (keymap_path); _g_free0 (keymap_name); _g_type_class_unref0 (enum_class); return; } _g_free0 (filename); _g_object_unref0 (builder); _g_object_unref0 (generator); _g_free0 (keymap_path); _g_free0 (keymap_name); _g_type_class_unref0 (enum_class); }