eion/purple-hangouts
Merge
draft
2019-05-24, Eion Robb
* Hangouts Plugin for libpurple/Pidgin * Copyright (c) 2015-2016 Eion Robb, Mike Ruprecht * 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 3 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, see <http://www.gnu.org/licenses/>. #include "hangouts_json.h" json_encode(JsonNode *node, gsize *len) JsonGenerator *generator = json_generator_new(); json_generator_set_root(generator, node); data = json_generator_to_data(generator, len); g_object_unref(generator); json_pretty_encode(JsonNode *node, gsize *len) JsonGenerator *generator = json_generator_new(); g_object_set(generator, "pretty", TRUE, NULL); g_object_set(generator, "indent-char", '\t', NULL); g_object_set(generator, "indent", 1, NULL); json_generator_set_root(generator, node); data = json_generator_to_data(generator, len); g_object_unref(generator); json_encode_array(JsonArray *array, gsize *len) JsonNode *node = json_node_new(JSON_NODE_ARRAY); json_node_set_array(node, array); json = json_encode(node, len); json_encode_object(JsonObject *object, gsize *len) JsonNode *node = json_node_new(JSON_NODE_OBJECT); json_node_set_object(node, object); json = json_encode(node, len); json_decode(const gchar *data, gssize len) JsonParser *parser = json_parser_new(); if (!data || !json_parser_load_from_data(parser, data, len, NULL)) purple_debug_error("hangouts", "Error parsing JSON: %s\n", data); root = json_parser_get_root(parser); root = json_node_copy(root); json_decode_array(const gchar *data, gssize len) JsonNode *root = json_decode(data, len); g_return_val_if_fail(root, NULL); if (!JSON_NODE_HOLDS_ARRAY(root)) { // That ain't my belly button! ret = json_node_dup_array(root); json_decode_object(const gchar *data, gssize len) JsonNode *root = json_decode(data, len); g_return_val_if_fail(root, NULL); if (!JSON_NODE_HOLDS_OBJECT(root)) { // That ain't my thumb, neither! ret = json_node_dup_object(root); hangouts_json_path_query(JsonNode *root, const gchar *expr, GError **error) if (g_str_equal(expr, "$")) { node = json_path_query(expr, root, error); result = json_node_get_array(node); if (json_array_get_length(result) == 0) ret = json_array_dup_element(result, 0); hangouts_json_path_query_string(JsonNode *root, const gchar *expr, GError **error) rslt = hangouts_json_path_query(root, expr, error); ret = g_strdup(json_node_get_string(rslt)); hangouts_json_path_query_int(JsonNode *root, const gchar *expr, GError **error) rslt = hangouts_json_path_query(root, expr, error); ret = json_node_get_int(rslt); hangouts_json_tidy_blank_arrays(const gchar *json) static GRegex *tidy_regex = NULL; if (tidy_regex == NULL) { tidy_regex = g_regex_new("\"(\\\\\"|.)*?\"(*SKIP)(*FAIL)|\\[\\](*SKIP)(*FAIL)|(?<=,|\\[)\\s*(?=,|\\])", G_REGEX_OPTIMIZE, 0, NULL); return g_regex_replace_literal(tidy_regex, json, -1, 0, "null", 0, NULL);