apply tizen c coding rule
[platform/core/messaging/msg-service.git] / include / utils / MsgJsonParser.h
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 #ifndef __MSG_JSON_PARSER_H__
18 #define __MSG_JSON_PARSER_H__
19
20 #include <stdio.h>
21 #include "MsgDebug.h"
22
23 typedef enum {
24         MSG_JSON_GEN_ARRAY,
25         MSG_JSON_GEN_OBJECT
26 } msg_json_gen_type;
27
28 typedef struct msg_json_gen_object_t {
29         msg_json_gen_type type;
30         void *value;
31 } msg_json_gen_object;
32
33 msg_json_gen_object* msg_json_gen_new_obj(msg_json_gen_type type);
34 void msg_json_gen_free_obj(msg_json_gen_object *obj);
35 void msg_json_gen_set_child(msg_json_gen_object *parent, const char *key, msg_json_gen_object *child);
36 void msg_json_gen_set_value(msg_json_gen_object *parent, const char *key, long long int value);
37 void msg_json_gen_set_value(msg_json_gen_object *parent, const char *key, const char *value);
38 char* msg_json_gen_make_json_msg(msg_json_gen_object *root_obj, unsigned long *len);
39
40 #define MSG_JSON_OBJ_SET_STR(json_obj, key, val)\
41         if (json_obj && key && val) {\
42                 json_object_set_string_member(json_obj, key, val);\
43         } else {\
44                 MSG_DEBUG("Error : json_obj = %p, key = %s, val = %s", json_obj, key, val);\
45         }
46
47 #define MSG_JSON_OBJ_SET_INT(json_obj, key, val)\
48         if (json_obj && key) {\
49                 json_object_set_int_member(json_obj, key, val);\
50         } else {\
51                 MSG_DEBUG("Error : json_obj = %p, key = %s, val = %d", json_obj, key, val);\
52         }
53
54 #define MSG_JSON_OBJ_SET_OBJ(json_obj, key, val)\
55         if (json_obj && key) {\
56                 json_object_set_object_member(json_obj, key, val);\
57         } else {\
58                 MSG_DEBUG("Error : json_obj = %p, key = %s, val = %p", json_obj, key, val);\
59         }
60
61 #define MSG_JSON_OBJ_SET_ARRAY(json_obj, key, json_array)\
62         if (json_obj && key && json_array) {\
63                 json_object_set_array_member(json_obj, key, json_array);\
64         } else {\
65                 MSG_DEBUG("Error : json_obj = %p, key = %s, json_array = %p", json_obj, key, json_array);\
66         }
67
68 #define MSG_JSON_ARRAY_ADD_OBJECT(json_array, json_obj)\
69         if (json_array && json_obj) {\
70                 json_array_add_object_element(json_array, json_obj);\
71         } else {\
72                 MSG_DEBUG("Error : json_array = %p, json_obj = %p", json_array, json_obj);\
73         }
74
75 #define MSG_PRINT_PARSER_OBJECT(index, obj) {\
76         switch (obj.type) {\
77         case MSG_JSON_PARSER_NULL:\
78                 MSG_DEBUG("parse obj : idx = %d, key =[%s], type = [%d], value = [%p], value num = [%f]", index, obj.key, obj.type, obj.value, obj.number_value);\
79                 break;\
80         case MSG_JSON_PARSER_INT:\
81                 MSG_DEBUG("parse obj : idx = [%d], key = [%s], type = [%s], value = [%d]", index, obj.key, "MSG_JSON_PARSER_INT", (int)obj.number_value);\
82                 break;\
83         case MSG_JSON_PARSER_UINT:\
84                 MSG_DEBUG("parse obj : idx = [%d], key = [%s], type = [%s], value = [%u]", index, obj.key, "MSG_JSON_PARSER_UINT", (unsigned int)obj.number_value);\
85                 break;\
86         case MSG_JSON_PARSER_REAL:\
87                 MSG_DEBUG("parse obj : idx = [%d], key = [%s], type = [%s], value = [%f]", index, obj.key, "MSG_JSON_PARSER_REAL", obj.number_value);\
88                 break;\
89         case MSG_JSON_PARSER_STRING:\
90                 MSG_DEBUG("parse obj : idx = [%d], key = [%s], type = [%s], value = [%s]", index, obj.key, "MSG_JSON_PARSER_STRING", (char *)obj.value);\
91                 break;\
92         case MSG_JSON_PARSER_BOOLEAN:\
93                 MSG_DEBUG("parse obj : idx = [%d], key = [%s], type = [%s], value = [%s]", index, obj.key, "MSG_JSON_PARSER_BOOLEAN", obj.number_value == 0 ? "TRUE" : "FALSE");\
94                 break;\
95         case MSG_JSON_PARSER_ARRAY:\
96                 MSG_DEBUG("parse obj : idx = [%d], key = [%s], type = [%s]", index, obj.key, "MSG_JSON_PARSER_ARRAY");\
97                 break;\
98         case MSG_JSON_PARSER_OBJECT:\
99                 MSG_DEBUG("parse obj : idx = [%d], key = [%s], type = [%s]", index, obj.key, "MSG_JSON_PARSER_OBJECT");\
100                 break;\
101         default:\
102                 MSG_DEBUG("parse obj : idx = %d, key =[%s], type = [%d], value = [%p], value num = [%f]", index, obj.key, obj.type, obj.value, obj.number_value);\
103                 break;\
104         } \
105 }
106 /**
107 *@enum msg_json_parser_value_type
108 * This enumerates json-node types.
109 */
110 typedef enum {
111         MSG_JSON_PARSER_NULL = 0,      /** 0 <null value */
112         MSG_JSON_PARSER_INT,           /** 1 < signed integer value */
113         MSG_JSON_PARSER_UINT,          /** 2 < unsigned integer value */
114         MSG_JSON_PARSER_REAL,          /** 3 < double value */
115         MSG_JSON_PARSER_STRING,        /** 4 < UTF-8 string value */
116         MSG_JSON_PARSER_BOOLEAN,       /** 5 < bool value */
117         MSG_JSON_PARSER_ARRAY,         /** 6 < array value (ordered list) */
118         MSG_JSON_PARSER_OBJECT,        /** 7 < object value */
119 } msg_json_parser_value_type;
120
121 typedef void *msg_json_parser_handle;
122
123 typedef struct msg_json_parser_object_t {
124         msg_json_parser_value_type type;
125         char *key;
126         void *value;    /** Value of the node. VALID
127                                                 only if type is
128                                                 MSG_JSON_PARSER_NULL,
129                                                 MSG_JSON_PARSER_STRING,
130                                                 MSG_JSON_PARSER_ARRAY,
131                                                 MSG_JSON_PARSER_OBJECT */
132         double float_value; /** Value of the node. VALID
133                                                 only if type is
134                                                 MSG_JSON_PARSER_REAL */
135         long long int number_value; /** Value of the node. VALID
136                                                 only if type is
137                                                 MSG_JSON_PARSER_UINT,
138                                                 MSG_JSON_PARSER_INT,
139                                                 MSG_JSON_PARSER_BOOLEAN */
140 } msg_json_parser_object;
141
142 msg_json_parser_handle msg_json_parser_handle_create(void);
143 void msg_json_parser_handle_destory(msg_json_parser_handle *handle);
144 int msg_json_parser_get_value(msg_json_parser_object *json_obj);
145 int msg_json_parser_object_get_value(msg_json_parser_object *json_obj);
146 int msg_json_parser_get_next_child(const msg_json_parser_object *parent, msg_json_parser_object *child, int index);
147 void msg_json_parser_parse_buffer(msg_json_parser_handle handle, const char* value, int value_len, msg_json_parser_object *json_obj);
148 int msg_json_parser_get_child_by_name(const msg_json_parser_object *parent, msg_json_parser_object *child, const char *name);
149 int msg_json_parser_get_child_count(msg_json_parser_object *object);
150
151 #endif /*__MSG_JSON_PARSER_H__*/