--- /dev/null
+/*
+ * 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
+
--- /dev/null
+/*
+ * 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
#include "common/logger.h"
#include "common/picojson.h"
#include "common/platform_exception.h"
+#include "common/json-utils.h"
namespace extension {
namespace messageport {
using common::UnknownException;
using common::NotFoundException;
using common::QuotaExceededException;
+using common::BundleJsonIterator;
MessageportInstance::MessageportInstance() {
ScopeLogger();
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); \