1 /*
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "normalize_version_utils.h"
17
18 #include "log.h"
19 #include "pt_json.h"
20
21 namespace OHOS {
22 namespace AppPackingTool {
23 namespace {
24 const std::string ORIGIN_VERSION_CODE = "originVersionCode";
25 const std::string ORIGIN_VERSION_NAME = "originVersionName";
26 const std::string MODULE_NAME = "moduleName";
27 }
28
ToString(const NormalizeVersion & normalizeVersion)29 std::string NormalizeVersionUtils::ToString(const NormalizeVersion& normalizeVersion)
30 {
31 std::unique_ptr<PtJson> ptJson = PtJson::CreateObject();
32 if (!ptJson->Add(MODULE_NAME.c_str(), normalizeVersion.moduleName.c_str())) {
33 return "";
34 }
35 if (!ptJson->Add(ORIGIN_VERSION_CODE.c_str(), normalizeVersion.originVersionCode)) {
36 return "";
37 }
38 if (!ptJson->Add(ORIGIN_VERSION_NAME.c_str(), normalizeVersion.originVersionName.c_str())) {
39 return "";
40 }
41 return ptJson->Stringify();
42 }
43
ArrayToString(const std::list<NormalizeVersion> & normalizeVersions)44 std::string NormalizeVersionUtils::ArrayToString(const std::list<NormalizeVersion>& normalizeVersions)
45 {
46 std::unique_ptr<PtJson> versionsJson = PtJson::CreateArray();
47 for (auto& normalizeVersion : normalizeVersions) {
48 std::unique_ptr<PtJson> versionJson = PtJson::CreateObject();
49 if (!versionJson->Add(MODULE_NAME.c_str(), normalizeVersion.moduleName.c_str())) {
50 return "";
51 }
52 if (!versionJson->Add(ORIGIN_VERSION_CODE.c_str(), normalizeVersion.originVersionCode)) {
53 return "";
54 }
55 if (!versionJson->Add(ORIGIN_VERSION_NAME.c_str(), normalizeVersion.originVersionName.c_str())) {
56 return "";
57 }
58 versionsJson->Push(versionJson);
59 }
60 return versionsJson->Stringify();
61 }
62 } // namespace AppPackingTool
63 } // namespace OHOS
64