Update duplicate part to get JSON value 94/212694/5
authorYoungHun Kim <yh8004.kim@samsung.com>
Mon, 26 Aug 2019 06:26:35 +0000 (15:26 +0900)
committerYoungHun Kim <yh8004.kim@samsung.com>
Mon, 26 Aug 2019 10:09:07 +0000 (19:09 +0900)
Change-Id: I8a9b16944fdf44fbc705407c9d38c7359256eaaf

core/src/muse_core.c

index f5dab2b..467e91b 100644 (file)
@@ -44,6 +44,7 @@ static json_object *_muse_msg_json_tokener_parse_len(const char *str, int *len,
 static gboolean _muse_msg_is_mem_ptr_valid(gpointer ptr);
 static void _muse_msg_json_factory_args(json_object *jobj, va_list ap);
 static json_object *_muse_msg_json_find_obj(json_object *jobj, const char *find_key);
+static gboolean _muse_msg_json_get_obj_value(json_object *obj, muse_core_msg_type_e m_type, void *data);
 
 static int _muse_get_valid_fd_count(int *fds)
 {
@@ -181,6 +182,59 @@ static json_object *_muse_msg_json_find_obj(json_object *jobj, const char *find_
        return NULL;
 }
 
+static gboolean _muse_msg_json_get_obj_value(json_object *obj, muse_core_msg_type_e m_type, void *data)
+{
+       int j_type, idx, len;
+       int *int_data;
+
+       muse_return_val_if_fail(obj, FALSE);
+       muse_return_val_if_fail(data, FALSE);
+
+       j_type = json_object_get_type(obj);
+       switch (j_type) {
+       case json_type_null:
+               LOGD("json_type_null");
+               break;
+       case json_type_double:
+               *(double *)data = json_object_get_double(obj);
+               break;
+       case json_type_int:
+               if (m_type == MUSE_TYPE_ANY || m_type == MUSE_TYPE_INT) {
+                       *(int32_t *)data = json_object_get_int(obj);
+               } else if (m_type == MUSE_TYPE_INT64) {
+                       *(int64_t *)data = json_object_get_int64(obj);
+               } else if (m_type == MUSE_TYPE_POINTER) {
+                       if (!_muse_msg_is_mem_ptr_valid(obj)) {
+                               LOGE("memory pointer is not valid");
+                               return FALSE;
+                       }
+                       if (sizeof(intptr_t) == 8)
+                               *(intptr_t *)data = json_object_get_int64(obj);
+                       else
+                               *(intptr_t *)data = json_object_get_int(obj);
+               } else if (m_type == MUSE_TYPE_DOUBLE) {
+                       *(double *)data = json_object_get_double(obj);
+               }
+               break;
+       case json_type_object:
+               break;
+       case json_type_string:
+               strncpy((char *)data, json_object_get_string(obj), strlen(json_object_get_string(obj)));
+               break;
+       case json_type_array:
+               int_data = (int *)data;
+               len = json_object_array_length(obj);
+               for (idx = 0; idx < len; idx++)
+                       int_data[idx] = json_object_get_int(json_object_array_get_idx(obj, idx));
+               break;
+       default:
+               LOGE("The value (%d) of json type is invalid", j_type);
+               break;
+       }
+
+       return TRUE;
+}
+
 void muse_core_log_cmd_info(char *cmd)
 {
        FILE *fp;
@@ -497,9 +551,8 @@ char *muse_core_msg_new(int api, ...)
 
 bool muse_core_msg_deserialize(const char *key, char *buf, int *parse_len, muse_core_msg_parse_err_e *err, muse_core_msg_type_e m_type, void *data)
 {
-       int j_type, idx, len;
-       int *int_data;
-       json_object *val = NULL, *jobj = NULL;
+       json_object *obj = NULL, *jobj = NULL;
+       bool ret = false;
 
        muse_return_val_if_fail(key, false);
        muse_return_val_if_fail(buf, false);
@@ -511,73 +564,25 @@ bool muse_core_msg_deserialize(const char *key, char *buf, int *parse_len, muse_
        jobj = _muse_msg_json_tokener_parse_len(buf, parse_len, err);
        if (!jobj) {
                LOGE("jobj is NULL");
-               goto out;
+               g_mutex_unlock(&msg_lock);
+               return false;
        }
 
-       val = _muse_msg_json_find_obj(jobj, key);
-       if (!val) {
+       obj = _muse_msg_json_find_obj(jobj, key);
+       if (!obj) {
                LOGE("\"%s\" key is not founded", key);
-               goto out;
+               json_object_put(jobj);
+               g_mutex_unlock(&msg_lock);
+               return false;
        }
 
-       j_type = json_object_get_type(val);
-       switch (j_type) {
-       case json_type_null:
-               LOGD("json_type_null");
-               break;
-       case json_type_boolean:
-               LOGD("json_type_boolean (%s)          value: %d", key, json_object_get_boolean(val));
-               break;
-       case json_type_double:
-               *(double *)data = json_object_get_double(val);
-               break;
-       case json_type_int:
-               if (m_type == MUSE_TYPE_ANY || m_type == MUSE_TYPE_INT) {
-                       *(int32_t *)data = json_object_get_int(val);
-               } else if (m_type == MUSE_TYPE_INT64) {
-                       *(int64_t *)data = json_object_get_int64(val);
-               } else if (m_type == MUSE_TYPE_POINTER) {
-                       if (!_muse_msg_is_mem_ptr_valid(val)) {
-                               LOGE("memory pointer is not valid");
-                               goto out;
-                       }
-                       if (sizeof(intptr_t) == 8)
-                               *(intptr_t *)data = json_object_get_int64(val);
-                       else
-                               *(intptr_t *)data = json_object_get_int(val);
-               } else if (m_type == MUSE_TYPE_DOUBLE) {
-                       *(double *)data = json_object_get_double(val);
-               }
-               break;
-       case json_type_object:
-               break;
-       case json_type_string:
-               strncpy((char *)data, json_object_get_string(val), strlen(json_object_get_string(val)));
-               break;
-       case json_type_array:
-               int_data = (int *)data;
-               len = json_object_array_length(val);
-               for (idx = 0; idx < len; idx++)
-                       int_data[idx] = json_object_get_int(json_object_array_get_idx(val, idx));
-               break;
-       default:
-               LOGE("The value (%d) of json type is invalid", j_type);
-               break;
-       }
+       ret = (bool)_muse_msg_json_get_obj_value(obj, m_type, data);
 
        json_object_put(jobj);
 
        g_mutex_unlock(&msg_lock);
 
-       return true;
-
-out:
-       if (jobj)
-               json_object_put(jobj);
-
-       g_mutex_unlock(&msg_lock);
-
-       return false;
+       return ret;
 }
 
 void muse_core_msg_free(char *msg)
@@ -602,9 +607,8 @@ void *muse_core_msg_object_new(char *str, int *parse_len, muse_core_msg_parse_er
 
 bool muse_core_msg_object_get_value(const char *key, void *jobj, muse_core_msg_type_e m_type, void *data)
 {
-       int j_type, idx, len;
-       int *int_data;
-       json_object *val;
+       json_object *obj;
+       bool ret = false;
 
        muse_return_val_if_fail(key, false);
        muse_return_val_if_fail(jobj, false);
@@ -613,65 +617,18 @@ bool muse_core_msg_object_get_value(const char *key, void *jobj, muse_core_msg_t
 
        g_mutex_lock(&msg_lock);
 
-       val = _muse_msg_json_find_obj((json_object *)jobj, key);
-       if (!val) {
+       obj = _muse_msg_json_find_obj((json_object *)jobj, key);
+       if (!obj) {
                LOGE("\"%s\" key is not found", key);
-               goto out;
-       }
-
-       j_type = json_object_get_type(val);
-       switch (j_type) {
-       case json_type_null:
-               LOGD("json_type_null");
-               break;
-       case json_type_boolean:
-               LOGD("json_type_boolean (%s)          value: %d", key, json_object_get_boolean(val));
-               break;
-       case json_type_double:
-               *(double *)data = json_object_get_double(val);
-               break;
-       case json_type_int:
-               if (m_type == MUSE_TYPE_ANY || m_type == MUSE_TYPE_INT) {
-                       *(int32_t *)data = json_object_get_int(val);
-               } else if (m_type == MUSE_TYPE_INT64) {
-                       *(int64_t *)data = json_object_get_int64(val);
-               } else if (m_type == MUSE_TYPE_POINTER) {
-                       if (!_muse_msg_is_mem_ptr_valid(val)) {
-                               LOGE("memory pointer is not valid");
-                               goto out;
-                       }
-                       if (sizeof(intptr_t) == 8)
-                               *(intptr_t *)data = json_object_get_int64(val);
-                       else
-                               *(intptr_t *)data = json_object_get_int(val);
-               } else if (m_type == MUSE_TYPE_DOUBLE) {
-                       *(double *)data = json_object_get_double(val);
-               }
-               break;
-       case json_type_object:
-               break;
-       case json_type_string:
-               strncpy((char *)data, json_object_get_string(val), strlen(json_object_get_string(val)));
-               break;
-       case json_type_array:
-               int_data = (int *)data;
-               len = json_object_array_length(val);
-               for (idx = 0; idx < len; idx++)
-                       int_data[idx] = json_object_get_int(json_object_array_get_idx(val, idx));
-               break;
-       default:
-               LOGE("The value (%d) of json type is invalid", j_type);
-               break;
+               g_mutex_unlock(&msg_lock);
+               return false;
        }
 
-       g_mutex_unlock(&msg_lock);
+       ret = (bool)_muse_msg_json_get_obj_value(obj, m_type, data);
 
-       return true;
-
-out:
        g_mutex_unlock(&msg_lock);
 
-       return false;
+       return ret;
 }
 
 void muse_core_msg_object_free(void *jobj)