From f4fb51a50fb8cc6bb0282ad19baccb4bae896a1d Mon Sep 17 00:00:00 2001 From: zqq Date: Mon, 19 May 2025 17:20:46 +0800 Subject: [PATCH 1/5] get last match item in cjson Signed-off-by: zqq --- .../common/include/cjson_object.h | 1 + .../distributeddb/common/src/cjson_object.cpp | 17 +++++++++++- .../distributeddb_json_precheck_unit_test.cpp | 27 +++++++++++++++++++ 3 files changed, 44 insertions(+), 1 deletion(-) diff --git a/frameworks/libs/distributeddb/common/include/cjson_object.h b/frameworks/libs/distributeddb/common/include/cjson_object.h index 57cb90d8b..6f7578e09 100644 --- a/frameworks/libs/distributeddb/common/include/cjson_object.h +++ b/frameworks/libs/distributeddb/common/include/cjson_object.h @@ -63,6 +63,7 @@ public: void Append(const CJsonObject &object); void RemoveMember(const std::string &path); void InsertOrReplaceField(const std::string &field, const CJsonObject &value); + cJSON *GetObjectItem(const std::string &key) const; // copy cjson ptr only, not copy content static CJsonObject CreateTempCJsonObject(const CJsonObject &object); diff --git a/frameworks/libs/distributeddb/common/src/cjson_object.cpp b/frameworks/libs/distributeddb/common/src/cjson_object.cpp index d74810590..4bc9220de 100644 --- a/frameworks/libs/distributeddb/common/src/cjson_object.cpp +++ b/frameworks/libs/distributeddb/common/src/cjson_object.cpp @@ -92,10 +92,25 @@ CJsonObject CJsonObject::operator[](const std::string &key) const obj.isOwner_ = false; return obj; } - auto rawCJsonPtr = cJSON_GetObjectItem(cjson_, key.c_str()); + // default cjson get first item when match + // we should keep it same as json cpp which is getting last item + auto rawCJsonPtr = GetObjectItem(key); return CJsonObject(rawCJsonPtr, false); } +cJSON *CJsonObject::GetObjectItem(const std::string &key) const +{ + cJSON *res = nullptr; + auto current = cjson_->child; + while (current != nullptr) { + if (current->string != nullptr && strcmp(current->string, key.c_str()) == 0) { + res = current; + } + current = current->next; + } + return res; +} + CJsonObject CJsonObject::operator[](int index) const { if (cjson_ == nullptr || !cJSON_IsArray(cjson_)) { diff --git a/frameworks/libs/distributeddb/test/unittest/common/common/distributeddb_json_precheck_unit_test.cpp b/frameworks/libs/distributeddb/test/unittest/common/common/distributeddb_json_precheck_unit_test.cpp index 3778b5f24..ad2964a66 100644 --- a/frameworks/libs/distributeddb/test/unittest/common/common/distributeddb_json_precheck_unit_test.cpp +++ b/frameworks/libs/distributeddb/test/unittest/common/common/distributeddb_json_precheck_unit_test.cpp @@ -204,4 +204,31 @@ HWTEST_F(DistributedDBJsonPrecheckUnitTest, ParseInvalidString003, TestSize.Leve int stepTwo = tempObj.Parse(JSON_STRING5); EXPECT_TRUE(stepTwo != E_OK); } + +/** + * @tc.name: ParseDuplicativeString001 + * @tc.desc: The json string has more than one field with same name. + * @tc.type: FUNC + * @tc.require: + * @tc.author: zqq + */ +HWTEST_F(DistributedDBJsonPrecheckUnitTest, ParseDuplicativeString001, TestSize.Level0) +{ + /** + * @tc.steps: step1. Parse json. + * @tc.expected: step1. return ok. + */ + std::string json = R"({"field1":"123", "field2": true, "field1": 123})"; + JsonObject object; + ASSERT_EQ(object.JsonObject::Parse(json), E_OK); + /** + * @tc.steps: step2. Check field. + * @tc.expected: step2. field1 is 123, field2 is True. + */ + FieldValue value; + ASSERT_EQ(object.GetFieldValueByFieldPath({"field1"}, value), E_OK); + EXPECT_EQ(value.integerValue, 123); + ASSERT_EQ(object.GetFieldValueByFieldPath({"field2"}, value), E_OK); + EXPECT_TRUE(value.boolValue); +} #endif \ No newline at end of file -- Gitee From a52e5bffe8b2be996811565cb50aa58839971206 Mon Sep 17 00:00:00 2001 From: zqq Date: Tue, 20 May 2025 09:09:28 +0800 Subject: [PATCH 2/5] fix compile error Signed-off-by: zqq --- .../common/include/cjson_object.h | 4 +- .../distributeddb/common/src/cjson_object.cpp | 11 +- .../distributeddb/common/src/json_object.cpp | 7 - .../distributeddb_json_precheck_unit_test.cpp | 125 ++++++++++++++++++ kvstoremock/distributeddb/BUILD.gn | 6 +- 5 files changed, 133 insertions(+), 20 deletions(-) diff --git a/frameworks/libs/distributeddb/common/include/cjson_object.h b/frameworks/libs/distributeddb/common/include/cjson_object.h index 6f7578e09..7e5a4a974 100644 --- a/frameworks/libs/distributeddb/common/include/cjson_object.h +++ b/frameworks/libs/distributeddb/common/include/cjson_object.h @@ -23,7 +23,7 @@ #include "db_types.h" namespace DistributedDB { -class CJsonObject { +class CJsonObject final { public: CJsonObject() = default; ~CJsonObject(); @@ -43,7 +43,7 @@ public: [[nodiscard]] std::string GetStringValue() const; [[nodiscard]] bool GetBoolValue() const; [[nodiscard]] int32_t GetInt32Value() const; - [[nodiscard]] int32_t GetInt64Value() const; + [[nodiscard]] int64_t GetInt64Value() const; [[nodiscard]] double GetDoubleValue() const; [[nodiscard]] std::vector GetMemberNames() const; diff --git a/frameworks/libs/distributeddb/common/src/cjson_object.cpp b/frameworks/libs/distributeddb/common/src/cjson_object.cpp index 4bc9220de..67df0e0b1 100644 --- a/frameworks/libs/distributeddb/common/src/cjson_object.cpp +++ b/frameworks/libs/distributeddb/common/src/cjson_object.cpp @@ -170,7 +170,7 @@ int32_t CJsonObject::GetInt32Value() const return static_cast(val); } -int32_t CJsonObject::GetInt64Value() const +int64_t CJsonObject::GetInt64Value() const { if (!IsInt64()) { return 0; @@ -291,12 +291,9 @@ void CJsonObject::Append(const CJsonObject &object) if (!IsArray()) { return; } - if (isOwner_) { - CJsonObject cpyObject = object; - cJSON_AddItemToArray(cjson_, cpyObject.cjson_); - } else { - cJSON_AddItemToArray(cjson_, object.cjson_); - } + CJsonObject cpyObject = object; + cJSON_AddItemToArray(cjson_, cpyObject.cjson_); + cpyObject.cjson_ = nullptr; } void CJsonObject::RemoveMember(const std::string &path) diff --git a/frameworks/libs/distributeddb/common/src/json_object.cpp b/frameworks/libs/distributeddb/common/src/json_object.cpp index dfc9e224d..2121f9caa 100644 --- a/frameworks/libs/distributeddb/common/src/json_object.cpp +++ b/frameworks/libs/distributeddb/common/src/json_object.cpp @@ -26,13 +26,6 @@ namespace DistributedDB { #ifndef OMIT_JSON namespace { const uint32_t MAX_NEST_DEPTH = 100; -#ifdef JSONCPP_USE_BUILDER - const int JSON_VALUE_PRECISION = 16; - const std::string JSON_CONFIG_INDENTATION = "indentation"; - const std::string JSON_CONFIG_COLLECT_COMMENTS = "collectComments"; - const std::string JSON_CONFIG_PRECISION = "precision"; - const std::string JSON_CONFIG_REJECT_DUP_KEYS = "rejectDupKeys"; -#endif } uint32_t JsonObject::maxNestDepth_ = MAX_NEST_DEPTH; diff --git a/frameworks/libs/distributeddb/test/unittest/common/common/distributeddb_json_precheck_unit_test.cpp b/frameworks/libs/distributeddb/test/unittest/common/common/distributeddb_json_precheck_unit_test.cpp index ad2964a66..22024dd44 100644 --- a/frameworks/libs/distributeddb/test/unittest/common/common/distributeddb_json_precheck_unit_test.cpp +++ b/frameworks/libs/distributeddb/test/unittest/common/common/distributeddb_json_precheck_unit_test.cpp @@ -231,4 +231,129 @@ HWTEST_F(DistributedDBJsonPrecheckUnitTest, ParseDuplicativeString001, TestSize. ASSERT_EQ(object.GetFieldValueByFieldPath({"field2"}, value), E_OK); EXPECT_TRUE(value.boolValue); } + +/** + * @tc.name: ParseString001 + * @tc.desc: Parse none obj json string. + * @tc.type: FUNC + * @tc.require: + * @tc.author: zqq + */ +HWTEST_F(DistributedDBJsonPrecheckUnitTest, ParseString001, TestSize.Level0) +{ + std::string nonJson = R"("field1":123)"; + JsonObject object; + std::vector data(nonJson.begin(), nonJson.end()); + EXPECT_EQ(object.Parse(data), -E_JSON_PARSE_FAIL); + EXPECT_TRUE(object.ToString().empty()); +} + +/** + * @tc.name: ParseString002 + * @tc.desc: Parse double and long. + * @tc.type: FUNC + * @tc.require: + * @tc.author: zqq + */ +HWTEST_F(DistributedDBJsonPrecheckUnitTest, ParseString002, TestSize.Level0) +{ + std::string json = R"({"field1":429496729500, "field2":11.1})"; + JsonObject object; + std::vector data(json.begin(), json.end()); + EXPECT_EQ(object.Parse(data), E_OK); + FieldValue value; + EXPECT_EQ(object.GetFieldValueByFieldPath({"field1"}, value), E_OK); + EXPECT_EQ(value.longValue, 429496729500); + EXPECT_EQ(object.GetFieldValueByFieldPath({"field2"}, value), E_OK); + EXPECT_NE(value.doubleValue, 0); +} + +/** + * @tc.name: ParseString003 + * @tc.desc: Parse obj. + * @tc.type: FUNC + * @tc.require: + * @tc.author: zqq + */ +HWTEST_F(DistributedDBJsonPrecheckUnitTest, ParseString003, TestSize.Level0) +{ + std::string json = R"({"field1":42949672950, "field2":{"field3":123}})"; + JsonObject object; + std::vector data(json.begin(), json.end()); + EXPECT_EQ(object.Parse(data), E_OK); + std::set outSubPath; + EXPECT_EQ(object.GetSubFieldPath({"field1"}, outSubPath), -E_NOT_SUPPORT); + EXPECT_EQ(object.GetSubFieldPath({"field2"}, outSubPath), E_OK); + std::set actualPath = {{"field2", "field3"}}; + EXPECT_EQ(outSubPath, actualPath); +} + +/** + * @tc.name: ParseString004 + * @tc.desc: Parse array. + * @tc.type: FUNC + * @tc.require: + * @tc.author: zqq + */ +HWTEST_F(DistributedDBJsonPrecheckUnitTest, ParseString004, TestSize.Level0) +{ + std::string json = R"({"field1":["123"], "field3":"field3"})"; + JsonObject object; + std::vector data(json.begin(), json.end()); + EXPECT_EQ(object.Parse(data), E_OK); + uint32_t size = 0u; + EXPECT_EQ(object.GetArraySize({"field3"}, size), -E_NOT_SUPPORT); + EXPECT_EQ(object.GetArraySize({"field1"}, size), E_OK); + EXPECT_EQ(size, 1u); + std::vector> content; + EXPECT_EQ(object.GetArrayContentOfStringOrStringArray({"field1"}, content), E_OK); + std::vector> actual = {{"123"}}; + EXPECT_EQ(content, actual); +} + +/** + * @tc.name: BuildObj001 + * @tc.desc: Build json obj. + * @tc.type: FUNC + * @tc.require: + * @tc.author: zqq + */ +HWTEST_F(DistributedDBJsonPrecheckUnitTest, BuildObj001, TestSize.Level0) +{ + JsonObject obj; + FieldValue val; + val.boolValue = true; + EXPECT_EQ(obj.InsertField({"bool_field"}, FieldType::LEAF_FIELD_BOOL, val), E_OK); + val.stringValue = "str"; + EXPECT_EQ(obj.InsertField({"str_field"}, FieldType::LEAF_FIELD_STRING, val), E_OK); + val.integerValue = INT32_MAX; + EXPECT_EQ(obj.InsertField({"int_field"}, FieldType::LEAF_FIELD_INTEGER, val), E_OK); + val.longValue = INT64_MAX; + EXPECT_EQ(obj.InsertField({"long_field"}, FieldType::LEAF_FIELD_LONG, val), E_OK); + val.doubleValue = 3.1415; + EXPECT_EQ(obj.InsertField({"double_field"}, FieldType::LEAF_FIELD_DOUBLE, val), E_OK); + EXPECT_EQ(obj.InsertField({"null_field"}, FieldType::LEAF_FIELD_NULL, val), E_OK); + EXPECT_EQ(obj.InsertField({"array_field"}, FieldType::LEAF_FIELD_ARRAY, val), -E_INVALID_ARGS); + EXPECT_EQ(obj.InsertField({"obj_field"}, FieldType::LEAF_FIELD_OBJECT, val), E_OK); + EXPECT_EQ(obj.DeleteField({"obj_field"}), E_OK); + LOGI("json is %s", obj.ToString().c_str()); +} + +/** + * @tc.name: BuildObj002 + * @tc.desc: Build json obj. + * @tc.type: FUNC + * @tc.require: + * @tc.author: zqq + */ +HWTEST_F(DistributedDBJsonPrecheckUnitTest, BuildObj002, TestSize.Level0) +{ + std::string json = R"({"array_field":[]})"; + JsonObject obj; + EXPECT_EQ(obj.Parse(json), E_OK); + FieldValue val; + val.stringValue = "str"; + EXPECT_EQ(obj.InsertField({"array_field"}, FieldType::LEAF_FIELD_STRING, val, true), E_OK); + LOGI("json is %s", obj.ToString().c_str()); +} #endif \ No newline at end of file diff --git a/kvstoremock/distributeddb/BUILD.gn b/kvstoremock/distributeddb/BUILD.gn index eae693d29..c55220069 100644 --- a/kvstoremock/distributeddb/BUILD.gn +++ b/kvstoremock/distributeddb/BUILD.gn @@ -94,7 +94,6 @@ config("distrdb_config") { visibility = [ ":*" ] include_dirs = distrdb_Dir include_dirs += [ - "//third_party/jsoncpp/include", "//third_party/zlib", "//third_party/sqlite/include", "//third_party/bounds_checking_function/include", @@ -123,7 +122,7 @@ ohos_shared_library("distributeddb_mock") { ldflags = [ "-v" ] deps = [ "//third_party/bounds_checking_function:libsec_static", - "//third_party/jsoncpp:jsoncpp_static", + "//third_party/cJSON:cjson", "//third_party/openssl:libcrypto_static", "//third_party/sqlite:sqlite_sdk", "//third_party/zlib:shared_libz", @@ -137,8 +136,7 @@ ohos_shared_library("distributeddb_mock") { deps += [ "//base/hiviewdfx/hilog/interfaces/native/innerkits:libhilog_mac" ] } - configs += [ "//third_party/jsoncpp:jsoncpp_config" ] - + cflags = [ "-Wno-c99-designator" ] subsystem_name = "distributeddatamgr" part_name = "kv_store" } -- Gitee From 3f5fd0299131dcaa95a9badcd739dd4a79ef4337 Mon Sep 17 00:00:00 2001 From: zqq Date: Wed, 21 May 2025 15:54:32 +0800 Subject: [PATCH 3/5] add ut Signed-off-by: zqq --- .../common/include/json_object.h | 5 +- .../distributeddb_json_precheck_unit_test.cpp | 69 ++++++++++++++++++- 2 files changed, 69 insertions(+), 5 deletions(-) diff --git a/frameworks/libs/distributeddb/common/include/json_object.h b/frameworks/libs/distributeddb/common/include/json_object.h index 96ce443d7..4c0188d43 100644 --- a/frameworks/libs/distributeddb/common/include/json_object.h +++ b/frameworks/libs/distributeddb/common/include/json_object.h @@ -92,14 +92,15 @@ public: // Should be called on an valid JsonObject. Never turn into invalid after call. An empty inPath is not allowed. // If inPath not exist, returns -E_JSON_DELETE_PATH_NOT_FOUND. Otherwise, delete field from its parent returns E_OK; int DeleteField(const FieldPath &inPath); -private: #ifndef OMIT_JSON // Auxiliary Method: If inPath not refer to an array, return error. If not all members are string, return error. int GetStringArrayContentByCJsonValue(const CJsonObject &value, std::vector &outStringArray) const; // Common Type Judgement Logic int GetFieldTypeByCJsonValue(const CJsonObject &value, FieldType &outType) const; - +#endif +private: +#ifndef OMIT_JSON // Return E_OK if JsonValueNode found at exact the path, otherwise not E_OK CJsonObject GetCJsonValueByFieldPath(const FieldPath &inPath, int &errCode) const; diff --git a/frameworks/libs/distributeddb/test/unittest/common/common/distributeddb_json_precheck_unit_test.cpp b/frameworks/libs/distributeddb/test/unittest/common/common/distributeddb_json_precheck_unit_test.cpp index 22024dd44..4f50c2273 100644 --- a/frameworks/libs/distributeddb/test/unittest/common/common/distributeddb_json_precheck_unit_test.cpp +++ b/frameworks/libs/distributeddb/test/unittest/common/common/distributeddb_json_precheck_unit_test.cpp @@ -220,15 +220,15 @@ HWTEST_F(DistributedDBJsonPrecheckUnitTest, ParseDuplicativeString001, TestSize. */ std::string json = R"({"field1":"123", "field2": true, "field1": 123})"; JsonObject object; - ASSERT_EQ(object.JsonObject::Parse(json), E_OK); + EXPECT_EQ(object.JsonObject::Parse(json), E_OK); /** * @tc.steps: step2. Check field. * @tc.expected: step2. field1 is 123, field2 is True. */ FieldValue value; - ASSERT_EQ(object.GetFieldValueByFieldPath({"field1"}, value), E_OK); + EXPECT_EQ(object.GetFieldValueByFieldPath({"field1"}, value), E_OK); EXPECT_EQ(value.integerValue, 123); - ASSERT_EQ(object.GetFieldValueByFieldPath({"field2"}, value), E_OK); + EXPECT_EQ(object.GetFieldValueByFieldPath({"field2"}, value), E_OK); EXPECT_TRUE(value.boolValue); } @@ -282,10 +282,16 @@ HWTEST_F(DistributedDBJsonPrecheckUnitTest, ParseString003, TestSize.Level0) std::vector data(json.begin(), json.end()); EXPECT_EQ(object.Parse(data), E_OK); std::set outSubPath; + std::map outSubPathType; EXPECT_EQ(object.GetSubFieldPath({"field1"}, outSubPath), -E_NOT_SUPPORT); + EXPECT_EQ(object.GetSubFieldPathAndType({"field1"}, outSubPathType), -E_NOT_SUPPORT); EXPECT_EQ(object.GetSubFieldPath({"field2"}, outSubPath), E_OK); + EXPECT_EQ(object.GetSubFieldPathAndType({"field2"}, outSubPathType), E_OK); std::set actualPath = {{"field2", "field3"}}; EXPECT_EQ(outSubPath, actualPath); + FieldPath inPath; + EXPECT_EQ(object.GetSubFieldPath(inPath, outSubPath), E_OK); + EXPECT_EQ(object.GetSubFieldPathAndType(inPath, outSubPathType), E_OK); } /** @@ -305,10 +311,35 @@ HWTEST_F(DistributedDBJsonPrecheckUnitTest, ParseString004, TestSize.Level0) EXPECT_EQ(object.GetArraySize({"field3"}, size), -E_NOT_SUPPORT); EXPECT_EQ(object.GetArraySize({"field1"}, size), E_OK); EXPECT_EQ(size, 1u); + FieldPath inPath; + EXPECT_EQ(object.GetArraySize(inPath, size), -E_NOT_SUPPORT); std::vector> content; EXPECT_EQ(object.GetArrayContentOfStringOrStringArray({"field1"}, content), E_OK); std::vector> actual = {{"123"}}; EXPECT_EQ(content, actual); + EXPECT_EQ(object.GetArrayContentOfStringOrStringArray(inPath, content), -E_NOT_SUPPORT); +} + +/** + * @tc.name: ParseString005 + * @tc.desc: Parse array. + * @tc.type: FUNC + * @tc.require: + * @tc.author: zqq + */ +HWTEST_F(DistributedDBJsonPrecheckUnitTest, ParseString005, TestSize.Level0) +{ + std::string json = R"({"field1":["123", null, ["456", 789]], "field3":"field3"})"; + JsonObject object; + EXPECT_EQ(object.Parse(json), E_OK); + std::vector> content; + EXPECT_EQ(object.GetArrayContentOfStringOrStringArray({"field1"}, content), -E_NOT_SUPPORT); + + json = R"({"field1":[["456", "789"], []], "field3":"field3"})"; + object = {}; + EXPECT_EQ(object.Parse(json), E_OK); + EXPECT_EQ(object.GetArrayContentOfStringOrStringArray({"field1"}, content), E_OK); + EXPECT_EQ(content.size(), 1u); } /** @@ -335,6 +366,9 @@ HWTEST_F(DistributedDBJsonPrecheckUnitTest, BuildObj001, TestSize.Level0) EXPECT_EQ(obj.InsertField({"null_field"}, FieldType::LEAF_FIELD_NULL, val), E_OK); EXPECT_EQ(obj.InsertField({"array_field"}, FieldType::LEAF_FIELD_ARRAY, val), -E_INVALID_ARGS); EXPECT_EQ(obj.InsertField({"obj_field"}, FieldType::LEAF_FIELD_OBJECT, val), E_OK); + EXPECT_EQ(obj.InsertField({"array_field"}, FieldType::LEAF_FIELD_OBJECT, val), E_OK); + EXPECT_EQ(obj.InsertField({"array_field", "array_inner"}, FieldType::LEAF_FIELD_OBJECT, val), E_OK); + EXPECT_EQ(obj.InsertField({"array_field", "array_inner", "inner"}, FieldType::LEAF_FIELD_OBJECT, val), E_OK); EXPECT_EQ(obj.DeleteField({"obj_field"}), E_OK); LOGI("json is %s", obj.ToString().c_str()); } @@ -353,7 +387,36 @@ HWTEST_F(DistributedDBJsonPrecheckUnitTest, BuildObj002, TestSize.Level0) EXPECT_EQ(obj.Parse(json), E_OK); FieldValue val; val.stringValue = "str"; + EXPECT_EQ(obj.InsertField({"array_field1"}, FieldType::LEAF_FIELD_STRING, val, true), E_OK); + EXPECT_EQ(obj.InsertField({"array_field2"}, FieldType::LEAF_FIELD_STRING, val), E_OK); EXPECT_EQ(obj.InsertField({"array_field"}, FieldType::LEAF_FIELD_STRING, val, true), E_OK); + EXPECT_EQ(obj.InsertField({"array_field"}, FieldType::LEAF_FIELD_STRING, val), E_OK); LOGI("json is %s", obj.ToString().c_str()); } + +/** + * @tc.name: InnerError001 + * @tc.desc: Inner error in json obj. + * @tc.type: FUNC + * @tc.require: + * @tc.author: zqq + */ +HWTEST_F(DistributedDBJsonPrecheckUnitTest, InnerError001, TestSize.Level0) +{ + auto obj = CJsonObject::Parse(""); + std::vector outArray; + JsonObject jsonObject; + EXPECT_EQ(jsonObject.GetStringArrayContentByCJsonValue(obj, outArray), -E_NOT_SUPPORT); + std::string json = R"({"array_field":[123]})"; + obj = CJsonObject::Parse(json); + EXPECT_EQ(jsonObject.GetStringArrayContentByCJsonValue(obj, outArray), -E_NOT_SUPPORT); + + FieldType type; + EXPECT_EQ(jsonObject.GetFieldTypeByCJsonValue(obj, type), E_OK); + EXPECT_EQ(type, FieldType::INTERNAL_FIELD_OBJECT); + json = R"({})"; + obj = CJsonObject::Parse(json); + EXPECT_EQ(jsonObject.GetFieldTypeByCJsonValue(obj, type), E_OK); + EXPECT_EQ(type, FieldType::LEAF_FIELD_OBJECT); +} #endif \ No newline at end of file -- Gitee From b91e8ab750803cba0ad843bc67bd17117d7691ca Mon Sep 17 00:00:00 2001 From: zqq Date: Wed, 21 May 2025 19:58:58 +0800 Subject: [PATCH 4/5] add ut Signed-off-by: zqq --- frameworks/libs/distributeddb/BUILD.gn | 2 +- .../common/include/json_object.h | 6 ++-- .../src/cloud/cloud_storage_utils_client.cpp | 5 +++- .../distributeddb_json_precheck_unit_test.cpp | 30 +++++++++++++++++++ 4 files changed, 38 insertions(+), 5 deletions(-) diff --git a/frameworks/libs/distributeddb/BUILD.gn b/frameworks/libs/distributeddb/BUILD.gn index 3d61737f1..7e87485b0 100644 --- a/frameworks/libs/distributeddb/BUILD.gn +++ b/frameworks/libs/distributeddb/BUILD.gn @@ -120,6 +120,7 @@ ohos_shared_library("distributeddb") { deps = [ "gaussdb_rd:gaussdb_rd" ] external_deps = [ + "cJSON:cjson", "c_utils:utils", "ffrt:libffrt", "hilog:libhilog", @@ -129,7 +130,6 @@ ohos_shared_library("distributeddb") { ] public_external_deps = [ - "cJSON:cjson", "openssl:libcrypto_shared", "sqlite:sqlite", ] diff --git a/frameworks/libs/distributeddb/common/include/json_object.h b/frameworks/libs/distributeddb/common/include/json_object.h index 4c0188d43..80c055fc3 100644 --- a/frameworks/libs/distributeddb/common/include/json_object.h +++ b/frameworks/libs/distributeddb/common/include/json_object.h @@ -92,6 +92,9 @@ public: // Should be called on an valid JsonObject. Never turn into invalid after call. An empty inPath is not allowed. // If inPath not exist, returns -E_JSON_DELETE_PATH_NOT_FOUND. Otherwise, delete field from its parent returns E_OK; int DeleteField(const FieldPath &inPath); + + // create if path not exist, exact contain {field, nearest} + int MoveToPath(const FieldPath &inPath, CJsonObject &exact, CJsonObject &nearest); #ifndef OMIT_JSON // Auxiliary Method: If inPath not refer to an array, return error. If not all members are string, return error. int GetStringArrayContentByCJsonValue(const CJsonObject &value, std::vector &outStringArray) const; @@ -113,9 +116,6 @@ private: int LocateCJsonValueByFieldPath(const FieldPath &inPath, CJsonObject &exact, CJsonObject &nearest, uint32_t &nearDepth); - // create if path not exist, exact contain {field, nearest} - int MoveToPath(const FieldPath &inPath, CJsonObject &exact, CJsonObject &nearest); - static uint32_t maxNestDepth_; bool isValid_ = false; diff --git a/frameworks/libs/distributeddb/storage/src/cloud/cloud_storage_utils_client.cpp b/frameworks/libs/distributeddb/storage/src/cloud/cloud_storage_utils_client.cpp index e923f67cf..deb2b564e 100644 --- a/frameworks/libs/distributeddb/storage/src/cloud/cloud_storage_utils_client.cpp +++ b/frameworks/libs/distributeddb/storage/src/cloud/cloud_storage_utils_client.cpp @@ -15,6 +15,7 @@ #include "cloud/cloud_storage_utils.h" +#include #include "db_common.h" #include "runtime_context.h" @@ -51,7 +52,9 @@ int CloudStorageUtils::DoubleToVector(const VBucket &vBucket, const Field &field if (CloudStorageUtils::GetValueFromVBucket(field.colName, vBucket, val) != E_OK) { // LCOV_EXCL_BR_LINE return -E_CLOUD_ERROR; } - DBCommon::StringToVector(std::to_string(val), value); + std::ostringstream s; + s << val; + DBCommon::StringToVector(s.str(), value); return E_OK; } diff --git a/frameworks/libs/distributeddb/test/unittest/common/common/distributeddb_json_precheck_unit_test.cpp b/frameworks/libs/distributeddb/test/unittest/common/common/distributeddb_json_precheck_unit_test.cpp index 4f50c2273..faf532c53 100644 --- a/frameworks/libs/distributeddb/test/unittest/common/common/distributeddb_json_precheck_unit_test.cpp +++ b/frameworks/libs/distributeddb/test/unittest/common/common/distributeddb_json_precheck_unit_test.cpp @@ -394,6 +394,36 @@ HWTEST_F(DistributedDBJsonPrecheckUnitTest, BuildObj002, TestSize.Level0) LOGI("json is %s", obj.ToString().c_str()); } +/** + * @tc.name: BuildObj003 + * @tc.desc: Build json obj. + * @tc.type: FUNC + * @tc.require: + * @tc.author: zqq + */ +HWTEST_F(DistributedDBJsonPrecheckUnitTest, BuildObj003, TestSize.Level0) +{ + std::string json = R"({"field":"field"})"; + JsonObject obj; + EXPECT_EQ(obj.Parse(json), E_OK); + + CJsonObject extract; + CJsonObject nearest; + EXPECT_EQ(obj.MoveToPath({"field", "field"}, extract, nearest), -E_JSON_INSERT_PATH_CONFLICT); + LOGI("json is %s", obj.ToString().c_str()); + + CJsonObject nullObj; + FieldType outType; + EXPECT_EQ(obj.GetFieldTypeByCJsonValue(nullObj, outType), E_OK); + auto cjsonPtr = cJSON_CreateRaw(json.c_str()); + CJsonObject rawObj(cjsonPtr, true); + EXPECT_EQ(obj.GetFieldTypeByCJsonValue(rawObj, outType), -E_NOT_SUPPORT); + + cjsonPtr = cJSON_CreateNumber(std::numeric_limits::infinity()); + CJsonObject doubleObj(cjsonPtr, true); + EXPECT_EQ(obj.GetFieldTypeByCJsonValue(doubleObj, outType), -E_NOT_SUPPORT); +} + /** * @tc.name: InnerError001 * @tc.desc: Inner error in json obj. -- Gitee From 5eae3a0360988988df93c6922699056ad09775c7 Mon Sep 17 00:00:00 2001 From: zqq Date: Mon, 26 May 2025 16:44:12 +0800 Subject: [PATCH 5/5] modify ut Signed-off-by: zqq --- .../common/distributeddb_json_precheck_unit_test.cpp | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/frameworks/libs/distributeddb/test/unittest/common/common/distributeddb_json_precheck_unit_test.cpp b/frameworks/libs/distributeddb/test/unittest/common/common/distributeddb_json_precheck_unit_test.cpp index faf532c53..4d25a53ab 100644 --- a/frameworks/libs/distributeddb/test/unittest/common/common/distributeddb_json_precheck_unit_test.cpp +++ b/frameworks/libs/distributeddb/test/unittest/common/common/distributeddb_json_precheck_unit_test.cpp @@ -336,9 +336,9 @@ HWTEST_F(DistributedDBJsonPrecheckUnitTest, ParseString005, TestSize.Level0) EXPECT_EQ(object.GetArrayContentOfStringOrStringArray({"field1"}, content), -E_NOT_SUPPORT); json = R"({"field1":[["456", "789"], []], "field3":"field3"})"; - object = {}; - EXPECT_EQ(object.Parse(json), E_OK); - EXPECT_EQ(object.GetArrayContentOfStringOrStringArray({"field1"}, content), E_OK); + JsonObject complexObject; + EXPECT_EQ(complexObject.Parse(json), E_OK); + EXPECT_EQ(complexObject.GetArrayContentOfStringOrStringArray({"field1"}, content), E_OK); EXPECT_EQ(content.size(), 1u); } @@ -418,10 +418,11 @@ HWTEST_F(DistributedDBJsonPrecheckUnitTest, BuildObj003, TestSize.Level0) auto cjsonPtr = cJSON_CreateRaw(json.c_str()); CJsonObject rawObj(cjsonPtr, true); EXPECT_EQ(obj.GetFieldTypeByCJsonValue(rawObj, outType), -E_NOT_SUPPORT); - + cJSON_Delete(cjsonPtr); cjsonPtr = cJSON_CreateNumber(std::numeric_limits::infinity()); CJsonObject doubleObj(cjsonPtr, true); EXPECT_EQ(obj.GetFieldTypeByCJsonValue(doubleObj, outType), -E_NOT_SUPPORT); + cJSON_Delete(cjsonPtr); } /** -- Gitee