Fix uncheck return of vconf_get_bool
[platform/core/messaging/msg-service.git] / utils / MsgJsonParser.cpp
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd. All rights reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15 */
16
17 #include <unistd.h>
18 #include <stdlib.h>
19 #include <stdio.h>
20 #include <string.h>
21 #include <glib.h>
22 #include <glib-object.h>
23 #include <json-glib/json-glib.h>
24 #include <json-glib/json-gobject.h>
25 #include "MsgJsonParser.h"
26
27 static char root_key[5] = "root";
28
29 msg_json_gen_object* msg_json_gen_new_obj(msg_json_gen_type type)
30 {
31         void *json_value = NULL;
32         switch(type) {
33         case MSG_JSON_GEN_OBJECT:
34                 json_value = (void *)json_object_new();
35                 break;
36         case MSG_JSON_GEN_ARRAY:
37                 json_value = (void *)json_array_new();
38                 break;
39         default:
40                 break;
41         }
42         if (json_value != NULL) {
43                 msg_json_gen_object *new_obj = NULL;
44                 new_obj = (msg_json_gen_object *)g_try_malloc0(sizeof(msg_json_gen_object));
45
46                 if (new_obj == NULL) {
47                         if (type == MSG_JSON_GEN_OBJECT)
48                                 json_object_unref((JsonObject *)json_value);
49                         else if (type == MSG_JSON_GEN_ARRAY)
50                                 json_array_unref((JsonArray *)json_value);
51
52                         return NULL;
53                 }
54
55                 new_obj->type = type;
56                 new_obj->value = json_value;
57                 return new_obj;
58         }
59         return NULL;
60 }
61
62 void msg_json_gen_free_obj(msg_json_gen_object *obj)
63 {
64         if (obj == NULL) {
65                 return;
66         }
67         if (obj->value != NULL) {
68                 if (obj->type == MSG_JSON_GEN_OBJECT)
69                         json_object_unref((JsonObject *)obj->value);
70                 else if (obj->type == MSG_JSON_GEN_ARRAY)
71                         json_array_unref((JsonArray *)obj->value);
72
73                 obj->value = NULL;
74         }
75         g_free(obj);
76         obj = NULL;
77 }
78
79 void msg_json_gen_set_child(msg_json_gen_object *parent, const char *key, msg_json_gen_object *child) {
80         if (parent == NULL || child == NULL) {
81                 return;
82         }
83
84         switch(parent->type) {
85         case MSG_JSON_GEN_OBJECT:
86                 if (key == NULL) {
87                         return;
88                 }
89                 switch(child->type) {
90                 case MSG_JSON_GEN_OBJECT:
91                         json_object_set_object_member((JsonObject *)parent->value, key, (JsonObject *)child->value);
92                         break;
93                 case MSG_JSON_GEN_ARRAY:
94                         json_object_set_array_member((JsonObject *)parent->value, key, (JsonArray *)child->value);
95                         break;
96                 }
97                 break;
98         case MSG_JSON_GEN_ARRAY:
99                 switch(child->type) {
100                 case MSG_JSON_GEN_OBJECT:
101                         json_array_add_object_element((JsonArray *)parent->value, (JsonObject *)child->value);
102                         break;
103                 case MSG_JSON_GEN_ARRAY:
104                         json_array_add_array_element((JsonArray *)parent->value, (JsonArray *)child->value);
105                         break;
106                 }
107                 break;
108         }
109 }
110
111 void msg_json_gen_set_value(msg_json_gen_object *parent, const char *key, long long int value) {
112         if (parent == NULL) {
113                 return;
114         }
115
116         switch(parent->type) {
117         case MSG_JSON_GEN_OBJECT:
118                 if (key == NULL) {
119                         return;
120                 }
121                 json_object_set_int_member((JsonObject *)parent->value, key, value);
122                 break;
123         case MSG_JSON_GEN_ARRAY:
124                 json_array_add_int_element((JsonArray *)parent->value, value);
125                 break;
126         }
127 }
128
129 void msg_json_gen_set_value(msg_json_gen_object *parent, const char *key, const char *value) {
130         if (parent == NULL) {
131                 return;
132         }
133
134         switch(parent->type) {
135         case MSG_JSON_GEN_OBJECT:
136                 if (key == NULL) {
137                         return;
138                 }
139                 json_object_set_string_member((JsonObject *)parent->value, key, value);
140                 break;
141         case MSG_JSON_GEN_ARRAY:
142                 json_array_add_string_element((JsonArray *)parent->value, value);
143                 break;
144         }
145 }
146
147 char* msg_json_gen_make_json_msg(msg_json_gen_object *root_obj, unsigned long *len) {
148         if (root_obj == NULL) {
149                 return NULL;
150         }
151         JsonGenerator *generator = json_generator_new();
152         JsonNode *root = json_node_new(JSON_NODE_OBJECT);
153
154         json_node_take_object(root, (JsonObject *)root_obj->value);
155         json_generator_set_root(generator, root);
156
157         char *json_msg = json_generator_to_data(generator, (gsize *)len);
158         g_object_unref(generator);
159         json_node_free(root);
160
161         return json_msg;
162 }
163
164 int msg_json_parser_get_value(msg_json_parser_object *json_obj)
165 {
166         GValue value = { 0 };
167
168         /*Input params validation */
169         if (json_obj == NULL) {
170                 MSG_DEBUG("Invalid Input Parameters");
171                 return -1;
172         }
173
174         json_obj->float_value = 0;
175         json_obj->number_value = 0;
176
177         json_node_get_value((JsonNode *) json_obj->value, &value);
178
179         switch (json_node_get_value_type((JsonNode *) json_obj->value)) {
180         case G_TYPE_STRING:
181                 json_obj->type = MSG_JSON_PARSER_STRING;
182                 json_obj->value = (void *)g_value_get_string(&value);
183                 break;
184         case G_TYPE_INT:
185                 json_obj->type = MSG_JSON_PARSER_INT;
186                 json_obj->value = NULL;
187                 json_obj->number_value = g_value_get_int(&value);
188                 break;
189         case G_TYPE_UINT:
190                 json_obj->type = MSG_JSON_PARSER_UINT;
191                 json_obj->value = NULL;
192                 json_obj->number_value = g_value_get_uint(&value);
193                 break;
194         case G_TYPE_BOOLEAN:
195                 json_obj->type = MSG_JSON_PARSER_BOOLEAN;
196                 json_obj->value = NULL;
197                 json_obj->number_value = g_value_get_boolean(&value);
198                 break;
199         case G_TYPE_INT64:
200                 json_obj->type = MSG_JSON_PARSER_INT;
201                 json_obj->value = NULL;
202                 json_obj->number_value = g_value_get_int64(&value);
203                 break;
204         case G_TYPE_UINT64:
205                 json_obj->type = MSG_JSON_PARSER_UINT;
206                 json_obj->value = NULL;
207                 json_obj->number_value = g_value_get_uint64(&value);
208                 break;
209         case G_TYPE_DOUBLE:
210                 json_obj->type = MSG_JSON_PARSER_REAL;
211                 json_obj->value = NULL;
212                 json_obj->float_value = (double)g_value_get_double(&value);
213                 break;
214         default:
215                 MSG_DEBUG(" Entering node default case");
216                 break;
217         }
218
219         return 1;
220 }
221
222 int msg_json_parser_object_get_value(msg_json_parser_object *json_obj)
223 {
224         GValue value = { 0 };
225         JsonNode *node = NULL;
226
227         /*Input params validation */
228         if (json_obj == NULL) {
229                 MSG_DEBUG("Invalid Input Parameters");
230                 return -1;
231         }
232         node = json_object_get_member((JsonObject *)json_obj->value, json_obj->key);
233
234         json_obj->float_value = 0;
235         json_obj->number_value = 0;
236
237         json_node_get_value((JsonNode *) node, &value);
238
239         switch (json_node_get_value_type((JsonNode *) node)) {
240         case G_TYPE_STRING:
241                 json_obj->type = MSG_JSON_PARSER_STRING;
242                 json_obj->value = (void *)g_value_get_string(&value);
243                 break;
244         case G_TYPE_INT:
245                 json_obj->type = MSG_JSON_PARSER_INT;
246                 json_obj->value = NULL;
247                 json_obj->number_value = g_value_get_int(&value);
248                 break;
249         case G_TYPE_UINT:
250                 json_obj->type = MSG_JSON_PARSER_UINT;
251                 json_obj->value = NULL;
252                 json_obj->number_value = g_value_get_uint(&value);
253                 break;
254         case G_TYPE_BOOLEAN:
255                 json_obj->type = MSG_JSON_PARSER_BOOLEAN;
256                 json_obj->value = NULL;
257                 json_obj->number_value = g_value_get_boolean(&value);
258                 break;
259         case G_TYPE_INT64:
260                 json_obj->type = MSG_JSON_PARSER_INT;
261                 json_obj->value = NULL;
262                 json_obj->number_value = g_value_get_int64(&value);
263                 break;
264         case G_TYPE_UINT64:
265                 json_obj->type = MSG_JSON_PARSER_UINT;
266                 json_obj->value = NULL;
267                 json_obj->number_value = g_value_get_uint64(&value);
268                 break;
269         case G_TYPE_DOUBLE:
270                 json_obj->type = MSG_JSON_PARSER_REAL;
271                 json_obj->value = NULL;
272                 json_obj->float_value = (double)g_value_get_double(&value);
273                 break;
274         default:
275                 MSG_DEBUG(" Entering node default case");
276                 break;
277         }
278
279         return 1;
280 }
281
282 msg_json_parser_handle msg_json_parser_handle_create(void)
283 {
284         JsonParser *jsonParser = NULL;
285
286         jsonParser = json_parser_new();
287
288         return(msg_json_parser_handle)jsonParser;
289 }
290
291 void msg_json_parser_handle_destory(msg_json_parser_handle *handle)
292 {
293         if (handle == NULL)
294                 return;
295
296         g_object_unref(*handle);
297         *handle = NULL;
298 }
299
300 void msg_json_parser_parse_buffer(msg_json_parser_handle handle, const char* value, int value_len, msg_json_parser_object *json_obj)
301 {
302         gboolean gRet = TRUE;
303         JsonNode *root = NULL;
304         GError *error = NULL;
305         JsonParser *jsonParser = (JsonParser *)handle;
306         JsonNodeType parentType = JSON_NODE_NULL;
307
308         /*Input params validation*/
309         if (value == NULL || value_len == 0) {
310                 MSG_DEBUG("Invalid Input Parameters");
311                 return ;
312         }
313
314         if (jsonParser != NULL) {
315                 /** Loads a JSON stream from a buffer and parses it */
316                 gRet = json_parser_load_from_data(jsonParser, value, value_len, &error);
317                 if (gRet != TRUE) {
318                         g_error_free(error);
319                 } else {
320                         /** Fetch the root node */
321                         root = json_parser_get_root(jsonParser);
322
323                         if (root != NULL) {
324                                 json_obj->key = root_key;
325                                 json_obj->value = root;
326                                 parentType = json_node_get_node_type(root);
327
328                                 if (parentType == JSON_NODE_VALUE) {
329                                         gRet = msg_json_parser_get_value(json_obj);
330                                 } else if (parentType == JSON_NODE_OBJECT) {
331                                         json_obj->type = MSG_JSON_PARSER_OBJECT;
332                                 } else if (parentType == JSON_NODE_ARRAY) {
333                                         json_obj->type = MSG_JSON_PARSER_ARRAY;
334                                 } else {
335                                         json_obj->type = MSG_JSON_PARSER_NULL;
336                                 }
337                         } else {
338                                 json_obj->key = NULL;
339                                 json_obj->value = NULL;
340                         }
341                 }
342         } else {
343         }
344
345         return ;
346 }
347
348 int msg_json_parser_get_next_child(const msg_json_parser_object *parent, msg_json_parser_object *child, int index)
349 {
350         int lReturn = 1;
351         JsonNodeType jNodeParentType = JSON_NODE_NULL;
352         JsonNodeType jNodeChildType = JSON_NODE_NULL;
353         JsonArray *tempArray = NULL;
354         JsonObject *tempObj = NULL;
355         GList *members = NULL;
356
357         /*Input params validation */
358         if (parent == NULL || parent->value == NULL || child == NULL || index < 0) {
359                 MSG_DEBUG("Invalid Input Parameters");
360                 return 0;
361         }
362
363         /** Get the JSON Parent Node Type */
364         jNodeParentType = json_node_get_node_type((JsonNode *) parent->value);
365
366         switch (jNodeParentType) {
367         case JSON_NODE_OBJECT: {
368                         /** Get the JSON object from JSON Parent Node */
369                         tempObj = json_node_get_object((JsonNode *) parent->value);
370
371                         /** Get the list of keys from the object node */
372                         members = json_object_get_members(tempObj);
373
374                         /** Get the key by index from the list */
375                         child->key = (char *)g_list_nth_data(members, index);
376
377                         g_list_free(members);
378                         if (child->key == NULL) {
379                                 return 0;
380                         }
381
382                         /** Get the JSONNode by key from the list */
383                         child->value = json_object_get_member(tempObj, child->key);
384
385                         /** Identify the node type of the JSOSNNode */
386                         jNodeChildType = json_node_get_node_type((JsonNode *)child->value);
387
388                         switch (jNodeChildType) {
389                         case JSON_NODE_OBJECT:
390                                 child->type = MSG_JSON_PARSER_OBJECT;
391                                 break;
392                         case JSON_NODE_ARRAY:
393                                 child->type = MSG_JSON_PARSER_ARRAY;
394                                 break;
395                         case JSON_NODE_VALUE:
396                                 lReturn = msg_json_parser_get_value(child);
397                                 break;
398                         case JSON_NODE_NULL:
399                                 break;
400                         default:
401                                 lReturn = 0;
402                                 break;
403                         }
404                 }
405                 break;
406         case JSON_NODE_ARRAY: {
407                         /** Get the JSON array from the JSON node */
408                         tempArray = json_node_get_array((JsonNode *) parent->value);
409
410                         child->key = NULL;
411
412                         if ((guint) index >= json_array_get_length(tempArray)) {
413                                 return 0;
414                         }
415
416                         /** Get the JSONNode from the list of values */
417                         child->value = (void *)json_array_get_element(tempArray, index);
418                         if (child->value == NULL) {
419                                 return 0;
420                         }
421                         /* Get the child type */
422                         jNodeChildType = json_node_get_node_type((JsonNode *) child->value);
423
424                         switch (jNodeChildType) {
425                         case JSON_NODE_OBJECT:
426                                 child->type = MSG_JSON_PARSER_OBJECT;
427                                 break;
428                         case JSON_NODE_ARRAY:
429                                 child->type = MSG_JSON_PARSER_ARRAY;
430                                 break;
431                         case JSON_NODE_VALUE:
432                                 lReturn = msg_json_parser_get_value(child);
433                                 break;
434                         case JSON_NODE_NULL:
435                                 break;
436                         default:
437                                 lReturn = 0;
438                                 break;
439                         }
440                 }
441                 break;
442         case JSON_NODE_VALUE:
443         default: {
444                         child->key = NULL;
445                         child->value = NULL;
446                         lReturn = 0;
447                 }
448                 break;
449         }
450
451         return lReturn;
452 }
453
454
455 /**
456  * @fn msg_json_parser_get_child_by_name
457  * This function is used for getting the child node by it's name for
458         input node.
459  * @param [IN] \n
460  * parent: msg_json_parser_object structure pointer whose child node
461         is to be retrieved \n
462  * name: const char pointer containing key of the required child \n
463  * @param [OUT] \n
464  * child: msg_json_parser_object structure pointer. Should be allocated
465         and freed after it's use by caller. It will be filled with
466         corresponding data \n
467  * @return      Return type is int. Should be typecasted to
468         msg_json_parser_parse_status to check the return status. If
469         MSG_JSON_PARSER_PARSE_SUCCESS, then only child contains
470         valid data, else some error occured. \n
471  * @remark This API is synchronous.
472  */
473 int msg_json_parser_get_child_by_name(const msg_json_parser_object *parent,
474                                         msg_json_parser_object *child,
475                                         const char *name)
476 {
477         int lReturn = -1;
478         JsonObject *tempObj = NULL;
479         JsonNodeType jNodeParentType = JSON_NODE_NULL;
480         JsonNodeType jNodeChildType = JSON_NODE_NULL;
481
482         /*Input params validation */
483         if (parent == NULL || parent->value == NULL || child == NULL
484             || name == NULL) {
485                 MSG_DEBUG("Invalid Input Parameters");
486                 return -1;
487         }
488
489         /** Get the JSON Parent Node Type */
490         jNodeParentType = json_node_get_node_type((JsonNode *) parent->value);
491
492         switch (jNodeParentType) {
493         case JSON_NODE_OBJECT: {
494                         /** Get the JSON object from JSON Parent Node */
495                         tempObj = json_node_get_object((JsonNode *) parent->value);
496
497                         /** Get the list of keys from the object node */
498                         GList *members = json_object_get_members(tempObj);
499                         if (members == NULL) {
500                                 return -1;
501                         }
502
503                         /** Get the key by index from the list */
504                         char *member = NULL;
505                         for (unsigned int i = 0; i < g_list_length(members); i++) {
506                                 member = (char *)g_list_nth_data(members, i);
507
508                                 if (g_strcmp0((char *)name, member) == 0) {
509                                         child->key      = member;
510                                 }
511                         }
512
513                         g_list_free(members);
514
515                         if (child->key == NULL) {
516                                 return -1;
517                         }
518
519                         /** Get the JSONNode by key from the list */
520                         child->value = (JsonNode *) json_object_get_member(tempObj,     child->key);
521
522                         /** Identify the node type of the JSOSNNode */
523                         jNodeChildType = json_node_get_node_type((JsonNode *) child->value);
524
525                         switch (jNodeChildType) {
526                         case JSON_NODE_OBJECT:
527                                 child->type = MSG_JSON_PARSER_OBJECT;
528                                 break;
529                         case JSON_NODE_ARRAY:
530                                 child->type = MSG_JSON_PARSER_ARRAY;
531                                 break;
532                         case JSON_NODE_VALUE:
533                                 lReturn = msg_json_parser_get_value(child);
534                                 break;
535                         case JSON_NODE_NULL:
536                                 break;
537                         default:
538                                 lReturn = -1;
539                                 break;
540                         }
541                 }
542                 break;
543         case JSON_NODE_ARRAY:
544         case JSON_NODE_VALUE:
545         default: {
546                         child->key = NULL;
547                         child->value = NULL;
548                         lReturn = -1;
549                 }
550                 break;
551         }
552
553         return lReturn;
554 }
555
556
557 /**
558  * @fn msg_json_parser_get_child_count
559  * This function returns the count for a specified JSON object.
560  * @param [IN] \n
561  * object: msg_json_parser_object structure pointer whose child count
562         is to be retrieved \n
563  * @return      Return type is int. This contains the value of the child node
564         for the specified JSON object \n
565  * @remark This API is synchronous.
566  */
567 int msg_json_parser_get_child_count(msg_json_parser_object *object)
568 {
569         JsonNodeType jNodeParentType = JSON_NODE_NULL;
570         JsonObject *tempObj = NULL;
571         JsonArray *tempArray = NULL;
572         int count = 0;
573
574         /*Input params validation */
575         if (object == NULL || object->value == NULL) {
576                 MSG_DEBUG("Invalid Input Parameters");
577                 return count;
578         }
579
580         /** Get the JSON Parent Node Type */
581         jNodeParentType = json_node_get_node_type((JsonNode *) object->value);
582
583         switch (jNodeParentType) {
584         case JSON_NODE_OBJECT: {
585                         MSG_DEBUG("  JSON_NODE_OBJECT ");
586                         tempObj = NULL;
587                         /** Get the JSON object from JSON Parent Node */
588                         tempObj = json_node_get_object((JsonNode *) object->value);
589                         /** Get the number of members from the object node */
590                         count = json_object_get_size(tempObj);
591                 }
592                 break;
593         case JSON_NODE_ARRAY: {
594                         MSG_DEBUG("  JSON_NODE_ARRAY ");
595                         tempArray = NULL;
596                         /** Get the JSON array from the JSON node */
597                         tempArray =
598                             json_node_get_array((JsonNode *) object->value);
599                         /** Get the number of members from the array node */
600                         count = json_array_get_length(tempArray);
601                 }
602                 break;
603         case JSON_NODE_VALUE:
604         default:
605                 break;
606         }
607
608         MSG_DEBUG("COUNT :: %d ", count);
609         return count;
610 }
611