From: Michal Michalski Date: Wed, 3 Apr 2019 10:52:21 +0000 (+0200) Subject: [messageport][common] Move BundleJsonIterator to common. X-Git-Tag: submit/tizen/20190408.122141~2 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=a36239733fbfe043ac9ee6ebb33c7ad4e101a12a;p=platform%2Fcore%2Fapi%2Fwebapi-plugins.git [messageport][common] Move BundleJsonIterator to common. It will be used in mediacontroller search API. [Verification] tct-messageport-tizen-tests 100% pass rate. Change-Id: I1ef945697c16cc21801cb53e2bb13a389f777e9a --- diff --git a/src/common/common.gyp b/src/common/common.gyp index 89f99a81..fab56f44 100644 --- a/src/common/common.gyp +++ b/src/common/common.gyp @@ -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 index 00000000..bb719626 --- /dev/null +++ b/src/common/json-utils.cc @@ -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 +#include +#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()) { + return LogAndCreateResult(ErrorCode::TYPE_MISMATCH_ERR, "Invalid json structure", + ("item.is() returned false")); + } + const auto& json = item.get(); + + 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()) { + 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(); + + 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()) { + 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(); + + 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(d); + picojson::value::object o; + + switch (bundle_keyval_get_type(const_cast(kv))) { + case BUNDLE_TYPE_STR: + bundle_keyval_get_basic_val(const_cast(kv), &basic_val, &basic_size); + o["key"] = picojson::value(key); + o["value"] = picojson::value(static_cast(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(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(kv), (void**)&basic_val, + &basic_size); + + for (unsigned int i = 0; i < basic_size; i++) { + tab.push_back(picojson::value(static_cast(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(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(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 index 00000000..a2809c52 --- /dev/null +++ b/src/common/json-utils.h @@ -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 +#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 diff --git a/src/messageport/messageport_instance.cc b/src/messageport/messageport_instance.cc index 4b342d46..a845668b 100644 --- a/src/messageport/messageport_instance.cc +++ b/src/messageport/messageport_instance.cc @@ -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(d); - picojson::value::object o; - - switch (bundle_keyval_get_type(const_cast(kv))) { - case BUNDLE_TYPE_STR: - bundle_keyval_get_basic_val(const_cast(kv), &basic_val, &basic_size); - o["key"] = picojson::value(key); - o["value"] = picojson::value(static_cast(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(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(kv), (void**)&basic_val, - &basic_size); - - for (unsigned int i = 0; i < basic_size; i++) { - tab.push_back(picojson::value(static_cast(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(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(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); \