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