From 985d1f16b144936f5a9a25c4a88257684fab9c87 Mon Sep 17 00:00:00 2001 From: mazhao Date: Mon, 3 Apr 2023 03:59:09 +0000 Subject: [PATCH 1/2] only json change Signed-off-by: mazhao --- .../src/common/include/json_common.h | 16 +- .../src/common/src/json_common.cpp | 151 ++++++-- .../src/oh_adapter/include/json_object.h | 44 ++- .../src/oh_adapter/src/cjson_object.cpp | 181 ---------- .../src/oh_adapter/src/cjson_object.h | 44 --- .../src/oh_adapter/src/json_object.cpp | 328 ++++++++++++++++++ 6 files changed, 491 insertions(+), 273 deletions(-) delete mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/src/cjson_object.cpp delete mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/src/cjson_object.h create mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/src/json_object.cpp diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/common/include/json_common.h b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/common/include/json_common.h index 22e19391..5936f7ad 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/common/include/json_common.h +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/common/include/json_common.h @@ -18,7 +18,8 @@ #include #include -#include "cjson_object.h" +#include +#include "json_object.h" namespace DocumentDB { class JsonCommon @@ -27,11 +28,14 @@ public: JsonCommon() = default; ~JsonCommon(); - ResultValue* GetValue(CjsonObject *root, std::vector path); - static int GetIdValue(CjsonObject *root, std::vector &id); - static bool CheckIsJson(const std::string &data); - static int GetJsonDeep(const std::string &data); + static ResultValue GetValueByFiled(JsonObject *node, const std::string& filed); + static bool CheckJsonField(const std::string &data); + static int ParseNode(JsonObject *Node, std::vector onePath, std::vector> &parsePath, bool isFirstFloor); + static std::vector> ParsePath(JsonObject* node); + static std::vector GetLeafValue(JsonObject *node); +private: + static bool CheckNode(JsonObject *Node, std::set setString, bool &errflag); + static int CheckLeafNode(JsonObject *Node, std::vector &leafValue); }; - } // DocumentDB #endif // JSON_COMMON_H \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/common/src/json_common.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/common/src/json_common.cpp index 49bccc16..16f835b7 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/common/src/json_common.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/common/src/json_common.cpp @@ -20,51 +20,142 @@ #include "securec.h" namespace DocumentDB { -ResultValue* JsonCommon::GetValue(CjsonObject *root, std::vector path) +ResultValue JsonCommon::GetValueByFiled(JsonObject *node, const std::string& filed) { - return nullptr; + if (node == nullptr) { + return ResultValue(); + } + while (node != nullptr) { + if (node->GetItemFiled() == filed) + { + auto item_value = node->GetItemValue(); + return item_value; + } + if (node->GetNext().IsNull() == true) { + return ResultValue(); + } + auto node_new = node->GetNext(); + node = &node_new; + } + return ResultValue(); } -int JsonCommon::GetIdValue(CjsonObject *root, std::vector &id) +int JsonCommon::CheckLeafNode(JsonObject *node, std::vector &leafValue) +{ + if (node->GetChild().IsNull() == true) { + auto item_value = node->GetItemValue(); + leafValue.emplace_back(item_value); + } + if (node->GetChild().IsNull() != true) { + auto node_new = node->GetChild(); + CheckLeafNode(&node_new, leafValue); + } + if (node->GetNext().IsNull() != true) { + auto node_new = node->GetNext(); + CheckLeafNode(&node_new, leafValue); + } + return E_OK; +} +std::vector JsonCommon::GetLeafValue(JsonObject *node) { - auto &node = root; - if (root == nullptr) { - return E_OK; + std::vector leafValue; + CheckLeafNode(node, leafValue); + return leafValue; +} + +bool JsonCommon::CheckNode(JsonObject *node, std::set setString, bool &errflag) { + if (errflag == false) { + return false; } - while (node->GetNext() != nullptr) { - if (node->GetItemFiled() == "_id") { - auto item_value = node->GetItemValue(); - if (item_value->value_type != ResultValue::ValueType::VALUE_STRING) { - return E_ERROR; - } - id.emplace_back(item_value->value_string); + std::string field_str; + if (node->GetItemValue().value_type != ResultValue::ValueType::VALUE_NULL) { + field_str = node->GetItemFiled(); + if (setString.find(field_str) == setString.end()) { + setString.insert(field_str); } - node = node->GetNext(); + else { + errflag = false; + return false; + } + for (int i = 0; i < field_str.size(); i++) { + if (!(('a'<=field_str[i] && field_str[i]<='z')|| ('A'<=field_str[i] && field_str[i]<='Z') || ('0'<=field_str[i] && field_str[i]<='9') || '_' == field_str[i])) { + errflag = false; + return false; + } + } } - return E_OK; + if (node->GetChild().IsNull() != true) { + auto node_new = node->GetChild(); + std::set stringSet_new; + CheckNode(&node_new, stringSet_new, errflag); + } + if (node->GetNext().IsNull() != true) { + auto node_new = node->GetNext(); + CheckNode(&node_new, setString, errflag); + } + return errflag; } -bool JsonCommon::CheckIsJson(const std::string &data) { - CjsonObject cjsonObj; - if (cjsonObj.Parse(data) == E_ERROR) { +bool JsonCommon::CheckJsonField(const std::string &data) { + JsonObject jsonObj; + if (jsonObj.Init(data) != E_OK) { return false; } - return true; + std::set stringSet; + bool errflag = true; + return CheckNode(&jsonObj, stringSet, errflag); } -int JsonCommon::GetJsonDeep(const std::string &data) { - int lens = 0; - int deep = 0; - for (int i = 0; i < data.size(); i++) { - if (data[i] == '[' || data[i] == '{') { - lens++; - } - else if (data[i] == ']' || data[i] == '}') { - deep = std::max(deep, lens); - lens--; +int JsonCommon::ParseNode(JsonObject* node, std::vector onePath, std::vector> &parsePath, bool isFirstFloor) +{ + std::vector forePath; + if (isFirstFloor) { + std::string tempparse_name; + std::vector parsed_mixfiled_name; + std::string mixfield_name = node->GetItemFiled(); + for (int j = 0; j < mixfield_name.size(); j++) { + if (mixfield_name[j] != '.') { + tempparse_name = tempparse_name + mixfield_name[j]; + } + if (mixfield_name[j] == '.' || j == mixfield_name.size() - 1) { + parsed_mixfiled_name.emplace_back(tempparse_name); + tempparse_name.clear(); + } } + forePath = onePath; + onePath.insert(onePath.end(), parsed_mixfiled_name.begin(), parsed_mixfiled_name.end()); + } else { + std::vector parsed_mixfiled_name; + parsed_mixfiled_name.emplace_back(node->GetItemFiled()); + forePath = onePath; + onePath.insert(onePath.end(), parsed_mixfiled_name.begin(), parsed_mixfiled_name.end()); + } + if (node->GetChild().IsNull() != true && node->GetChild().GetItemFiled() != "") { + auto node_new = node->GetChild(); + ParseNode(&node_new, onePath, parsePath, false); + } + else { + parsePath.emplace_back(onePath); } - return deep; + if (node->GetNext().IsNull() != true) { + auto node_new = node->GetNext(); + ParseNode(&node_new, forePath, parsePath, isFirstFloor); + } + return 0; } +std::vector> JsonCommon::ParsePath(JsonObject* root) +{ + std::vector> parsePath; + auto projection_json = root->GetChild(); + if (projection_json.IsNull() == true) { + GLOGE("projection_json is null"); + } + std::vector onePath; + ParseNode(&projection_json, onePath, parsePath, true); + return parsePath; +} + + + } // namespace DocumentDB \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/include/json_object.h b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/include/json_object.h index f0ebd8fd..925299fd 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/include/json_object.h +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/include/json_object.h @@ -17,12 +17,22 @@ #define JSON_OBJECT_H #include +#include +#include -namespace DocumentDB { +#include "cJSON.h" +namespace DocumentDB { class JsonObject; class ResultValue { public: + ResultValue() {}; + ResultValue(const ResultValue& newValue) { + value_number = newValue.value_number; + value_string = newValue.value_string; + value_type = newValue.value_type; + value_Object = newValue.value_Object; + } enum class ValueType { VALUE_FALSE, VALUE_TRUE, @@ -40,17 +50,27 @@ public: class JsonObject { public: - JsonObject () = default; - virtual ~JsonObject () = default; + JsonObject(); + JsonObject(const JsonObject& newObj); + ~JsonObject (); - virtual int Parse(const std::string &str) = 0; - virtual std::string Print() = 0; - virtual JsonObject* GetObjectItem(const std::string &field) = 0; - virtual JsonObject* GetArrayItem(const int index) = 0; - virtual ResultValue* GetItemValue() = 0; - virtual std::string GetItemFiled() = 0; - virtual int DeleteItemFromObject(const std::string &field) = 0; - virtual int Delete() = 0; + int Init(const std::string &str); + std::string Print(); + JsonObject GetObjectItem(const std::string &field, bool caseSensitive); + JsonObject GetArrayItem(const int index); + JsonObject GetNext(); + JsonObject GetChild(); + int DeleteItemFromObject(const std::string &field); + ResultValue GetItemValue(); + std::string GetItemFiled(); + JsonObject FindItem(const std::vector json_path); + int DeleteItemOnTarget(std::vector &path); + bool IsNull(); +private: + int GetDeep(cJSON *cjson, int deep, int &maxDeep); + cJSON *cjson_; + bool isClone_ = false; }; } // DocumentDB -#endif // JSON_OBJECT_H \ No newline at end of file +#endif // JSON_OBJECT_H + diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/src/cjson_object.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/src/cjson_object.cpp deleted file mode 100644 index 05eb385b..00000000 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/src/cjson_object.cpp +++ /dev/null @@ -1,181 +0,0 @@ -/* -* Copyright (c) 2023 Huawei Device Co., Ltd. -* 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 "doc_errno.h" -#include "cjson_object.h" -#include "log_print.h" - -namespace DocumentDB { - -int CjsonObject::Parse(const std::string &str) -{ - cjson_ = cJSON_Parse(str.c_str()); - if (cjson_ == nullptr) { - return E_ERROR; - } - return E_OK; -} - -std::string CjsonObject::Print() -{ - if (cjson_ == nullptr) { - GLOGE("======================>NULL"); - return ""; - } - char *ret = cJSON_Print(cjson_); - std::string str = ret; - free(ret); - GLOGE("======================>%s", str.c_str()); - return str; -} - -CjsonObject* CjsonObject::GetObjectItem(const std::string &field) -{ - if (cjson_ == nullptr) { - return nullptr; - } - CjsonObject *item = new CjsonObject(); - auto cjson_item = cJSON_GetObjectItem(cjson_, field.c_str()); - if (cjson_item == nullptr) { - return item; - } - item->cjson_ = cjson_item; - return item; -} - -CjsonObject* CjsonObject::GetArrayItem(const int index) -{ - if (cjson_ == nullptr) { - return nullptr; - } - CjsonObject *item = new CjsonObject(); - auto cjson_item = cJSON_GetArrayItem(cjson_, index); - if (cjson_item == nullptr) { - return item; - } - item->cjson_ = cjson_item; - return item; -} - -CjsonObject* CjsonObject::GetNext() -{ - if (cjson_ == nullptr) { - return nullptr; - } - CjsonObject *next = new CjsonObject(); - if (cjson_->next == nullptr) { - return next; - } - next->cjson_ = cjson_->next; - return next; -} - -CjsonObject* CjsonObject::GetChild() -{ - if (cjson_ == nullptr) { - return nullptr; - } - CjsonObject *child = new CjsonObject(); - if (cjson_->child == nullptr) { - return child; - } - child->cjson_ = cjson_->child; - return child; -} - -int CjsonObject::DeleteItemFromObject(const std::string &field) -{ - if (field == "") { - return E_OK; - } - cJSON_DeleteItemFromObject(cjson_, field.c_str()); - return E_OK; -} - -ResultValue* CjsonObject::GetItemValue() -{ - if (cjson_ == nullptr) { - return nullptr; - } - ResultValue* value = new ResultValue(); - switch (cjson_->type) - { - case cJSON_False: - { - value->value_type = ResultValue::ValueType::VALUE_FALSE; - } - break; - case cJSON_True: - { - value->value_type = ResultValue::ValueType::VALUE_TRUE; - } - break; - case cJSON_NULL: - { - value->value_type = ResultValue::ValueType::VALUE_NULL; - } - break; - case cJSON_Number: - { - value->value_type = ResultValue::ValueType::VALUE_NUMBER; - value->value_number = cjson_->valuedouble; - } - break; - case cJSON_String: - { - value->value_type = ResultValue::ValueType::VALUE_STRING; - value->value_string = cjson_->valuestring; - } - break; - case cJSON_Array: - { - value->value_type = ResultValue::ValueType::VALUE_ARRAY; - value->value_Object = GetChild(); - } - break; - case cJSON_Object: - { - value->value_type = ResultValue::ValueType::VALUE_OBJECT; - value->value_Object = GetChild(); - } - break; - default: - break; - } - return value; -} - -std::string CjsonObject::GetItemFiled() -{ - if (cjson_ == nullptr) { - GLOGE("cjson is null"); - return ""; - } - if (cjson_->string == nullptr) { - GLOGE("cjson string is null"); - return ""; - } - return cjson_->string; -} - -int CjsonObject::Delete() -{ - if (cjson_ != nullptr) { - cJSON_Delete(cjson_); - } - return E_OK; -} - -} \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/src/cjson_object.h b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/src/cjson_object.h deleted file mode 100644 index dcda3cb2..00000000 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/src/cjson_object.h +++ /dev/null @@ -1,44 +0,0 @@ -/* -* Copyright (c) 2023 Huawei Device Co., Ltd. -* 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 CJSON_OBJECT_H -#define CJSON_OBJECT_H - -#include -#include "cJSON.h" -#include "json_object.h" - -namespace DocumentDB { -class CjsonObject : public JsonObject{ -public: - CjsonObject () = default; - virtual ~CjsonObject () = default; - - int Parse(const std::string &str) override; - std::string Print() override; - CjsonObject* GetObjectItem(const std::string &field) override; - CjsonObject* GetArrayItem(const int index) override; - CjsonObject* GetNext(); - CjsonObject* GetChild(); - int DeleteItemFromObject(const std::string &field) override; - ResultValue* GetItemValue() override; - std::string GetItemFiled() override; - int Delete() override; -private: - cJSON *cjson_; -}; -} // DocumentDB -#endif // CJSON_OBJECT_H - diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/src/json_object.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/src/json_object.cpp new file mode 100644 index 00000000..6b719281 --- /dev/null +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/src/json_object.cpp @@ -0,0 +1,328 @@ +/* +* Copyright (c) 2023 Huawei Device Co., Ltd. +* 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 "doc_errno.h" +#include "log_print.h" +#include "grd_format_config.h" +#include "json_object.h" + + +namespace DocumentDB { + +JsonObject::JsonObject() +{ + cjson_ = nullptr; + isClone_ = true; +} + +JsonObject::JsonObject(const JsonObject& newObj) +{ + cjson_ = newObj.cjson_; + isClone_ = true; +} + +JsonObject::~JsonObject() +{ + if (isClone_ == false) { + cJSON_Delete(cjson_); + } +} + +bool JsonObject::IsNull() +{ + if (cjson_ == nullptr) { + return true; + } + return false; +} + +int JsonObject::GetDeep(cJSON *cjson, int deep, int &maxDeep) +{ + if (cjson->child == nullptr) { + maxDeep = std::max(deep, maxDeep); + } + if (cjson->child != nullptr) { + GetDeep(cjson->child, deep + 1, maxDeep); + } + if (cjson->next != nullptr) { + GetDeep(cjson->next, deep, maxDeep); + } + return E_OK; +} + +int JsonObject::Init(const std::string &str) +{ + if (str.length() + 1 > JSON_LENS_MAX) { + return E_INVALID_ARGS; + } + const char *end = NULL; + cjson_ = cJSON_ParseWithOpts(str.c_str(), &end, true); + if (cjson_ == nullptr) { + return E_INVALID_ARGS; + } + if (cjson_->type != cJSON_Object) { + return E_INVALID_ARGS; + } + int deep = 0; + GetDeep(cjson_, 0, deep); + if (deep > JSON_DEEP_MAX) { + return E_INVALID_ARGS; + } + isClone_ = false; + return E_OK; +} + +std::string JsonObject::Print() +{ + if (cjson_ == nullptr) { + return ""; + } + char *ret = cJSON_PrintUnformatted(cjson_); + std::string str = ret; + free(ret); + return str; +} + +JsonObject JsonObject::GetObjectItem(const std::string &field, bool caseSensitive) +{ + if (cjson_ == nullptr) { + return JsonObject(); + } + JsonObject item; + if (caseSensitive) { + cJSON *cjson_item = cJSON_GetObjectItemCaseSensitive(cjson_, field.c_str()); + } else { + cJSON *cjson_item = cJSON_GetObjectItem(cjson_, field.c_str()); + item.cjson_ = cjson_item; + } + return item; +} + +JsonObject JsonObject::GetArrayItem(const int index) +{ + if (cjson_ == nullptr) { + return JsonObject(); + } + JsonObject item; + auto cjson_item = cJSON_GetArrayItem(cjson_, index); + if (cjson_item == nullptr) { + return item; + } + item.cjson_ = cjson_item; + return item; +} + +JsonObject JsonObject::GetNext() +{ + if (cjson_ == nullptr) { + return JsonObject(); + } + JsonObject next; + if (cjson_->next == nullptr) { + return JsonObject(); + } + next.cjson_ = cjson_->next; + return next; +} + +JsonObject JsonObject::GetChild() +{ + if (cjson_ == nullptr) { + return JsonObject(); + } + JsonObject child; + if (cjson_->child == nullptr) { + return JsonObject(); + } + child.cjson_ = cjson_->child; + return child; +} + +int JsonObject::DeleteItemFromObject(const std::string &field) +{ + if (field == "") { + return E_OK; + } + cJSON_DeleteItemFromObjectCaseSensitive(cjson_, field.c_str()); + return E_OK; +} + +ResultValue JsonObject::GetItemValue() +{ + if (cjson_ == nullptr) { + return ResultValue(); + } + ResultValue value; + switch (cjson_->type) { + case cJSON_False: { + value.value_type = ResultValue::ValueType::VALUE_FALSE; + } + break; + case cJSON_True: + { + value.value_type = ResultValue::ValueType::VALUE_TRUE; + } + break; + case cJSON_NULL: + { + value.value_type = ResultValue::ValueType::VALUE_NULL; + } + break; + case cJSON_Number: + { + value.value_type = ResultValue::ValueType::VALUE_NUMBER; + value.value_number = cjson_->valuedouble; + } + break; + case cJSON_String: + { + value.value_type = ResultValue::ValueType::VALUE_STRING; + value.value_string = cjson_->valuestring; + } + break; + case cJSON_Array: + { + value.value_type = ResultValue::ValueType::VALUE_ARRAY; + if (GetChild().IsNull() != true) { + auto item = GetChild(); + value.value_Object = &item; + } + } + break; + case cJSON_Object: + { + value.value_type = ResultValue::ValueType::VALUE_OBJECT; + if (GetChild().IsNull() != true) { + auto item = GetChild(); + value.value_Object = &item; + } + } + break; + default: + break; + } + + return value; +} + +std::string JsonObject::GetItemFiled() +{ + if (cjson_ == nullptr) { + return ""; + } + if (cjson_->string == nullptr) { + return ""; + } + return cjson_->string; +} + +JsonObject JsonObject::FindItem(const std::vector json_path) +{ + cJSON *cJsondata_item = cjson_; + cJSON *cJsondata_temp; + if (json_path.size() != 0 && json_path[0] == "") { + return JsonObject(); + } + for (int i = 0; i < json_path.size(); i++) { + if (cJsondata_item->type == cJSON_Object) { + if (cJSON_GetObjectItem(cJsondata_item,json_path[i].c_str()) == nullptr) { + return JsonObject(); + } + cJsondata_temp = cJSON_GetObjectItem(cJsondata_item,json_path[i].c_str()); + cJsondata_item = cJsondata_temp; + } + if (cJsondata_item->type == cJSON_Array) { + int nums = 0; + for (int j = 0; j < json_path[i].size(); j++) { + if (json_path[i][j]-'0' > 9 || json_path[i][j] - '0' < 0 || + json_path[i][j] - '0' > cJSON_GetArraySize(cJsondata_item)) { + break; + } + auto GetArrayret = cJSON_GetArrayItem(cJsondata_item, json_path[i][j]-'0'); + if (typeid(GetArrayret) == typeid(cJSON*)) { + cJsondata_item = GetArrayret; + } + else { + if ( i != json_path.size() - 1) { + GLOGD("[cjson_object]=====>Patharg wrong"); + } + else { + return JsonObject(); + } + } + } + } + + } + JsonObject item; + item.cjson_ = cJsondata_item; + return item; +} + +int JsonObject::DeleteItemOnTarget(std::vector &path) +{ + cJSON *node_father; + cJSON *cJsondata_root; + std::string lastString; + if (path.size()>0) { + lastString = path.back(); + path.pop_back(); + if (path.size() == 0) { + std::vector emptyPath; + auto cjsonObj = FindItem(emptyPath); + if (cjsonObj.IsNull() == true) { + return E_OK; + } + node_father = cjsonObj.cjson_; + cJsondata_root = cjsonObj.cjson_; + path.emplace_back(lastString); + } + else { + std::vector forePath; + for (int i = 0; i < path.size() - 1; i++) { + forePath.emplace_back(path[i]); + } + auto foreObj = FindItem(forePath); + if (foreObj.IsNull() == true) { + return E_OK; + } + cJsondata_root = foreObj.cjson_; + auto cjsonObj = FindItem(path); + if (cjsonObj.IsNull() == true) { + return E_OK; + } + node_father = cjsonObj.cjson_; + path.emplace_back(lastString); + } + } + if (node_father->type == cJSON_Object) { + if (cJSON_GetObjectItem(node_father, lastString.c_str()) != nullptr) { + cJSON_DeleteItemFromObject(node_father, lastString.c_str()); + } + else { + GLOGE("no item that can be deleted"); + } + } + if (node_father->type == cJSON_Array) { + if (cJSON_GetArrayItem(node_father, lastString[0] - '0') != nullptr) { + cJSON_DeleteItemFromArray(node_father, lastString[0] - '0'); + } + else { + GLOGE("no item that can be deleted"); + } + } + return E_OK; +} +} \ No newline at end of file -- Gitee From 758bf898bde9862d8d5a1d1a0223c8b11bf0472b Mon Sep 17 00:00:00 2001 From: mazhao Date: Mon, 3 Apr 2023 07:35:24 +0000 Subject: [PATCH 2/2] change the name Signed-off-by: mazhao --- .../src/common/include/json_common.h | 4 +- .../src/common/src/json_common.cpp | 122 +++++++++--------- .../src/oh_adapter/include/json_object.h | 18 +-- .../src/oh_adapter/src/json_object.cpp | 100 +++++++------- 4 files changed, 121 insertions(+), 123 deletions(-) diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/common/include/json_common.h b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/common/include/json_common.h index 5936f7ad..745718c1 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/common/include/json_common.h +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/common/include/json_common.h @@ -30,11 +30,11 @@ public: static ResultValue GetValueByFiled(JsonObject *node, const std::string& filed); static bool CheckJsonField(const std::string &data); - static int ParseNode(JsonObject *Node, std::vector onePath, std::vector> &parsePath, bool isFirstFloor); + static int ParseNode(JsonObject *Node, std::vector singlePath, std::vector> &resultPath, bool isFirstFloor); static std::vector> ParsePath(JsonObject* node); static std::vector GetLeafValue(JsonObject *node); private: - static bool CheckNode(JsonObject *Node, std::set setString, bool &errflag); + static bool CheckNode(JsonObject *Node, std::set filedSet, bool &errFlag); static int CheckLeafNode(JsonObject *Node, std::vector &leafValue); }; } // DocumentDB diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/common/src/json_common.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/common/src/json_common.cpp index 16f835b7..a87c0570 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/common/src/json_common.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/common/src/json_common.cpp @@ -28,14 +28,14 @@ ResultValue JsonCommon::GetValueByFiled(JsonObject *node, const std::string& fil while (node != nullptr) { if (node->GetItemFiled() == filed) { - auto item_value = node->GetItemValue(); - return item_value; + auto itemValue = node->GetItemValue(); + return itemValue; } if (node->GetNext().IsNull() == true) { return ResultValue(); } - auto node_new = node->GetNext(); - node = &node_new; + auto nodeNew = node->GetNext(); + node = &nodeNew; } return ResultValue(); } @@ -43,19 +43,20 @@ ResultValue JsonCommon::GetValueByFiled(JsonObject *node, const std::string& fil int JsonCommon::CheckLeafNode(JsonObject *node, std::vector &leafValue) { if (node->GetChild().IsNull() == true) { - auto item_value = node->GetItemValue(); - leafValue.emplace_back(item_value); + auto itemValue = node->GetItemValue(); + leafValue.emplace_back(itemValue); } if (node->GetChild().IsNull() != true) { - auto node_new = node->GetChild(); - CheckLeafNode(&node_new, leafValue); + auto nodeNew = node->GetChild(); + CheckLeafNode(&nodeNew, leafValue); } if (node->GetNext().IsNull() != true) { - auto node_new = node->GetNext(); - CheckLeafNode(&node_new, leafValue); + auto nodeNew = node->GetNext(); + CheckLeafNode(&nodeNew, leafValue); } return E_OK; } + std::vector JsonCommon::GetLeafValue(JsonObject *node) { std::vector leafValue; @@ -63,37 +64,37 @@ std::vector JsonCommon::GetLeafValue(JsonObject *node) return leafValue; } -bool JsonCommon::CheckNode(JsonObject *node, std::set setString, bool &errflag) { - if (errflag == false) { +bool JsonCommon::CheckNode(JsonObject *node, std::set filedSet, bool &errFlag) { + if (errFlag == false) { return false; } - std::string field_str; - if (node->GetItemValue().value_type != ResultValue::ValueType::VALUE_NULL) { - field_str = node->GetItemFiled(); - if (setString.find(field_str) == setString.end()) { - setString.insert(field_str); + std::string fieldName; + if (node->GetItemValue().valueType != ResultValue::ValueType::VALUE_NULL) { + fieldName = node->GetItemFiled(); + if (filedSet.find(fieldName) == filedSet.end()) { + filedSet.insert(fieldName); } else { - errflag = false; + errFlag = false; return false; } - for (int i = 0; i < field_str.size(); i++) { - if (!(('a'<=field_str[i] && field_str[i]<='z')|| ('A'<=field_str[i] && field_str[i]<='Z') || ('0'<=field_str[i] && field_str[i]<='9') || '_' == field_str[i])) { - errflag = false; + for (int i = 0; i < fieldName.size(); i++) { + if (!(('a'<=fieldName[i] && fieldName[i]<='z')|| ('A'<=fieldName[i] && fieldName[i]<='Z') || ('0'<=fieldName[i] && fieldName[i]<='9') || '_' == fieldName[i])) { + errFlag = false; return false; } } } if (node->GetChild().IsNull() != true) { - auto node_new = node->GetChild(); - std::set stringSet_new; - CheckNode(&node_new, stringSet_new, errflag); + auto nodeNew = node->GetChild(); + std::set newFiledSet; + CheckNode(&nodeNew, newFiledSet, errFlag); } if (node->GetNext().IsNull() != true) { - auto node_new = node->GetNext(); - CheckNode(&node_new, setString, errflag); + auto nodeNew = node->GetNext(); + CheckNode(&nodeNew, filedSet, errFlag); } - return errflag; + return errFlag; } bool JsonCommon::CheckJsonField(const std::string &data) { @@ -101,61 +102,58 @@ bool JsonCommon::CheckJsonField(const std::string &data) { if (jsonObj.Init(data) != E_OK) { return false; } - std::set stringSet; - bool errflag = true; - return CheckNode(&jsonObj, stringSet, errflag); + std::set filedSet; + bool errFlag = true; + return CheckNode(&jsonObj, filedSet, errFlag); } -int JsonCommon::ParseNode(JsonObject* node, std::vector onePath, std::vector> &parsePath, bool isFirstFloor) +int JsonCommon::ParseNode(JsonObject* node, std::vector singlePath, std::vector> &resultPath, bool isFirstFloor) { - std::vector forePath; + std::vector fatherPath; if (isFirstFloor) { - std::string tempparse_name; - std::vector parsed_mixfiled_name; - std::string mixfield_name = node->GetItemFiled(); - for (int j = 0; j < mixfield_name.size(); j++) { - if (mixfield_name[j] != '.') { - tempparse_name = tempparse_name + mixfield_name[j]; + std::string tempParseName; + std::vector allFiledsName; + std::string priFieldName = node->GetItemFiled(); + for (int j = 0; j < priFieldName.size(); j++) { + if (priFieldName[j] != '.') { + tempParseName = tempParseName + priFieldName[j]; } - if (mixfield_name[j] == '.' || j == mixfield_name.size() - 1) { - parsed_mixfiled_name.emplace_back(tempparse_name); - tempparse_name.clear(); + if (priFieldName[j] == '.' || j == priFieldName.size() - 1) { + allFiledsName.emplace_back(tempParseName); + tempParseName.clear(); } } - forePath = onePath; - onePath.insert(onePath.end(), parsed_mixfiled_name.begin(), parsed_mixfiled_name.end()); + fatherPath = singlePath; + singlePath.insert(singlePath.end(), allFiledsName.begin(), allFiledsName.end()); } else { - std::vector parsed_mixfiled_name; - parsed_mixfiled_name.emplace_back(node->GetItemFiled()); - forePath = onePath; - onePath.insert(onePath.end(), parsed_mixfiled_name.begin(), parsed_mixfiled_name.end()); + std::vector allFiledsName; + allFiledsName.emplace_back(node->GetItemFiled()); + fatherPath = singlePath; + singlePath.insert(singlePath.end(), allFiledsName.begin(), allFiledsName.end()); } if (node->GetChild().IsNull() != true && node->GetChild().GetItemFiled() != "") { - auto node_new = node->GetChild(); - ParseNode(&node_new, onePath, parsePath, false); + auto nodeNew = node->GetChild(); + ParseNode(&nodeNew, singlePath, resultPath, false); } else { - parsePath.emplace_back(onePath); + resultPath.emplace_back(singlePath); } if (node->GetNext().IsNull() != true) { - auto node_new = node->GetNext(); - ParseNode(&node_new, forePath, parsePath, isFirstFloor); + auto nodeNew = node->GetNext(); + ParseNode(&nodeNew, fatherPath, resultPath, isFirstFloor); } return 0; } std::vector> JsonCommon::ParsePath(JsonObject* root) { - std::vector> parsePath; - auto projection_json = root->GetChild(); - if (projection_json.IsNull() == true) { - GLOGE("projection_json is null"); + std::vector> resultPath; + auto projectionJson = root->GetChild(); + if (projectionJson.IsNull() == true) { + GLOGE("projectionJson is null"); } - std::vector onePath; - ParseNode(&projection_json, onePath, parsePath, true); - return parsePath; + std::vector singlePath; + ParseNode(&projectionJson, singlePath, resultPath, true); + return resultPath; } - - - } // namespace DocumentDB \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/include/json_object.h b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/include/json_object.h index 925299fd..9b1c4119 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/include/json_object.h +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/include/json_object.h @@ -28,10 +28,10 @@ class ResultValue { public: ResultValue() {}; ResultValue(const ResultValue& newValue) { - value_number = newValue.value_number; - value_string = newValue.value_string; - value_type = newValue.value_type; - value_Object = newValue.value_Object; + valueNumber = newValue.valueNumber; + valueString = newValue.valueString; + valueType = newValue.valueType; + valueObject = newValue.valueObject; } enum class ValueType { VALUE_FALSE, @@ -42,10 +42,10 @@ public: VALUE_ARRAY, VALUE_OBJECT }; - int value_number; - std::string value_string; - ValueType value_type = ValueType::VALUE_NULL; - JsonObject *value_Object = nullptr; + int valueNumber; + std::string valueString; + ValueType valueType = ValueType::VALUE_NULL; + JsonObject *valueObject = nullptr; }; class JsonObject { @@ -63,7 +63,7 @@ public: int DeleteItemFromObject(const std::string &field); ResultValue GetItemValue(); std::string GetItemFiled(); - JsonObject FindItem(const std::vector json_path); + JsonObject FindItem(const std::vector jsonPath); int DeleteItemOnTarget(std::vector &path); bool IsNull(); private: diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/src/json_object.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/src/json_object.cpp index 6b719281..19a75a55 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/src/json_object.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/src/json_object.cpp @@ -102,10 +102,10 @@ JsonObject JsonObject::GetObjectItem(const std::string &field, bool caseSensitiv } JsonObject item; if (caseSensitive) { - cJSON *cjson_item = cJSON_GetObjectItemCaseSensitive(cjson_, field.c_str()); + cJSON *cjsonItem = cJSON_GetObjectItemCaseSensitive(cjson_, field.c_str()); } else { - cJSON *cjson_item = cJSON_GetObjectItem(cjson_, field.c_str()); - item.cjson_ = cjson_item; + cJSON *cjsonItem = cJSON_GetObjectItem(cjson_, field.c_str()); + item.cjson_ = cjsonItem; } return item; } @@ -116,11 +116,11 @@ JsonObject JsonObject::GetArrayItem(const int index) return JsonObject(); } JsonObject item; - auto cjson_item = cJSON_GetArrayItem(cjson_, index); - if (cjson_item == nullptr) { + auto cjsonItem = cJSON_GetArrayItem(cjson_, index); + if (cjsonItem == nullptr) { return item; } - item.cjson_ = cjson_item; + item.cjson_ = cjsonItem; return item; } @@ -167,46 +167,46 @@ ResultValue JsonObject::GetItemValue() ResultValue value; switch (cjson_->type) { case cJSON_False: { - value.value_type = ResultValue::ValueType::VALUE_FALSE; + value.valueType = ResultValue::ValueType::VALUE_FALSE; } break; case cJSON_True: { - value.value_type = ResultValue::ValueType::VALUE_TRUE; + value.valueType = ResultValue::ValueType::VALUE_TRUE; } break; case cJSON_NULL: { - value.value_type = ResultValue::ValueType::VALUE_NULL; + value.valueType = ResultValue::ValueType::VALUE_NULL; } break; case cJSON_Number: { - value.value_type = ResultValue::ValueType::VALUE_NUMBER; - value.value_number = cjson_->valuedouble; + value.valueType = ResultValue::ValueType::VALUE_NUMBER; + value.valueNumber = cjson_->valuedouble; } break; case cJSON_String: { - value.value_type = ResultValue::ValueType::VALUE_STRING; - value.value_string = cjson_->valuestring; + value.valueType = ResultValue::ValueType::VALUE_STRING; + value.valueString = cjson_->valuestring; } break; case cJSON_Array: { - value.value_type = ResultValue::ValueType::VALUE_ARRAY; + value.valueType = ResultValue::ValueType::VALUE_ARRAY; if (GetChild().IsNull() != true) { auto item = GetChild(); - value.value_Object = &item; + value.valueObject = &item; } } break; case cJSON_Object: { - value.value_type = ResultValue::ValueType::VALUE_OBJECT; + value.valueType = ResultValue::ValueType::VALUE_OBJECT; if (GetChild().IsNull() != true) { auto item = GetChild(); - value.value_Object = &item; + value.valueObject = &item; } } break; @@ -228,34 +228,34 @@ std::string JsonObject::GetItemFiled() return cjson_->string; } -JsonObject JsonObject::FindItem(const std::vector json_path) +JsonObject JsonObject::FindItem(const std::vector jsonPath) { - cJSON *cJsondata_item = cjson_; - cJSON *cJsondata_temp; - if (json_path.size() != 0 && json_path[0] == "") { + cJSON *cJsondataItem = cjson_; + if (jsonPath.size() != 0 && jsonPath[0] == "") { return JsonObject(); } - for (int i = 0; i < json_path.size(); i++) { - if (cJsondata_item->type == cJSON_Object) { - if (cJSON_GetObjectItem(cJsondata_item,json_path[i].c_str()) == nullptr) { + for (int i = 0; i < jsonPath.size(); i++) { + if (cJsondataItem->type == cJSON_Object) { + if (cJSON_GetObjectItem(cJsondataItem,jsonPath[i].c_str()) == nullptr) { return JsonObject(); } - cJsondata_temp = cJSON_GetObjectItem(cJsondata_item,json_path[i].c_str()); - cJsondata_item = cJsondata_temp; + cJSON *cJsondataTemp; + cJsondataTemp = cJSON_GetObjectItem(cJsondataItem,jsonPath[i].c_str()); + cJsondataItem = cJsondataTemp; } - if (cJsondata_item->type == cJSON_Array) { + if (cJsondataItem->type == cJSON_Array) { int nums = 0; - for (int j = 0; j < json_path[i].size(); j++) { - if (json_path[i][j]-'0' > 9 || json_path[i][j] - '0' < 0 || - json_path[i][j] - '0' > cJSON_GetArraySize(cJsondata_item)) { + for (int j = 0; j < jsonPath[i].size(); j++) { + if (jsonPath[i][j]-'0' > 9 || jsonPath[i][j] - '0' < 0 || + jsonPath[i][j] - '0' > cJSON_GetArraySize(cJsondataItem)) { break; } - auto GetArrayret = cJSON_GetArrayItem(cJsondata_item, json_path[i][j]-'0'); - if (typeid(GetArrayret) == typeid(cJSON*)) { - cJsondata_item = GetArrayret; + auto GetArrayRet = cJSON_GetArrayItem(cJsondataItem, jsonPath[i][j]-'0'); + if (typeid(GetArrayRet) == typeid(cJSON*)) { + cJsondataItem = GetArrayRet; } else { - if ( i != json_path.size() - 1) { + if ( i != jsonPath.size() - 1) { GLOGD("[cjson_object]=====>Patharg wrong"); } else { @@ -267,14 +267,14 @@ JsonObject JsonObject::FindItem(const std::vector json_path) } JsonObject item; - item.cjson_ = cJsondata_item; + item.cjson_ = cJsondataItem; return item; } int JsonObject::DeleteItemOnTarget(std::vector &path) { - cJSON *node_father; - cJSON *cJsondata_root; + cJSON *nodeFather; + cJSON *cJsondataRoot; std::string lastString; if (path.size()>0) { lastString = path.back(); @@ -285,39 +285,39 @@ int JsonObject::DeleteItemOnTarget(std::vector &path) if (cjsonObj.IsNull() == true) { return E_OK; } - node_father = cjsonObj.cjson_; - cJsondata_root = cjsonObj.cjson_; + nodeFather = cjsonObj.cjson_; + cJsondataRoot = cjsonObj.cjson_; path.emplace_back(lastString); } else { - std::vector forePath; + std::vector fatherPath; for (int i = 0; i < path.size() - 1; i++) { - forePath.emplace_back(path[i]); + fatherPath.emplace_back(path[i]); } - auto foreObj = FindItem(forePath); + auto foreObj = FindItem(fatherPath); if (foreObj.IsNull() == true) { return E_OK; } - cJsondata_root = foreObj.cjson_; + cJsondataRoot = foreObj.cjson_; auto cjsonObj = FindItem(path); if (cjsonObj.IsNull() == true) { return E_OK; } - node_father = cjsonObj.cjson_; + nodeFather = cjsonObj.cjson_; path.emplace_back(lastString); } } - if (node_father->type == cJSON_Object) { - if (cJSON_GetObjectItem(node_father, lastString.c_str()) != nullptr) { - cJSON_DeleteItemFromObject(node_father, lastString.c_str()); + if (nodeFather->type == cJSON_Object) { + if (cJSON_GetObjectItem(nodeFather, lastString.c_str()) != nullptr) { + cJSON_DeleteItemFromObject(nodeFather, lastString.c_str()); } else { GLOGE("no item that can be deleted"); } } - if (node_father->type == cJSON_Array) { - if (cJSON_GetArrayItem(node_father, lastString[0] - '0') != nullptr) { - cJSON_DeleteItemFromArray(node_father, lastString[0] - '0'); + if (nodeFather->type == cJSON_Array) { + if (cJSON_GetArrayItem(nodeFather, lastString[0] - '0') != nullptr) { + cJSON_DeleteItemFromArray(nodeFather, lastString[0] - '0'); } else { GLOGE("no item that can be deleted"); -- Gitee