json-gobject.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /* json-gobject.h - JSON GObject integration
  2. *
  3. * This file is part of JSON-GLib
  4. * Copyright (C) 2007 OpenedHand Ltd.
  5. * Copyright (C) 2009 Intel Corp.
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. * Author:
  21. * Emmanuele Bassi <ebassi@linux.intel.com>
  22. */
  23. #ifndef __JSON_GOBJECT_H__
  24. #define __JSON_GOBJECT_H__
  25. #include <json-glib/json-types.h>
  26. G_BEGIN_DECLS
  27. #define JSON_TYPE_SERIALIZABLE (json_serializable_get_type ())
  28. #define JSON_SERIALIZABLE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), JSON_TYPE_SERIALIZABLE, JsonSerializable))
  29. #define JSON_IS_SERIALIZABLE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), JSON_TYPE_SERIALIZABLE))
  30. #define JSON_SERIALIZABLE_GET_IFACE(obj) (G_TYPE_INSTANCE_GET_INTERFACE ((obj), JSON_TYPE_SERIALIZABLE, JsonSerializableIface))
  31. typedef struct _JsonSerializable JsonSerializable; /* dummy */
  32. typedef struct _JsonSerializableIface JsonSerializableIface;
  33. /**
  34. * JsonSerializableIface:
  35. * @serialize_property: virtual function for serializing a #GObject property
  36. * into a #JsonNode
  37. * @deserialize_property: virtual function for deserializing a #JsonNode
  38. * into a #GObject property
  39. * @find_property: virtual function for finding a property definition using
  40. * its name
  41. * @list_properties: virtual function for listing the installed property
  42. * definitions
  43. * @set_property: virtual function for setting a property
  44. * @get_property: virtual function for getting a property
  45. *
  46. * Interface that allows serializing and deserializing #GObject instances
  47. * with properties storing complex data types. The json_serialize_gobject()
  48. * function will check if the passed #GObject implements this interface,
  49. * so it can also be used to override the default property serialization
  50. * sequence.
  51. */
  52. struct _JsonSerializableIface
  53. {
  54. /*< private >*/
  55. GTypeInterface g_iface;
  56. /*< public >*/
  57. JsonNode *(* serialize_property) (JsonSerializable *serializable,
  58. const gchar *property_name,
  59. const GValue *value,
  60. GParamSpec *pspec);
  61. gboolean (* deserialize_property) (JsonSerializable *serializable,
  62. const gchar *property_name,
  63. GValue *value,
  64. GParamSpec *pspec,
  65. JsonNode *property_node);
  66. GParamSpec * (* find_property) (JsonSerializable *serializable,
  67. const char *name);
  68. GParamSpec **(* list_properties) (JsonSerializable *serializable,
  69. guint *n_pspecs);
  70. void (* set_property) (JsonSerializable *serializable,
  71. GParamSpec *pspec,
  72. const GValue *value);
  73. void (* get_property) (JsonSerializable *serializable,
  74. GParamSpec *pspec,
  75. GValue *value);
  76. };
  77. JSON_AVAILABLE_IN_1_0
  78. GType json_serializable_get_type (void) G_GNUC_CONST;
  79. JSON_AVAILABLE_IN_1_0
  80. JsonNode *json_serializable_serialize_property (JsonSerializable *serializable,
  81. const gchar *property_name,
  82. const GValue *value,
  83. GParamSpec *pspec);
  84. JSON_AVAILABLE_IN_1_0
  85. gboolean json_serializable_deserialize_property (JsonSerializable *serializable,
  86. const gchar *property_name,
  87. GValue *value,
  88. GParamSpec *pspec,
  89. JsonNode *property_node);
  90. JSON_AVAILABLE_IN_1_0
  91. GParamSpec * json_serializable_find_property (JsonSerializable *serializable,
  92. const char *name);
  93. JSON_AVAILABLE_IN_1_0
  94. GParamSpec ** json_serializable_list_properties (JsonSerializable *serializable,
  95. guint *n_pspecs);
  96. JSON_AVAILABLE_IN_1_0
  97. void json_serializable_set_property (JsonSerializable *serializable,
  98. GParamSpec *pspec,
  99. const GValue *value);
  100. JSON_AVAILABLE_IN_1_0
  101. void json_serializable_get_property (JsonSerializable *serializable,
  102. GParamSpec *pspec,
  103. GValue *value);
  104. JSON_AVAILABLE_IN_1_0
  105. JsonNode *json_serializable_default_serialize_property (JsonSerializable *serializable,
  106. const gchar *property_name,
  107. const GValue *value,
  108. GParamSpec *pspec);
  109. JSON_AVAILABLE_IN_1_0
  110. gboolean json_serializable_default_deserialize_property (JsonSerializable *serializable,
  111. const gchar *property_name,
  112. GValue *value,
  113. GParamSpec *pspec,
  114. JsonNode *property_node);
  115. /**
  116. * JsonBoxedSerializeFunc:
  117. * @boxed: a #GBoxed
  118. *
  119. * Serializes the passed #GBoxed and stores it inside a #JsonNode
  120. *
  121. * Return value: the newly created #JsonNode
  122. *
  123. * Since: 0.10
  124. */
  125. typedef JsonNode *(* JsonBoxedSerializeFunc) (gconstpointer boxed);
  126. /**
  127. * JsonBoxedDeserializeFunc:
  128. * @node: a #JsonNode
  129. *
  130. * Deserializes the contents of the passed #JsonNode into a #GBoxed
  131. *
  132. * Return value: the newly created boxed type
  133. *
  134. * Since: 0.10
  135. */
  136. typedef gpointer (* JsonBoxedDeserializeFunc) (JsonNode *node);
  137. JSON_AVAILABLE_IN_1_0
  138. void json_boxed_register_serialize_func (GType gboxed_type,
  139. JsonNodeType node_type,
  140. JsonBoxedSerializeFunc serialize_func);
  141. JSON_AVAILABLE_IN_1_0
  142. void json_boxed_register_deserialize_func (GType gboxed_type,
  143. JsonNodeType node_type,
  144. JsonBoxedDeserializeFunc deserialize_func);
  145. JSON_AVAILABLE_IN_1_0
  146. gboolean json_boxed_can_serialize (GType gboxed_type,
  147. JsonNodeType *node_type);
  148. JSON_AVAILABLE_IN_1_0
  149. gboolean json_boxed_can_deserialize (GType gboxed_type,
  150. JsonNodeType node_type);
  151. JSON_AVAILABLE_IN_1_0
  152. JsonNode *json_boxed_serialize (GType gboxed_type,
  153. gconstpointer boxed);
  154. JSON_AVAILABLE_IN_1_0
  155. gpointer json_boxed_deserialize (GType gboxed_type,
  156. JsonNode *node);
  157. JSON_AVAILABLE_IN_1_0
  158. JsonNode *json_gobject_serialize (GObject *gobject);
  159. JSON_AVAILABLE_IN_1_0
  160. GObject * json_gobject_deserialize (GType gtype,
  161. JsonNode *node);
  162. JSON_AVAILABLE_IN_1_0
  163. GObject * json_gobject_from_data (GType gtype,
  164. const gchar *data,
  165. gssize length,
  166. GError **error);
  167. JSON_AVAILABLE_IN_1_0
  168. gchar * json_gobject_to_data (GObject *gobject,
  169. gsize *length);
  170. JSON_DEPRECATED_IN_1_0_FOR(json_gobject_from_data)
  171. GObject * json_construct_gobject (GType gtype,
  172. const gchar *data,
  173. gsize length,
  174. GError **error);
  175. JSON_DEPRECATED_IN_1_0_FOR(json_gobject_to_data)
  176. gchar * json_serialize_gobject (GObject *gobject,
  177. gsize *length) G_GNUC_MALLOC;
  178. #ifdef G_DEFINE_AUTOPTR_CLEANUP_FUNC
  179. G_DEFINE_AUTOPTR_CLEANUP_FUNC (JsonSerializable, g_object_unref)
  180. #endif
  181. G_END_DECLS
  182. #endif /* __JSON_GOBJECT_H__ */