[messageport][common] Move BundleJsonIterator to common. 35/202735/5
authorMichal Michalski <m.michalski2@partner.samsung.com>
Wed, 3 Apr 2019 10:52:21 +0000 (12:52 +0200)
committerMichal Michalski <m.michalski2@partner.samsung.com>
Thu, 4 Apr 2019 07:53:17 +0000 (09:53 +0200)
It will be used in mediacontroller search API.

[Verification]

tct-messageport-tizen-tests 100% pass rate.

Change-Id: I1ef945697c16cc21801cb53e2bb13a389f777e9a

src/common/common.gyp
src/common/json-utils.cc [new file with mode: 0644]
src/common/json-utils.h [new file with mode: 0644]
src/messageport/messageport_instance.cc

index 89f99a8..fab56f4 100644 (file)
@@ -19,6 +19,8 @@
         'filter-utils.cc',
         'filter-utils.h',
         'picojson.h',
+        'json-utils.cc',
+        'json-utils.h',
         'utils.h',
         'logger.cc',
         'logger.h',
diff --git a/src/common/json-utils.cc b/src/common/json-utils.cc
new file mode 100644 (file)
index 0000000..bb71962
--- /dev/null
@@ -0,0 +1,160 @@
+/*
+ * Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *    Licensed under the Apache License, Version 2.0 (the "License");
+ *    you may not use this file except in compliance with the License.
+ *    You may obtain a copy of the License at
+ *
+ *        http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *    Unless required by applicable law or agreed to in writing, software
+ *    distributed under the License is distributed on an "AS IS" BASIS,
+ *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *    See the License for the specific language governing permissions and
+ *    limitations under the License.
+ */
+
+#include <bundle.h>
+#include <bundle_internal.h>
+#include "common/logger.h"
+#include "common/picojson.h"
+#include "json-utils.h"
+
+
+namespace common {
+
+
+PlatformResult JsonToBundle(const picojson::object& json, bundle* bundle_data) {
+  ScopeLogger();
+  for (const auto& entry: json) {
+    int ret = bundle_add(bundle_data, entry.first.c_str(), entry.second.serialize().c_str());
+    if (BUNDLE_ERROR_NONE != ret) {
+      return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Error occured during json to bundle conversion",
+        ("bundle_add() returned error code: %d, error msg: %s", ret, get_error_message(ret)));
+    }
+  }
+  return PlatformResult(ErrorCode::NO_ERROR);
+}
+
+PlatformResult JsonToBundle(const picojson::array& json, bundle* bundle_data) {
+  ScopeLogger();
+  for (const auto& item: json) {
+    if (!item.is<picojson::object>()) {
+      return LogAndCreateResult(ErrorCode::TYPE_MISMATCH_ERR, "Invalid json structure",
+        ("item.is<picojson::object>() returned false"));
+    }
+    const auto& json = item.get<picojson::object>();
+
+    if (json.find("key") == json.end()) {
+      return LogAndCreateResult(ErrorCode::VALIDATION_ERR, "Key attribute is missing",
+        ("Key attribute not found in json object."));
+    }
+    if (!json.at("key").is<std::string>()) {
+      return LogAndCreateResult(ErrorCode::VALIDATION_ERR, "Key attribute needs to have string type",
+        ("Key attribute is not a string. Key value: %s", json.at("key").serialize().c_str()));
+    }
+    std::string key = json.at("key").get<std::string>();
+
+    if (json.find("value") == json.end()) {
+      return LogAndCreateResult(ErrorCode::VALIDATION_ERR, "Value attribute is missing",
+        ("Value attribute not found in json object."));
+    }
+    if (!json.at("value").is<std::string>()) {
+      return LogAndCreateResult(ErrorCode::VALIDATION_ERR, "Value attribute needs to have string type",
+        ("Value attribute is not a string. Value: %s", json.at("value").serialize().c_str()));
+    }
+    std::string value = json.at("value").get<std::string>();
+
+    int ret = bundle_add(bundle_data, key.c_str(), value.c_str());
+    if (BUNDLE_ERROR_NONE != ret) {
+      return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Error occured during json to bundle conversion",
+        ("bundle_add() returned error code: %d, error msg: %s", ret, get_error_message(ret)));
+    }
+  }
+  return PlatformResult(ErrorCode::NO_ERROR);
+}
+
+void BundleJsonIterator(const char* key, const int type, const bundle_keyval_t* kv, void* d) {
+  ScopeLogger();
+
+  void* basic_val = nullptr;
+  size_t basic_size = 0;
+
+  picojson::value::array* array = static_cast<picojson::value::array*>(d);
+  picojson::value::object o;
+
+  switch (bundle_keyval_get_type(const_cast<bundle_keyval_t*>(kv))) {
+    case BUNDLE_TYPE_STR:
+      bundle_keyval_get_basic_val(const_cast<bundle_keyval_t*>(kv), &basic_val, &basic_size);
+      o["key"] = picojson::value(key);
+      o["value"] = picojson::value(static_cast<char*>(basic_val));
+      break;
+
+    case BUNDLE_TYPE_STR_ARRAY: {
+      picojson::value::array tab;
+      void** array_val = nullptr;
+      unsigned int array_len = 0;
+      size_t* array_elem_size = nullptr;
+
+      bundle_keyval_get_array_val(const_cast<bundle_keyval_t*>(kv), &array_val, &array_len,
+                                  &array_elem_size);
+
+      for (unsigned int i = 0; i < array_len; i++) {
+        tab.push_back(picojson::value(((char**)array_val)[i]));
+      }
+
+      o["key"] = picojson::value(key);
+      o["value"] = picojson::value(picojson::value(tab));
+
+      break;
+    }
+
+    case BUNDLE_TYPE_BYTE: {
+      picojson::value::array tab;
+
+      unsigned char* basic_val = nullptr;
+      size_t basic_size = 0;
+
+      bundle_keyval_get_basic_val(const_cast<bundle_keyval_t*>(kv), (void**)&basic_val,
+                                  &basic_size);
+
+      for (unsigned int i = 0; i < basic_size; i++) {
+        tab.push_back(picojson::value(static_cast<double>(basic_val[i])));
+      }
+
+      o["key"] = picojson::value(key);
+      o["value"] = picojson::value(picojson::value(tab));
+      break;
+    }
+    case BUNDLE_TYPE_BYTE_ARRAY: {
+      picojson::value::array tab;
+
+      unsigned char** array_value = nullptr;
+      size_t* array_ele_size = nullptr;
+      unsigned int ele_nos = 0;
+
+      bundle_keyval_get_array_val(const_cast<bundle_keyval_t*>(kv), (void***)&array_value, &ele_nos,
+                                  &array_ele_size);
+
+      for (unsigned int i = 0; i < ele_nos; i++) {
+        picojson::value::array tab2;
+        for (unsigned int j = 0; j < array_ele_size[i]; j++) {
+          tab2.push_back(picojson::value(static_cast<double>(array_value[i][j])));
+        }
+        tab.push_back(picojson::value(tab2));
+      }
+
+      o["key"] = picojson::value(key);
+      o["value"] = picojson::value(picojson::value(tab));
+      break;
+    }
+    default:
+      o["key"] = picojson::value(key);
+      o["value"] = picojson::value();
+      break;
+  }
+  array->push_back(picojson::value(o));
+}
+
+} // namespace common
+
diff --git a/src/common/json-utils.h b/src/common/json-utils.h
new file mode 100644 (file)
index 0000000..a2809c5
--- /dev/null
@@ -0,0 +1,35 @@
+/*
+ * Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *    Licensed under the Apache License, Version 2.0 (the "License");
+ *    you may not use this file except in compliance with the License.
+ *    You may obtain a copy of the License at
+ *
+ *        http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *    Unless required by applicable law or agreed to in writing, software
+ *    distributed under the License is distributed on an "AS IS" BASIS,
+ *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *    See the License for the specific language governing permissions and
+ *    limitations under the License.
+ */
+
+#ifndef COMMON_JSON_UTILS_H
+#define COMMON_JSON_UTILS_H
+
+#include <bundle.h>
+#include "platform_result.h"
+#include "picojson.h"
+
+
+namespace common {
+
+void BundleJsonIterator(const char* key, const int type, const bundle_keyval_t* kv, void* d);
+
+PlatformResult JsonToBundle(const picojson::object& json, bundle* bundle_data);
+PlatformResult JsonToBundle(const picojson::array& json, bundle* bundle_data);
+
+} // namespace common;
+
+
+#endif // COMMON_JSON_UTILS_H
index 4b342d4..a845668 100644 (file)
@@ -24,6 +24,7 @@
 #include "common/logger.h"
 #include "common/picojson.h"
 #include "common/platform_exception.h"
