1 /*
2 * Copyright (c) 2021 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 "module_info.h"
17
18 #include "nlohmann/json.hpp"
19 #include "string_ex.h"
20
21 #include "json_util.h"
22 #include "parcel_macro.h"
23
24 namespace OHOS {
25 namespace AppExecFwk {
26 namespace {
27 const std::string MODULE_INFO_MODULE_SOURCE_DIR = "moduleSourceDir";
28 const std::string MODULE_INFO_PRELOADS = "preloads";
29 }
30
ReadFromParcel(Parcel & parcel)31 bool ModuleInfo::ReadFromParcel(Parcel &parcel)
32 {
33 moduleName = Str16ToStr8(parcel.ReadString16());
34 moduleSourceDir = Str16ToStr8(parcel.ReadString16());
35 int32_t preloadsSize;
36 READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, preloadsSize);
37 for (auto i = 0; i < preloadsSize; i++) {
38 preloads.emplace_back(Str16ToStr8(parcel.ReadString16()));
39 }
40 return true;
41 }
42
Unmarshalling(Parcel & parcel)43 ModuleInfo *ModuleInfo::Unmarshalling(Parcel &parcel)
44 {
45 ModuleInfo *info = new (std::nothrow) ModuleInfo();
46 if (info && !info->ReadFromParcel(parcel)) {
47 APP_LOGW("read from parcel failed");
48 delete info;
49 info = nullptr;
50 }
51 return info;
52 }
53
Marshalling(Parcel & parcel) const54 bool ModuleInfo::Marshalling(Parcel &parcel) const
55 {
56 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(moduleName));
57 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(moduleSourceDir));
58 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, preloads.size());
59 for (auto &preload : preloads) {
60 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(preload));
61 }
62 return true;
63 }
64
to_json(nlohmann::json & jsonObject,const ModuleInfo & moduleInfo)65 void to_json(nlohmann::json &jsonObject, const ModuleInfo &moduleInfo)
66 {
67 jsonObject = nlohmann::json {
68 {Constants::MODULE_NAME, moduleInfo.moduleName},
69 {MODULE_INFO_MODULE_SOURCE_DIR, moduleInfo.moduleSourceDir},
70 {MODULE_INFO_PRELOADS, moduleInfo.preloads}
71 };
72 }
73
from_json(const nlohmann::json & jsonObject,ModuleInfo & moduleInfo)74 void from_json(const nlohmann::json &jsonObject, ModuleInfo &moduleInfo)
75 {
76 const auto &jsonObjectEnd = jsonObject.end();
77 int32_t parseResult = ERR_OK;
78 GetValueIfFindKey<std::string>(jsonObject,
79 jsonObjectEnd,
80 Constants::MODULE_NAME,
81 moduleInfo.moduleName,
82 JsonType::STRING,
83 false,
84 parseResult,
85 ArrayType::NOT_ARRAY);
86 GetValueIfFindKey<std::string>(jsonObject,
87 jsonObjectEnd,
88 MODULE_INFO_MODULE_SOURCE_DIR,
89 moduleInfo.moduleSourceDir,
90 JsonType::STRING,
91 false,
92 parseResult,
93 ArrayType::NOT_ARRAY);
94 GetValueIfFindKey<std::vector<std::string>>(jsonObject,
95 jsonObjectEnd,
96 MODULE_INFO_PRELOADS,
97 moduleInfo.preloads,
98 JsonType::ARRAY,
99 false,
100 parseResult,
101 ArrayType::STRING);
102 }
103 } // namespace AppExecFwk
104 } // namespace OHOS
105