1 /*
2  * Copyright (c) 2023-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 "module_info.h"
17 
18 #include "hilog_tag_wrapper.h"
19 #include "json_util.h"
20 #include "nlohmann/json.hpp"
21 #include "string_ex.h"
22 
23 namespace OHOS {
24 namespace AppExecFwk {
25 namespace {
26 const std::string MODULE_INFO_MODULE_SOURCE_DIR = "moduleSourceDir";
27 const std::string MODULE_INFO_PRELOADS = "preloads";
28 }
to_json(nlohmann::json & jsonObject,const ModuleInfo & moduleInfo)29 void to_json(nlohmann::json &jsonObject, const ModuleInfo &moduleInfo)
30 {
31     jsonObject = nlohmann::json {
32         {Constants::MODULE_NAME, moduleInfo.moduleName},
33         {MODULE_INFO_MODULE_SOURCE_DIR, moduleInfo.moduleSourceDir},
34         {MODULE_INFO_PRELOADS, moduleInfo.preloads}
35     };
36 }
37 
from_json(const nlohmann::json & jsonObject,ModuleInfo & moduleInfo)38 void from_json(const nlohmann::json &jsonObject, ModuleInfo &moduleInfo)
39 {
40     const auto &jsonObjectEnd = jsonObject.end();
41     int32_t parseResult = ERR_OK;
42     GetValueIfFindKey<std::string>(jsonObject,
43         jsonObjectEnd,
44         Constants::MODULE_NAME,
45         moduleInfo.moduleName,
46         JsonType::STRING,
47         false,
48         parseResult,
49         ArrayType::NOT_ARRAY);
50     GetValueIfFindKey<std::string>(jsonObject,
51         jsonObjectEnd,
52         MODULE_INFO_MODULE_SOURCE_DIR,
53         moduleInfo.moduleSourceDir,
54         JsonType::STRING,
55         false,
56         parseResult,
57         ArrayType::NOT_ARRAY);
58     GetValueIfFindKey<std::vector<std::string>>(jsonObject,
59         jsonObjectEnd,
60         MODULE_INFO_PRELOADS,
61         moduleInfo.preloads,
62         JsonType::ARRAY,
63         false,
64         parseResult,
65         ArrayType::STRING);
66     if (parseResult != ERR_OK) {
67         TAG_LOGE(AAFwkTag::ABILITY_SIM,
68             "read module moduleInfo error:%{public}d", parseResult);
69     }
70 }
71 } // namespace AppExecFwk
72 } // namespace OHOS
73