+#include "common/json-utils.h"
 
 namespace extension {
 namespace messageport {
@@ -33,6 +34,7 @@ using common::InvalidValuesException;
 using common::UnknownException;
 using common::NotFoundException;
 using common::QuotaExceededException;
+using common::BundleJsonIterator;
 
 MessageportInstance::MessageportInstance() {
   ScopeLogger();
@@ -66,89 +68,6 @@ enum MessageportCallbacks {
   LocalMessagePortAddmessageportlistenerCallback
 };
 
-static void BundleJsonIterator(const char* key, const int type, const bundle_keyval_t* kv,
-                               void* d) {
-  ScopeLogger();
-
-  void* basic_val = nullptr;
-  size_t basic_size = 0;
-
-  picojson::value::array* array = static_cast<picojson::value::array*>(d);
-  picojson::value::object o;
-
-  switch (bundle_keyval_get_type(const_cast<bundle_keyval_t*>(kv))) {
-    case BUNDLE_TYPE_STR:
-      bundle_keyval_get_basic_val(const_cast<bundle_keyval_t*>(kv), &basic_val, &basic_size);
-      o["key"] = picojson::value(key);
-      o["value"] = picojson::value(static_cast<char*>(basic_val));
-      break;
-
-    case BUNDLE_TYPE_STR_ARRAY: {
-      picojson::value::array tab;
-      void** array_val = nullptr;
-      unsigned int array_len = 0;
-      size_t* array_elem_size = nullptr;
-
-      bundle_keyval_get_array_val(const_cast<bundle_keyval_t*>(kv), &array_val, &array_len,
-                                  &array_elem_size);
-
-      for (unsigned int i = 0; i < array_len; i++) {
-        tab.push_back(picojson::value(((char**)array_val)[i]));
-      }
-
-      o["key"] = picojson::value(key);
-      o["value"] = picojson::value(picojson::value(tab));
-
-      break;
-    }
-
-    case BUNDLE_TYPE_BYTE: {
-      picojson::value::array tab;
-
-      unsigned char* basic_val = nullptr;
-      size_t basic_size = 0;
-
-      bundle_keyval_get_basic_val(const_cast<bundle_keyval_t*>(kv), (void**)&basic_val,
-                                  &basic_size);
-
-      for (unsigned int i = 0; i < basic_size; i++) {
-        tab.push_back(picojson::value(static_cast<double>(basic_val[i])));
-      }
-
-      o["key"] = picojson::value(key);
-      o["value"] = picojson::value(picojson::value(tab));
-      break;
-    }
-    case BUNDLE_TYPE_BYTE_ARRAY: {
-      picojson::value::array tab;
-
-      unsigned char** array_value = nullptr;
-      size_t* array_ele_size = nullptr;
-      unsigned int ele_nos = 0;
-
-      bundle_keyval_get_array_val(const_cast<bundle_keyval_t*>(kv), (void***)&array_value, &ele_nos,
-                                  &array_ele_size);
-
-      for (unsigned int i = 0; i < ele_nos; i++) {
-        picojson::value::array tab2;
-        for (unsigned int j = 0; j < array_ele_size[i]; j++) {
-          tab2.push_back(picojson::value(static_cast<double>(array_value[i][j])));
-        }
-        tab.push_back(picojson::value(tab2));
-      }
-
-      o["key"] = picojson::value(key);
-      o["value"] = picojson::value(picojson::value(tab));
-      break;
-    }
-    default:
-      o["key"] = picojson::value(key);
-      o["value"] = picojson::value();
-      break;
-  }
-  array->push_back(picojson::value(o));
-}
-
 #define CHECK_EXIST(args, name, out)                                             \
   if (!args.contains(name)) {                                                    \
     LogAndReportError(TypeMismatchException(name " is required argument"), out); \