Imported Upstream version 1.2.4
[platform/upstream/json-glib.git] / json-glib / json-gobject.h
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
24 #ifndef __JSON_GOBJECT_H__
25 #define __JSON_GOBJECT_H__
26
27 #include <json-glib/json-types.h>
28
29 G_BEGIN_DECLS
30
31 #define JSON_TYPE_SERIALIZABLE                  (json_serializable_get_type ())
32 #define JSON_SERIALIZABLE(obj)                  (G_TYPE_CHECK_INSTANCE_CAST ((obj), JSON_TYPE_SERIALIZABLE, JsonSerializable))
33 #define JSON_IS_SERIALIZABLE(obj)               (G_TYPE_CHECK_INSTANCE_TYPE ((obj), JSON_TYPE_SERIALIZABLE))
34 #define JSON_SERIALIZABLE_GET_IFACE(obj)        (G_TYPE_INSTANCE_GET_INTERFACE ((obj), JSON_TYPE_SERIALIZABLE, JsonSerializableIface))
35
36 typedef struct _JsonSerializable        JsonSerializable; /* dummy */
37 typedef struct _JsonSerializableIface   JsonSerializableIface;
38
39 /**
40  * JsonSerializableIface:
41  * @serialize_property: virtual function for serializing a #GObject property
42  *   into a #JsonNode
43  * @deserialize_property: virtual function for deserializing a #JsonNode
44  *   into a #GObject property
45  * @find_property: virtual function for finding a property definition using
46  *   its name
47  * @list_properties: virtual function for listing the installed property
48  *   definitions
49  * @set_property: virtual function for setting a property
50  * @get_property: virtual function for getting a property
51  *
52  * Interface that allows serializing and deserializing #GObject instances
53  * with properties storing complex data types. The json_serialize_gobject()
54  * function will check if the passed #GObject implements this interface,
55  * so it can also be used to override the default property serialization
56  * sequence.
57  */
58 struct _JsonSerializableIface
59 {
60   /*< private >*/
61   GTypeInterface g_iface;
62
63   /*< public >*/
64   JsonNode *(* serialize_property)   (JsonSerializable *serializable,
65                                       const gchar      *property_name,
66                                       const GValue     *value,
67                                       GParamSpec       *pspec);
68   gboolean  (* deserialize_property) (JsonSerializable *serializable,
69                                       const gchar      *property_name,
70                                       GValue           *value,
71                                       GParamSpec       *pspec,
72                                       JsonNode         *property_node);
73
74   GParamSpec * (* find_property)       (JsonSerializable *serializable,
75                                         const char       *name);
76   GParamSpec **(* list_properties)     (JsonSerializable *serializable,
77                                         guint            *n_pspecs);
78   void         (* set_property)        (JsonSerializable *serializable,
79                                         GParamSpec       *pspec,
80                                         const GValue     *value);
81   void         (* get_property)        (JsonSerializable *serializable,
82                                         GParamSpec       *pspec,
83                                         GValue           *value);
84 };
85
86 JSON_AVAILABLE_IN_1_0
87 GType json_serializable_get_type (void) G_GNUC_CONST;
88
89 JSON_AVAILABLE_IN_1_0
90 JsonNode *json_serializable_serialize_property           (JsonSerializable *serializable,
91                                                           const gchar      *property_name,
92                                                           const GValue     *value,
93                                                           GParamSpec       *pspec);
94 JSON_AVAILABLE_IN_1_0
95 gboolean  json_serializable_deserialize_property         (JsonSerializable *serializable,
96                                                           const gchar      *property_name,
97                                                           GValue           *value,
98                                                           GParamSpec       *pspec,
99                                                           JsonNode         *property_node);
100
101 JSON_AVAILABLE_IN_1_0
102 GParamSpec *    json_serializable_find_property         (JsonSerializable *serializable,
103                                                          const char       *name);
104 JSON_AVAILABLE_IN_1_0
105 GParamSpec **   json_serializable_list_properties       (JsonSerializable *serializable,
106                                                          guint            *n_pspecs);
107 JSON_AVAILABLE_IN_1_0
108 void            json_serializable_set_property          (JsonSerializable *serializable,
109                                                          GParamSpec       *pspec,
110                                                          const GValue     *value);
111 JSON_AVAILABLE_IN_1_0
112 void            json_serializable_get_property          (JsonSerializable *serializable,
113                                                          GParamSpec       *pspec,
114                                                          GValue           *value);
115
116 JSON_AVAILABLE_IN_1_0
117 JsonNode *json_serializable_default_serialize_property   (JsonSerializable *serializable,
118                                                           const gchar      *property_name,
119                                                           const GValue     *value,
120                                                           GParamSpec       *pspec);
121 JSON_AVAILABLE_IN_1_0
122 gboolean  json_serializable_default_deserialize_property (JsonSerializable *serializable,
123                                                           const gchar      *property_name,
124                                                           GValue           *value,
125                                                           GParamSpec       *pspec,
126                                                           JsonNode         *property_node);
127
128 /**
129  * JsonBoxedSerializeFunc:
130  * @boxed: a #GBoxed
131  *
132  * Serializes the passed #GBoxed and stores it inside a #JsonNode
133  *
134  * Return value: the newly created #JsonNode
135  *
136  * Since: 0.10
137  */
138 typedef JsonNode *(* JsonBoxedSerializeFunc) (gconstpointer boxed);
139
140 /**
141  * JsonBoxedDeserializeFunc:
142  * @node: a #JsonNode
143  *
144  * Deserializes the contents of the passed #JsonNode into a #GBoxed
145  *
146  * Return value: the newly created boxed type
147  *
148  * Since: 0.10
149  */
150 typedef gpointer (* JsonBoxedDeserializeFunc) (JsonNode *node);
151
152 JSON_AVAILABLE_IN_1_0
153 void      json_boxed_register_serialize_func   (GType                    gboxed_type,
154                                                 JsonNodeType             node_type,
155                                                 JsonBoxedSerializeFunc   serialize_func);
156 JSON_AVAILABLE_IN_1_0
157 void      json_boxed_register_deserialize_func (GType                    gboxed_type,
158                                                 JsonNodeType             node_type,
159                                                 JsonBoxedDeserializeFunc deserialize_func);
160 JSON_AVAILABLE_IN_1_0
161 gboolean  json_boxed_can_serialize             (GType                    gboxed_type,
162                                                 JsonNodeType            *node_type);
163 JSON_AVAILABLE_IN_1_0
164 gboolean  json_boxed_can_deserialize           (GType                    gboxed_type,
165                                                 JsonNodeType             node_type);
166 JSON_AVAILABLE_IN_1_0
167 JsonNode *json_boxed_serialize                 (GType                    gboxed_type,
168                                                 gconstpointer            boxed);
169 JSON_AVAILABLE_IN_1_0
170 gpointer  json_boxed_deserialize               (GType                    gboxed_type,
171                                                 JsonNode                *node);
172
173 JSON_AVAILABLE_IN_1_0
174 JsonNode *json_gobject_serialize               (GObject                 *gobject);
175 JSON_AVAILABLE_IN_1_0
176 GObject * json_gobject_deserialize             (GType                    gtype,
177                                                 JsonNode                *node);
178
179 JSON_AVAILABLE_IN_1_0
180 GObject * json_gobject_from_data               (GType                    gtype,
181                                                 const gchar             *data,
182                                                 gssize                   length,
183                                                 GError                 **error);
184 JSON_AVAILABLE_IN_1_0
185 gchar *   json_gobject_to_data                 (GObject                 *gobject,
186                                                 gsize                   *length);
187
188 JSON_DEPRECATED_IN_1_0_FOR(json_gobject_from_data)
189 GObject * json_construct_gobject               (GType                    gtype,
190                                                 const gchar             *data,
191                                                 gsize                    length,
192                                                 GError                 **error);
193 JSON_DEPRECATED_IN_1_0_FOR(json_gobject_to_data)
194 gchar *   json_serialize_gobject               (GObject                 *gobject,
195                                                 gsize                   *length) G_GNUC_MALLOC;
196
197 #ifdef G_DEFINE_AUTOPTR_CLEANUP_FUNC
198 G_DEFINE_AUTOPTR_CLEANUP_FUNC (JsonSerializable, g_object_unref)
199 #endif
200
201 G_END_DECLS
202
203 #endif /* __JSON_GOBJECT_H__ */