json_compat.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /*
  2. * Discord plugin for libpurple
  3. * Copyright (C) 2016-2017 Eion Robb
  4. * Copyright (C) 2017 Alyssa Rosenzweig
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  18. */
  19. #include <json-glib/json-glib.h>
  20. // Suppress overzealous json-glib 'critical errors'
  21. #define json_object_has_member(JSON_OBJECT, MEMBER) \
  22. (JSON_OBJECT ? json_object_has_member(JSON_OBJECT, MEMBER) : FALSE)
  23. #define json_object_get_int_member(JSON_OBJECT, MEMBER) \
  24. (json_object_has_member(JSON_OBJECT, MEMBER) ? json_object_get_int_member(JSON_OBJECT, MEMBER) : 0)
  25. #define json_object_get_string_member(JSON_OBJECT, MEMBER) \
  26. (json_object_has_member(JSON_OBJECT, MEMBER) ? json_object_get_string_member(JSON_OBJECT, MEMBER) : NULL)
  27. #define json_object_get_array_member(JSON_OBJECT, MEMBER) \
  28. (json_object_has_member(JSON_OBJECT, MEMBER) ? json_object_get_array_member(JSON_OBJECT, MEMBER) : NULL)
  29. #define json_object_get_object_member(JSON_OBJECT, MEMBER) \
  30. (json_object_has_member(JSON_OBJECT, MEMBER) ? json_object_get_object_member(JSON_OBJECT, MEMBER) : NULL)
  31. #define json_object_get_boolean_member(JSON_OBJECT, MEMBER) \
  32. (json_object_has_member(JSON_OBJECT, MEMBER) ? json_object_get_boolean_member(JSON_OBJECT, MEMBER) : FALSE)
  33. #define json_array_get_length(JSON_ARRAY) \
  34. (JSON_ARRAY ? json_array_get_length(JSON_ARRAY) : 0)
  35. static gchar *
  36. json_object_to_string(JsonObject *obj)
  37. {
  38. JsonNode *node;
  39. gchar *str;
  40. JsonGenerator *generator;
  41. node = json_node_new(JSON_NODE_OBJECT);
  42. json_node_set_object(node, obj);
  43. // a json string ...
  44. generator = json_generator_new();
  45. json_generator_set_root(generator, node);
  46. str = json_generator_to_data(generator, NULL);
  47. g_object_unref(generator);
  48. json_node_free(node);
  49. return str;
  50. }
  51. //just in case we optimize...
  52. #define g_hash_table_insert_int64(a, b, c) g_hash_table_insert((a), &(b), (c))
  53. #define g_hash_table_replace_int64(a, b, c) g_hash_table_replace((a), &(b), (c))
  54. #define g_hash_table_steal_int64(a, b) g_hash_table_steal((a), &(b))
  55. #define g_hash_table_lookup_int64(a, b) g_hash_table_lookup((a), &(b))
  56. #define g_hash_table_contains_int64(a, b) g_hash_table_contains((a), &(b))
  57. #define g_hash_table_lookup_extended_int64(a, b, c, d) g_hash_table_lookup_extended((a), &(b), (c), (d))