Beispiel #1
0
/* TODO: use a command_finalize_func for that too */
void interface_notify() {
    GString* str = g_string_sized_new(1024);
    JsonBuilder* jb = json_builder_new();
    command_context ctx = { jb, NULL, NULL };

    json_builder_begin_object(jb);
    status(&ctx);
    json_builder_end_object(jb);

    JsonGenerator *gen = json_generator_new();
    g_object_set(gen, "pretty", config_get_bool_opt("pretty_json", FALSE), NULL);
    json_generator_set_root(gen, json_builder_get_root(jb));

    gchar *tmp = json_generator_to_data(gen, NULL);
    g_string_assign(str, tmp);
    g_string_append(str, "\n");

    g_object_unref(gen);
    g_object_unref(jb);
    g_free(tmp);

    /* First notify idle channels */
    g_list_foreach(g_idle_channels, interface_notify_chan, str);
    g_list_free(g_idle_channels);
    g_idle_channels = NULL;

    /* Then call callbacks from plugins */
    g_list_foreach(g_notification_callbacks, interface_notify_callback, str);

    g_string_free(str, TRUE);
}
Beispiel #2
0
static void _piwigo_set_account(dt_storage_piwigo_gui_data_t *ui)
{
  /// serialize data;
  JsonBuilder *builder = json_builder_new();
  json_builder_begin_object(builder);
  json_builder_set_member_name(builder, "server");
  json_builder_add_string_value(builder, gtk_entry_get_text(ui->server_entry));
  json_builder_set_member_name(builder, "username");
  json_builder_add_string_value(builder, gtk_entry_get_text(ui->user_entry));
  json_builder_set_member_name(builder, "password");
  json_builder_add_string_value(builder, gtk_entry_get_text(ui->pwd_entry));

  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("piwigo");
  g_hash_table_insert(table, g_strdup(gtk_entry_get_text(ui->server_entry)), data);
  dt_pwstorage_set("piwigo", table);
  g_hash_table_destroy(table);
}
Beispiel #3
0
static JSValueRef
js_emit (JSContextRef ctx, JSObjectRef object, JSObjectRef thisObject,
	 size_t argumentCount, const JSValueRef arguments[],
	 JSValueRef * exception)
{
  JsonObject *obj;
  JsonNode *node;
  gchar *buffer;
  if (argumentCount != 1)
    {
      *exception = JSValueMakeNumber (ctx, 1);
      return NULL;
    }
  if (JSValueIsObject (ctx, arguments[0]) == false)
    {
      *exception = JSValueMakeNumber (ctx, 1);
      return NULL;
    }

  obj = json_object_new();

  js_value (ctx, (JSObjectRef) arguments[0], &node);
  json_object_set_member (obj, "emit", node);

  JsonNode *node1 = json_node_new (JSON_NODE_OBJECT);

  if (node1 == NULL)
    {
      json_object_unref (obj);
      return NULL;
    }

  json_node_set_object (node1, obj);

  JsonGenerator *gen = json_generator_new();

  if (gen == NULL)
    {
      json_node_free (node1);
      return NULL;
    }

  json_generator_set_root (gen, node1 );
  buffer = json_generator_to_data (gen,NULL);

  if (buffer == NULL)
    {
      json_node_free (node1);
      g_object_unref (gen);
      return NULL;
    }

  json_node_free (node1);
  g_object_unref (gen);

  puts (buffer);
  g_free (buffer);

  return NULL; /* shouldn't be an object ? */
}
Beispiel #4
0
static void save_account_info(dt_storage_facebook_gui_data_t *ui, FBAccountInfo *accountinfo)
{
  FBContext *ctx = ui->facebook_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, "token");
  json_builder_add_string_value(builder, accountinfo->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("facebook");
  g_hash_table_insert(table, accountinfo->id, data);
  dt_pwstorage_set("facebook", table);

  g_hash_table_destroy(table);
}
Beispiel #5
0
static void
send_response(SoupWebsocketConnection *ws,
            const gchar *protocol, const gchar *command, JsonObject *payload)
{
    g_return_if_fail(ws);

    JsonObject *response = json_object_new();
    g_assert(response);

    json_object_set_string_member(response, "protocol", protocol);
    json_object_set_string_member(response, "command", command);
    json_object_set_object_member(response, "payload", payload);

    JsonGenerator *generator = json_generator_new();
    JsonNode *node = json_node_new(JSON_NODE_OBJECT);
    json_node_take_object(node, response);
    json_generator_set_root(generator, node);

    gsize len = 0;
    gchar *data = json_generator_to_data(generator, &len);
    GBytes *resp = g_bytes_new_take(data, len);
    g_print ("SEND: %.*s\n", (int)len, data);
    soup_websocket_connection_send(ws, SOUP_WEBSOCKET_DATA_TEXT, resp);

    g_object_unref(generator);
}
Beispiel #6
0
static char *
file_list_to_json (GList *files)
{
    JsonNode *root;
    JsonArray *array;
    GList *ptr;
    char *file;
    JsonGenerator *gen;
    char *json_data;
    gsize len;

    root = json_node_new (JSON_NODE_ARRAY);
    array = json_array_new ();

    for (ptr = files; ptr; ptr = ptr->next) {
        file = ptr->data;
        json_array_add_string_element (array, file);
    }
    json_node_set_array (root, array);

    gen = json_generator_new ();
    json_generator_set_root (gen, root);
    json_data = json_generator_to_data (gen, &len);
    json_node_free (root);
    g_object_unref (gen);

    return json_data;
}
static std::string toJson(GHashTable *dict)
{
    fprintf(stderr, "JSONDocument::toJson\n");
    JsonBuilder *builder = json_builder_new ();

    json_builder_begin_object (builder);

    if (dict != NULL)
    {
        g_hash_table_foreach (dict, insertToJson, builder);
    }

    json_builder_end_object (builder);

    JsonGenerator *generator = json_generator_new ();
    json_generator_set_root (generator, json_builder_get_root (builder));
    gchar *str = json_generator_to_data (generator, 0);

    std::string msg(str);

    g_object_unref (generator);
    g_object_unref (builder);

    //g_print("[%s]\n", msg.c_str());

    return msg + "\n";
}
Beispiel #8
0
/**
 * json_gobject_to_data:
 * @gobject: a #GObject
 * @length: (out): return value for the length of the buffer, or %NULL
 *
 * Serializes a #GObject into a JSON data stream, iterating recursively
 * over each property.
 *
 * If @gobject implements the #JsonSerializableIface interface, it will
 * be asked to serialize all its properties; otherwise, the default
 * implementation will be use to translate the compatible types into
 * JSON native types.
 *
 * Return value: a JSON data stream representing the passed #GObject
 *
 * Since: 0.10
 */
