2 * Copyright (c) 2014 Samsung Electronics Co., Ltd. All rights reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #ifndef __MSG_JSON_PARSER_H__
18 #define __MSG_JSON_PARSER_H__
28 typedef struct msg_json_gen_object_t {
29 msg_json_gen_type type;
31 } msg_json_gen_object;
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);
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);\
44 MSG_DEBUG("Error : json_obj = %p, key = %s, val = %s", json_obj, key, val);\
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);\
51 MSG_DEBUG("Error : json_obj = %p, key = %s, val = %d", json_obj, key, val);\
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);\
58 MSG_DEBUG("Error : json_obj = %p, key = %s, val = %p", json_obj, key, val);\
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);\
65 MSG_DEBUG("Error : json_obj = %p, key = %s, json_array = %p", json_obj, key, json_array);\
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);\
72 MSG_DEBUG("Error : json_array = %p, json_obj = %p", json_array, json_obj);\
75 #define MSG_PRINT_PARSER_OBJECT(index, obj) {\
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);\
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);\
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);\
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);\
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);\
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");\
95 case MSG_JSON_PARSER_ARRAY:\
96 MSG_DEBUG("parse obj : idx = [%d], key = [%s], type = [%s]", index, obj.key, "MSG_JSON_PARSER_ARRAY");\
98 case MSG_JSON_PARSER_OBJECT:\
99 MSG_DEBUG("parse obj : idx = [%d], key = [%s], type = [%s]", index, obj.key, "MSG_JSON_PARSER_OBJECT");\
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);\
107 *@enum msg_json_parser_value_type
108 * This enumerates json-node types.
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;
121 typedef void *msg_json_parser_handle;
123 typedef struct msg_json_parser_object_t {
124 msg_json_parser_value_type type;
126 void *value; /** Value of the node. VALID
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
134 MSG_JSON_PARSER_REAL */
135 long long int number_value; /** Value of the node. VALID
137 MSG_JSON_PARSER_UINT,
139 MSG_JSON_PARSER_BOOLEAN */
140 } msg_json_parser_object;
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);
151 #endif /*__MSG_JSON_PARSER_H__*/