summaryrefslogtreecommitdiff
path: root/src/json.c
blob: fdce0b6b1ac7655d129186b2a88feda930bc7cf3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*
 * transmission-remote-gtk - A GTK RPC client to Transmission
 * Copyright (C) 2011-2013  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 "config.h"
#include "protocol-constants.h"
#include "requests.h"
#include "json.h"

/* JSON helper functions */

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(trg_response * response, GError ** error)
{
    JsonParser *parser;
    JsonNode *root;
    JsonObject *ret = NULL;

    parser = json_parser_new();
    json_parser_load_from_data(parser, response->raw, response->size,
                               error);
    if (*error == NULL) {
        root = json_parser_get_root(parser);
#ifdef DEBUG
        if (g_getenv("TRG_SHOW_INCOMING") != NULL) {
            g_debug("<=(INcoming)<=: %s", response->raw);
        } else if (g_getenv("TRG_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_debug("<=(incoming)<=:\n%s\n", pgdata);
            g_free(pgdata);

            g_object_unref(pg);
        }
#endif
        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);
}

gdouble json_double_to_progress(JsonNode * n)
{
    return json_node_really_get_double(n) * 100.0;
}

gdouble json_node_really_get_double(JsonNode * node)
{
    GValue a = { 0 };

    json_node_get_value(node, &a);
    switch (G_VALUE_TYPE(&a)) {
    case G_TYPE_INT64:
        return (gdouble) g_value_get_int64(&a);
    case G_TYPE_DOUBLE:
        return g_value_get_double(&a);
    default:
        return 0.0;
    }
}