gchar *
json_gobject_to_data (GObject *gobject,
                      gsize   *length)
{
  JsonGenerator *gen;
  JsonNode *root;
  gchar *data;

  g_return_val_if_fail (G_OBJECT (gobject), NULL);

  root = json_gobject_serialize (gobject);

  gen = g_object_new (JSON_TYPE_GENERATOR,
                      "root", root,
                      "pretty", TRUE,
                      "indent", 2,
                      NULL);

  data = json_generator_to_data (gen, length);
  g_object_unref (gen);

  json_node_free (root);

  return data;
}
Beispiel #9
0
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;
        if (g_str_equal(name, "method"))
        {
            GVariant *value = NULL;
            value = ipcam_iajax_get_configuration(iajax, "network:method");
            if (value)
            {
                add_value(builder, name, value);
                g_variant_unref(value);
            }
        }
        else if (g_str_equal(name, "address"))
        {
            do_get_action_address(iajax, builder);
        }
        else if (g_str_equal(name, "pppoe"))
        {
            do_get_action_pppoe(iajax, builder);
        }
        else if (g_str_equal(name, "port"))
        {
            do_get_action_port(iajax, builder);
        }
        else
        {
            g_warn_if_reached();
        }
    }
    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;
}
Beispiel #10
0
static gboolean
gs_plugin_odrs_vote (GsPlugin *plugin, AsReview *review,
		     const gchar *uri, GError **error)
{
	GsPluginData *priv = gs_plugin_get_data (plugin);
	const gchar *tmp;
	g_autofree gchar *data = NULL;
	g_autoptr(JsonBuilder) builder = NULL;
	g_autoptr(JsonGenerator) json_generator = NULL;
	g_autoptr(JsonNode) json_root = NULL;

	/* create object with vote 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"));
	tmp = as_review_get_id (review);
	if (tmp != NULL) {
		gint64 review_id;
		json_builder_set_member_name (builder, "review_id");
		review_id = g_ascii_strtoll (tmp, NULL, 10);
		json_builder_add_int_value (builder, review_id);
	}
	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 FALSE;

	/* clear cache */
	if (!gs_plugin_odrs_invalidate_cache (review, error))
		return FALSE;

	/* send to server */
	if (!gs_plugin_odrs_json_post (gs_plugin_get_soup_session (plugin),
						  uri, data, error))
		return FALSE;

	/* mark as voted */
	as_review_add_flags (review, AS_REVIEW_FLAG_VOTED);

	/* success */
	return TRUE;
}
Beispiel #11
0
/**
 * fwupd_build_history_report_json:
 * @devices: (element-type FwupdDevice): devices
 * @error: A #GError or %NULL
 *
 * Builds a JSON report for the list of devices. No filtering is done on the
 * @devices array, and it is expected that the caller will filter to something
 * sane, e.g. %FWUPD_DEVICE_FLAG_REPORTED at the bare minimum.
 *
 * Returns: a string, or %NULL if the ID is not present
 *
 * Since: 1.0.4
 **/
