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.
22 #include <glib-object.h>
23 #include <json-glib/json-glib.h>
24 #include <json-glib/json-gobject.h>
25 #include "MsgJsonParser.h"
27 static char root_key[5] = "root";
29 msg_json_gen_object* msg_json_gen_new_obj(msg_json_gen_type type)
31 void *json_value = NULL;
33 case MSG_JSON_GEN_OBJECT:
34 json_value = (void *)json_object_new();
36 case MSG_JSON_GEN_ARRAY:
37 json_value = (void *)json_array_new();
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));
46 if (new_obj == NULL) {
47 if (type == MSG_JSON_GEN_OBJECT)
48 json_object_ref((JsonObject *)json_value);
49 else if (type == MSG_JSON_GEN_ARRAY)
50 json_array_unref((JsonArray *)json_value);
56 new_obj->value = json_value;
62 void msg_json_gen_free_obj(msg_json_gen_object *obj)
67 if (obj->value != NULL) {
68 if (obj->type == MSG_JSON_GEN_OBJECT)
69 json_object_ref((JsonObject *)obj->value);
70 else if (obj->type == MSG_JSON_GEN_ARRAY)
71 json_array_unref((JsonArray *)obj->value);
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) {
84 switch(parent->type) {
85 case MSG_JSON_GEN_OBJECT:
90 case MSG_JSON_GEN_OBJECT:
91 json_object_set_object_member((JsonObject *)parent->value, key, (JsonObject *)child->value);
93 case MSG_JSON_GEN_ARRAY:
94 json_object_set_array_member((JsonObject *)parent->value, key, (JsonArray *)child->value);
98 case MSG_JSON_GEN_ARRAY:
100 case MSG_JSON_GEN_OBJECT:
101 json_array_add_object_element((JsonArray *)parent->value, (JsonObject *)child->value);
103 case MSG_JSON_GEN_ARRAY:
104 json_array_add_array_element((JsonArray *)parent->value, (JsonArray *)child->value);
111 void msg_json_gen_set_value(msg_json_gen_object *parent, const char *key, long long int value) {
112 if (parent == NULL) {
116 switch(parent->type) {
117 case MSG_JSON_GEN_OBJECT:
121 json_object_set_int_member((JsonObject *)parent->value, key, value);
123 case MSG_JSON_GEN_ARRAY:
124 json_array_add_int_element((JsonArray *)parent->value, value);
129 void msg_json_gen_set_value(msg_json_gen_object *parent, const char *key, const char *value) {
130 if (parent == NULL) {
134 switch(parent->type) {
135 case MSG_JSON_GEN_OBJECT:
139 json_object_set_string_member((JsonObject *)parent->value, key, value);
141 case MSG_JSON_GEN_ARRAY:
142 json_array_add_string_element((JsonArray *)parent->value, value);
147 char* msg_json_gen_make_json_msg(msg_json_gen_object *root_obj, unsigned long *len) {
148 if (root_obj == NULL) {
151 JsonGenerator *generator = json_generator_new();
152 JsonNode *root = json_node_new(JSON_NODE_OBJECT);
154 json_node_take_object(root, (JsonObject *)root_obj->value);
155 json_generator_set_root(generator, root);
157 char *json_msg = json_generator_to_data(generator, (gsize *)len);
158 g_object_unref(generator);
159 json_node_free(root);
164 int msg_json_parser_get_value(msg_json_parser_object *json_obj)
166 GValue value = { 0 };
168 /*Input params validation */
169 if (json_obj == NULL) {
170 MSG_DEBUG("Invalid Input Parameters");
174 json_obj->float_value = 0;
175 json_obj->number_value = 0;
177 json_node_get_value((JsonNode *) json_obj->value, &value);
179 switch (json_node_get_value_type((JsonNode *) json_obj->value)) {
181 json_obj->type = MSG_JSON_PARSER_STRING;
182 json_obj->value = (void *)g_value_get_string(&value);
185 json_obj->type = MSG_JSON_PARSER_INT;
186 json_obj->value = NULL;
187 json_obj->number_value = g_value_get_int(&value);
190 json_obj->type = MSG_JSON_PARSER_UINT;
191 json_obj->value = NULL;
192 json_obj->number_value = g_value_get_uint(&value);
195 json_obj->type = MSG_JSON_PARSER_BOOLEAN;
196 json_obj->value = NULL;
197 json_obj->number_value = g_value_get_boolean(&value);
200 json_obj->type = MSG_JSON_PARSER_INT;
201 json_obj->value = NULL;
202 json_obj->number_value = g_value_get_int64(&value);
205 json_obj->type = MSG_JSON_PARSER_UINT;
206 json_obj->value = NULL;
207 json_obj->number_value = g_value_get_uint64(&value);
210 json_obj->type = MSG_JSON_PARSER_REAL;
211 json_obj->value = NULL;
212 json_obj->float_value = (double)g_value_get_double(&value);
215 MSG_DEBUG(" Entering node default case");
222 int msg_json_parser_object_get_value(msg_json_parser_object *json_obj)
224 GValue value = { 0 };
225 JsonNode *node = NULL;
227 /*Input params validation */
228 if (json_obj == NULL) {
229 MSG_DEBUG("Invalid Input Parameters");
232 node = json_object_get_member((JsonObject *)json_obj->value, json_obj->key);
234 json_obj->float_value = 0;
235 json_obj->number_value = 0;
237 json_node_get_value((JsonNode *) node, &value);
239 switch (json_node_get_value_type((JsonNode *) node)) {
241 json_obj->type = MSG_JSON_PARSER_STRING;
242 json_obj->value = (void *)g_value_get_string(&value);
245 json_obj->type = MSG_JSON_PARSER_INT;
246 json_obj->value = NULL;
247 json_obj->number_value = g_value_get_int(&value);
250 json_obj->type = MSG_JSON_PARSER_UINT;
251 json_obj->value = NULL;
252 json_obj->number_value = g_value_get_uint(&value);
255 json_obj->type = MSG_JSON_PARSER_BOOLEAN;
256 json_obj->value = NULL;
257 json_obj->number_value = g_value_get_boolean(&value);
260 json_obj->type = MSG_JSON_PARSER_INT;
261 json_obj->value = NULL;
262 json_obj->number_value = g_value_get_int64(&value);
265 json_obj->type = MSG_JSON_PARSER_UINT;
266 json_obj->value = NULL;
267 json_obj->number_value = g_value_get_uint64(&value);
270 json_obj->type = MSG_JSON_PARSER_REAL;
271 json_obj->value = NULL;
272 json_obj->float_value = (double)g_value_get_double(&value);
275 MSG_DEBUG(" Entering node default case");
282 msg_json_parser_handle msg_json_parser_handle_create(void)
284 JsonParser *jsonParser = NULL;
286 jsonParser = json_parser_new();
288 return(msg_json_parser_handle)jsonParser;
291 void msg_json_parser_handle_destory(msg_json_parser_handle *handle)
296 g_object_unref(*handle);
300 void msg_json_parser_parse_buffer(msg_json_parser_handle handle, const char* value, int value_len, msg_json_parser_object *json_obj)
302 gboolean gRet = TRUE;
303 JsonNode *root = NULL;
304 GError *error = NULL;
305 JsonParser *jsonParser = (JsonParser *)handle;
306 JsonNodeType parentType = JSON_NODE_NULL;
308 /*Input params validation*/
309 if (value == NULL || value_len == 0) {
310 MSG_DEBUG("Invalid Input Parameters");
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);
320 /** Fetch the root node */
321 root = json_parser_get_root(jsonParser);
324 json_obj->key = root_key;
325 json_obj->value = root;
326 parentType = json_node_get_node_type(root);
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;
335 json_obj->type = MSG_JSON_PARSER_NULL;
338 json_obj->key = NULL;
339 json_obj->value = NULL;
348 int msg_json_parser_get_next_child(const msg_json_parser_object *parent, msg_json_parser_object *child, int index)
351 JsonNodeType jNodeParentType = JSON_NODE_NULL;
352 JsonNodeType jNodeChildType = JSON_NODE_NULL;
353 JsonArray *tempArray = NULL;
354 JsonObject *tempObj = NULL;
355 GList *members = NULL;
357 /*Input params validation */
358 if (parent == NULL || parent->value == NULL || child == NULL || index < 0) {
359 MSG_DEBUG("Invalid Input Parameters");
363 /** Get the JSON Parent Node Type */
364 jNodeParentType = json_node_get_node_type((JsonNode *) parent->value);
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);
371 /** Get the list of keys from the object node */
372 members = json_object_get_members(tempObj);
374 /** Get the key by index from the list */
375 child->key = (char *)g_list_nth_data(members, index);
377 g_list_free(members);
378 if (child->key == NULL) {
382 /** Get the JSONNode by key from the list */
383 child->value = json_object_get_member(tempObj, child->key);
385 /** Identify the node type of the JSOSNNode */
386 jNodeChildType = json_node_get_node_type((JsonNode *)child->value);
388 switch (jNodeChildType) {
389 case JSON_NODE_OBJECT:
390 child->type = MSG_JSON_PARSER_OBJECT;
392 case JSON_NODE_ARRAY:
393 child->type = MSG_JSON_PARSER_ARRAY;
395 case JSON_NODE_VALUE:
396 lReturn = msg_json_parser_get_value(child);
406 case JSON_NODE_ARRAY: {
407 /** Get the JSON array from the JSON node */
408 tempArray = json_node_get_array((JsonNode *) parent->value);
412 if ((guint) index >= json_array_get_length(tempArray)) {
416 /** Get the JSONNode from the list of values */
417 child->value = (void *)json_array_get_element(tempArray, index);
418 if (child->value == NULL) {
421 /* Get the child type */
422 jNodeChildType = json_node_get_node_type((JsonNode *) child->value);
424 switch (jNodeChildType) {
425 case JSON_NODE_OBJECT:
426 child->type = MSG_JSON_PARSER_OBJECT;
428 case JSON_NODE_ARRAY:
429 child->type = MSG_JSON_PARSER_ARRAY;
431 case JSON_NODE_VALUE:
432 lReturn = msg_json_parser_get_value(child);
442 case JSON_NODE_VALUE:
456 * @fn msg_json_parser_get_child_by_name
457 * This function is used for getting the child node by it's name for
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
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.
473 int msg_json_parser_get_child_by_name(const msg_json_parser_object *parent,
474 msg_json_parser_object *child,
478 JsonObject *tempObj = NULL;
479 JsonNodeType jNodeParentType = JSON_NODE_NULL;
480 JsonNodeType jNodeChildType = JSON_NODE_NULL;
482 /*Input params validation */
483 if (parent == NULL || parent->value == NULL || child == NULL
485 MSG_DEBUG("Invalid Input Parameters");
489 /** Get the JSON Parent Node Type */
490 jNodeParentType = json_node_get_node_type((JsonNode *) parent->value);
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);
497 /** Get the list of keys from the object node */
498 GList *members = json_object_get_members(tempObj);
499 if (members == NULL) {
503 /** Get the key by index from the list */
505 for (unsigned int i = 0; i < g_list_length(members); i++) {
506 member = (char *)g_list_nth_data(members, i);
508 if (g_strcmp0((char *)name, member) == 0) {
513 g_list_free(members);
515 if (child->key == NULL) {
519 /** Get the JSONNode by key from the list */
520 child->value = (JsonNode *) json_object_get_member(tempObj, child->key);
522 /** Identify the node type of the JSOSNNode */
523 jNodeChildType = json_node_get_node_type((JsonNode *) child->value);
525 switch (jNodeChildType) {
526 case JSON_NODE_OBJECT:
527 child->type = MSG_JSON_PARSER_OBJECT;
529 case JSON_NODE_ARRAY:
530 child->type = MSG_JSON_PARSER_ARRAY;
532 case JSON_NODE_VALUE:
533 lReturn = msg_json_parser_get_value(child);
543 case JSON_NODE_ARRAY:
544 case JSON_NODE_VALUE:
558 * @fn msg_json_parser_get_child_count
559 * This function returns the count for a specified JSON object.
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.
567 int msg_json_parser_get_child_count(msg_json_parser_object *object)
569 JsonNodeType jNodeParentType = JSON_NODE_NULL;
570 JsonObject *tempObj = NULL;
571 JsonArray *tempArray = NULL;
574 /*Input params validation */
575 if (object == NULL || object->value == NULL) {
576 MSG_DEBUG("Invalid Input Parameters");
580 /** Get the JSON Parent Node Type */
581 jNodeParentType = json_node_get_node_type((JsonNode *) object->value);
583 switch (jNodeParentType) {
584 case JSON_NODE_OBJECT: {
585 MSG_DEBUG(" JSON_NODE_OBJECT ");
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);
593 case JSON_NODE_ARRAY: {
594 MSG_DEBUG(" JSON_NODE_ARRAY ");
596 /** Get the JSON array from the JSON node */
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);
603 case JSON_NODE_VALUE:
608 MSG_DEBUG("COUNT :: %d ", count);