summaryrefslogtreecommitdiff
path: root/src/json.c
diff options
context:
space:
mode:
authorGravatar Alan Fitton <ajf@eth0.org.uk>2011-01-30 10:57:07 +0000
committerGravatar Alan Fitton <ajf@eth0.org.uk>2011-01-30 10:57:07 +0000
commiteca35c468094fc6b7177f33ef51fa873eb88e79c (patch)
treefa07fd0ea1db72ff2ad4bd8b04b8e546717f2731 /src/json.c
hello world!
Diffstat (limited to 'src/json.c')
-rw-r--r--src/json.c93
1 files changed, 93 insertions, 0 deletions
diff --git a/src/json.c b/src/json.c
new file mode 100644
index 0000000..fab44a8
--- /dev/null
+++ b/src/json.c
@@ -0,0 +1,93 @@
+/*
+ * transmission-remote-gtk - A GTK RPC client to Transmission
+ * Copyright (C) 2010 Alan Fitton
+
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#include <glib-object.h>
+#include <glib/gprintf.h>
+#include <json-glib/json-glib.h>
+#include <gtk/gtk.h>
+
+#include "protocol-constants.h"
+#include "requests.h"
+#include "dispatch.h"
+#include "http.h"
+#include "json.h"
+
+gchar *trg_serialize(JsonNode * req)
+{
+ JsonGenerator *generator;
+ gsize length;
+ gchar *response;
+
+ generator = json_generator_new();
+ json_generator_set_root(generator, req);
+
+ response = json_generator_to_data(generator, &length);
+ g_object_unref(generator);
+
+ return response;
+}
+
+JsonObject *trg_deserialize(struct http_response * response,
+ GError ** error)
+{
+ JsonParser *parser;
+ JsonNode *root;
+ JsonObject *ret = NULL;
+
+ parser = json_parser_new();
+ json_parser_load_from_data(parser, response->data, response->size,
+ error);
+ if (*error == NULL) {
+ root = json_parser_get_root(parser);
+ if (g_getenv("SHOW_INCOMING") != NULL) {
+ g_printf("incoming JSON data:\n%s\n", response->data);
+ } else if (g_getenv("SHOW_INCOMING_PRETTY") != NULL) {
+ JsonGenerator *pg;
+ gsize len;
+ gchar *pgdata;
+
+ pg = json_generator_new();
+ g_object_set(pg, "pretty", TRUE, NULL);
+ json_generator_set_root(pg, root);
+
+ pgdata = json_generator_to_data(pg, &len);
+ g_printf("incoming JSON data:\n%s\n", pgdata);
+ g_free(pgdata);
+
+ g_object_unref(pg);
+ }
+
+ ret = json_node_get_object(root);
+ json_object_ref(ret);
+ }
+
+ g_object_unref(parser);
+ return ret;
+}
+
+JsonObject *node_get_arguments(JsonNode * req)
+{
+ JsonObject *rootObj = json_node_get_object(req);
+ return get_arguments(rootObj);
+}
+
+JsonObject *get_arguments(JsonObject * req)
+{
+ return json_object_get_object_member(req, PARAM_ARGUMENTS);
+}