gchar *
fwupd_build_history_report_json (GPtrArray *devices, GError **error)
{
	gchar *data;
	g_autofree gchar *machine_id = NULL;
	g_autoptr(JsonBuilder) builder = NULL;
	g_autoptr(JsonGenerator) json_generator = NULL;
	g_autoptr(JsonNode) json_root = NULL;

	/* get a hash that represents the machine */
	machine_id = fwupd_build_machine_id ("fwupd", error);
	if (machine_id == NULL)
		return NULL;

	/* create header */
	builder = json_builder_new ();
	json_builder_begin_object (builder);
	json_builder_set_member_name (builder, "ReportVersion");
	json_builder_add_int_value (builder, 2);
	json_builder_set_member_name (builder, "MachineId");
	json_builder_add_string_value (builder, machine_id);

	/* this is system metadata not stored in the database */
	json_builder_set_member_name (builder, "Metadata");
	json_builder_begin_object (builder);
	if (!fwupd_build_history_report_json_metadata (builder, error))
		return NULL;
	json_builder_end_object (builder);

	/* add each device */
	json_builder_set_member_name (builder, "Reports");
	json_builder_begin_array (builder);
	for (guint i = 0; i < devices->len; i++) {
		FwupdDevice *dev = g_ptr_array_index (devices, i);
		json_builder_begin_object (builder);
		fwupd_build_history_report_json_device (builder, dev);
		json_builder_end_object (builder);
	}
	json_builder_end_array (builder);
	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) {
		g_set_error_literal (error,
				     FWUPD_ERROR,
				     FWUPD_ERROR_INTERNAL,
				     "Failed to convert to JSON string");
		return NULL;
	}
	return data;
}
/** 
 * Utility function for turning a json node into a string.
 */
