From 40d50bf12560c4a66ac2d67c5c1af99baf235726 Mon Sep 17 00:00:00 2001 From: mazhao Date: Mon, 27 Mar 2023 08:37:26 +0000 Subject: [PATCH] json supply Signed-off-by: mazhao --- .../src/common/include/doc_common.h | 14 ++ .../src/common/include/json_common.h | 37 ++++ .../src/common/src/doc_common.cpp | 67 +++++++ .../src/common/src/json_common.cpp | 70 +++++++ .../src/oh_adapter/include/json_object.h | 56 ++++++ .../src/oh_adapter/json_operator.h | 0 .../src/oh_adapter/src/cjson_object.cpp | 181 ++++++++++++++++++ .../src/oh_adapter/src/cjson_object.h | 44 +++++ 8 files changed, 469 insertions(+) create mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/common/include/json_common.h create mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/common/src/doc_common.cpp create mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/common/src/json_common.cpp create mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/include/json_object.h delete mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/json_operator.h create mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/src/cjson_object.cpp create mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/src/cjson_object.h diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/common/include/doc_common.h b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/common/include/doc_common.h index a4919b67..24065eda 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/common/include/doc_common.h +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/common/include/doc_common.h @@ -18,9 +18,23 @@ #include #include +#include "json_common.h" +class JsonCommon; namespace DocumentDB { +class CheckCommon +{ +public: + CheckCommon() = default; + ~CheckCommon(); + + static bool CheckCollectionName(const std::string &collectionName); + static bool CheckFilter(const std::string &filter); + static bool CheckIdFormat(const std::string &data); + static bool CheckDocument(const std::string &document); +}; using Key = std::vector; using Value = std::vector; + } // DocumentDB #endif // DOC_COMMON_H \ No newline at end of file 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 new file mode 100644 index 00000000..22e19391 --- /dev/null +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/common/include/json_common.h @@ -0,0 +1,37 @@ +/* +* 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 JSON_COMMON_H +#define JSON_COMMON_H + +#include +#include +#include "cjson_object.h" + +namespace DocumentDB { +class JsonCommon +{ +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); +}; + +} // 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/doc_common.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/common/src/doc_common.cpp new file mode 100644 index 00000000..fa39bd34 --- /dev/null +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/common/src/doc_common.cpp @@ -0,0 +1,67 @@ +/* + * 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 + +#include "doc_common.h" +#include "doc_errno.h" +#include "log_print.h" +#include "securec.h" + +namespace DocumentDB { +bool CheckCommon::CheckCollectionName(const std::string &collectionName) +{ + if (collectionName.empty()) { + return false; + } + if (collectionName.length() > 512) { + return false; + } + if (collectionName.compare(0, 4, "GRD", 0, 4) == 0 || collectionName.compare(0, 7, "GM_SYS_", 0, 7) == 0) { + return false; + } + return true; +} + +bool CheckCommon::CheckFilter(const std::string &filter) +{ + if (JsonCommon::CheckIsJson(filter) == false) { + return false; + } + if (JsonCommon::GetJsonDeep(filter) > 4) { + return false; + } + if (CheckIdFormat(filter) == false) { + return false; + } + return true; +} + +bool CheckCommon::CheckIdFormat(const std::string &data) +{ + CjsonObject filter_json; + filter_json.Parse(data); + std::vector id; + if (JsonCommon::GetIdValue(&filter_json, id) == E_ERROR) { + return false; + } + return true; +} + +bool CheckCommon::CheckDocument(const std::string &document) +{ + return true; +} + +} // namespace DocumentDB \ 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 new file mode 100644 index 00000000..49bccc16 --- /dev/null +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/common/src/json_common.cpp @@ -0,0 +1,70 @@ +/* + * 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 + +#include "json_common.h" +#include "doc_errno.h" +#include "log_print.h" +#include "securec.h" + +namespace DocumentDB { +ResultValue* JsonCommon::GetValue(CjsonObject *root, std::vector path) +{ + return nullptr; +} + +int JsonCommon::GetIdValue(CjsonObject *root, std::vector &id) +{ + auto &node = root; + if (root == nullptr) { + return E_OK; + } + 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); + } + node = node->GetNext(); + } + return E_OK; +} + +bool JsonCommon::CheckIsJson(const std::string &data) { + CjsonObject cjsonObj; + if (cjsonObj.Parse(data) == E_ERROR) { + return false; + } + return true; +} + +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--; + } + } + return deep; +} + +} // 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 new file mode 100644 index 00000000..f0ebd8fd --- /dev/null +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/include/json_object.h @@ -0,0 +1,56 @@ +/* +* 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 JSON_OBJECT_H +#define JSON_OBJECT_H + +#include + +namespace DocumentDB { + +class JsonObject; +class ResultValue { +public: + enum class ValueType { + VALUE_FALSE, + VALUE_TRUE, + VALUE_NULL, + VALUE_NUMBER, + VALUE_STRING, + VALUE_ARRAY, + VALUE_OBJECT + }; + int value_number; + std::string value_string; + ValueType value_type = ValueType::VALUE_NULL; + JsonObject *value_Object = nullptr; +}; + +class JsonObject { +public: + JsonObject () = default; + virtual ~JsonObject () = default; + + 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; +}; +} // DocumentDB +#endif // JSON_OBJECT_H \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/json_operator.h b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/json_operator.h deleted file mode 100644 index e69de29b..00000000 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 new file mode 100644 index 00000000..05eb385b --- /dev/null +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/src/cjson_object.cpp @@ -0,0 +1,181 @@ +/* +* 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 new file mode 100644 index 00000000..dcda3cb2 --- /dev/null +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/src/cjson_object.h @@ -0,0 +1,44 @@ +/* +* 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 + -- Gitee