gchar *glib_jsonrpc_json_to_string(JsonNode *node)
{
  // Serialize response into content_string
  JsonGenerator *gen = json_generator_new ();
  gsize len;
  json_generator_set_root (gen, node);
  gchar *json_string = json_generator_to_data(gen, &len);
  g_object_unref (gen);
  return json_string;
}
Beispiel #13
0
gboolean
tests_common_check_set (GHashTable *data, const gchar *id, GdaSet *set, GError **error)
{
	gchar *s;
	const gchar *got = NULL;

	s = tests_common_set_serialize (set);
	
	if (id)
		got = g_hash_table_lookup (data, id);
	if (!got) {
#ifdef HAVE_JSON_GLIB
		JsonParser *jparser;
		jparser = json_parser_new ();
		if (!json_parser_load_from_data (jparser, s, -1, NULL)) 
			g_set_error (error, TEST_ERROR, TEST_ERROR_GENERIC,
				     "Unknown ID '%s', GdaSet is: %s (JSON INVALID)\n", id, s);
		else {
			JsonGenerator *jgen;
			gchar *out;
			jgen = json_generator_new ();
			g_object_set (G_OBJECT (jgen), "pretty", TRUE, "indent", 5, NULL);
			json_generator_set_root (jgen, json_parser_get_root (jparser));
			out = json_generator_to_data (jgen, NULL);
			g_set_error (error, TEST_ERROR, TEST_ERROR_GENERIC,
				     "Unknown ID '%s', GdaSet is: %s\nJSON: %s\n", id, s, out);
			g_free (out);
			g_object_unref (jgen);
		}
		g_object_unref (jparser);
#else
		g_set_error (error, TEST_ERROR, TEST_ERROR_GENERIC,
			     "Unknown ID '%s', GdaSet is: %s\n", id, s);
#endif

		g_free (s);
		g_object_unref (set);
		return FALSE;
	}

	if (strcmp (got, s)) {
		g_set_error (error, TEST_ERROR, TEST_ERROR_GENERIC,
			     "GdaSet error:\nexp: %s\ngot: %s\n", got, s);
		g_free (s);
		g_object_unref (set);
		return FALSE;
	}
	g_free (s);
	return TRUE;
}
Beispiel #14
0
std::string JSONBuilder::get_string(Node root_node, bool pretty) {
  JsonGenerator *generator = json_generator_new();
  gsize length = 0;
  if (pretty)
    json_generator_set_pretty(generator, TRUE);
  else
    json_generator_set_pretty(generator, FALSE);
  json_generator_set_root(generator, root_node->get());
  gchar *data = json_generator_to_data(generator, &length);
  std::string description(data);
  g_free(data);
  g_object_unref(generator);
  return description;
}
int
main (int argc, char *argv[])
{

    MyFlowArrow *arrow;
    MySystem *system;

    JsonGenerator *gen = json_generator_new ();
    JsonNode *node, *root;
    JsonArray *array;
    gchar *data;
    gsize len;

    gtk_init (NULL, NULL);
    libgoffice_init ();

    root = json_node_new (JSON_NODE_ARRAY);

    array = json_array_new ();

    arrow =
        g_object_new (MY_TYPE_FLOW_ARROW,
                      "x0", 100.0,
                      "x1", 200.0,
                      "y0", 200.0,
                      "y1", 300.0,
                      "energy-quantity", -22.0, 
                      "anchor", MY_ANCHOR_NORTH,
                      "label-text", "test", NULL);

    node = json_gobject_serialize (G_OBJECT (arrow));
    json_array_add_element (array, node);

    system = g_object_new (MY_TYPE_SYSTEM, "x", 100.0, "y", 200.0, NULL);
    node = json_gobject_serialize (G_OBJECT (system));
    json_array_add_element (array, node);

    json_node_set_array (root, array);

    json_generator_set_root (gen, root);
    json_generator_set_pretty (gen, TRUE);

    data = json_generator_to_data (gen, &len);

    g_print ("%s\n", data);

    libgoffice_shutdown ();

    return 0;
}
Beispiel #16
0
/** 
 * Utility function for turning a json node into a string.
 */
gchar *glib_jsonrpc_json_to_string(JsonNode *node)
{
  // Simplify for string. Should probably be optional.
  if (json_node_get_string (node) != NULL)
    return g_strdup(json_node_get_string (node));

  // Serialize response into content_string
  JsonGenerator *gen = json_generator_new ();
  gsize len;
  json_generator_set_root (gen, node);
  gchar *json_string = json_generator_to_data(gen, &len);
  g_object_unref (gen);
  return json_string;
}
Beispiel #17
0
static gchar*
to_item_json (MarinaItem *item)
{
  /* NOTE: When WebKit gets GObject DOM support, we should be injecting
   *    the item directly into the dom. This will get us by for now.
   */
  
  if (!item)
    return g_strdup ("{}");
  
  gchar         *json;
  MarinaDate    *item_date = marina_item_get_published_at (item);
  JsonGenerator *generator = json_generator_new ();
  JsonNode      *root      = json_node_new (JSON_NODE_OBJECT);
  JsonObject    *object    = json_object_new ();
  JsonNode      *author;
  JsonNode      *content   = to_json_string (marina_item_get_content (item));
  JsonNode      *date      = to_json_string (marina_date_to_string (item_date));
  JsonNode      *link      = to_json_string (marina_item_get_link (item));
  JsonNode      *title;

  if (!marina_item_get_author (item) ||
      g_str_equal (marina_item_get_author (item), ""))
    author = to_json_string (_("Unknown"));
  else
    author = to_json_string (marina_item_get_author (item));
  
  if (!marina_item_get_title (item) ||
      g_str_equal (marina_item_get_title (item), ""))
    title = to_json_string (_("Untitled"));
  else
    title = to_json_string (marina_item_get_title (item));
  
  json_object_add_member (object, "author", author);
  json_object_add_member (object, "content", content);
  json_object_add_member (object, "date", date);
  json_object_add_member (object, "link", link);
  json_object_add_member (object, "title", title);
  
  json_node_take_object (root, object);
  json_generator_set_root (generator, root);
  json = json_generator_to_data (generator, NULL);
  
  json_node_free (root);
  g_object_unref (generator);
  
  return json;
}
Beispiel #18
0
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 */
}
Beispiel #19
0
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);
}
Beispiel #20
0
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;
}
/* This function is asynchronous .. slightly worried about re-entrancy here */
static void
mex_queue_model_save (MexQueueModel *model)
{
  GFile *f;
  gchar *filename;
  JsonGenerator *generator;
  gchar *buf;
  gsize buf_len;

  filename = _queue_file_name ();
  f = g_file_new_for_path (filename);

  if (mex_model_get_length (MEX_MODEL (model)) == 0)
    {
      GError *error = NULL;

      if (!g_file_delete (f, NULL, &error))
        {
          g_warning (G_STRLOC ": Unable to delete file: %s",
                     error->message);
          g_clear_error (&error);
        }

      g_object_unref (f);
      g_free (filename);
      return;
    }

  generator = _model_to_generator (model);

  buf = json_generator_to_data (generator, &buf_len);

  g_file_replace_contents_async (f,
                                 buf,
                                 buf_len,
                                 NULL,
                                 FALSE,
                                 G_FILE_CREATE_REPLACE_DESTINATION,
                                 NULL,
                                 (GAsyncReadyCallback)_file_replaced_cb,
                                 buf);

  g_object_unref (f);
  g_free (filename);
  g_object_unref (generator);
}
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 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;
}
Beispiel #25
0
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);
}
Beispiel #26
0
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;
}
Beispiel #27
0
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);
}
Beispiel #28
0
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;
}
Beispiel #29
0
/* 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;
}
Beispiel #30
0
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;
}