1 /*
2 * Copyright (C) 2021-2022 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 "init/json_parser_utils.h"
17
18 #include <fstream>
19 #include <sstream>
20
21 #include "common/hap_verify_log.h"
22
23 namespace OHOS {
24 namespace Security {
25 namespace Verify {
ReadTrustedRootCAFromJson(nlohmann::json & jsonObj,const std::string & jsonPath,std::string & error)26 bool JsonParserUtils::ReadTrustedRootCAFromJson(nlohmann::json& jsonObj,
27 const std::string& jsonPath, std::string& error)
28 {
29 std::ifstream jsonFileStream;
30 jsonFileStream.open(jsonPath.c_str(), std::ios::in);
31 if (!jsonFileStream.is_open()) {
32 error += "open file failed";
33 return false;
34 }
35 std::ostringstream buf;
36 char ch;
37 while (buf && jsonFileStream.get(ch)) {
38 buf.put(ch);
39 }
40 jsonFileStream.close();
41
42 std::string jsonStr = buf.str();
43 jsonObj = nlohmann::json::parse(jsonStr, nullptr, false);
44 if (!jsonObj.is_structured()) {
45 error += "parse jsonStr failed";
46 return false;
47 }
48 return true;
49 }
50
GetJsonString(const nlohmann::json & json,const std::string & key,std::string & value)51 bool JsonParserUtils::GetJsonString(const nlohmann::json& json, const std::string& key, std::string& value)
52 {
53 if (!json.is_object()) {
54 return false;
55 }
56 if (json.find(key) != json.end() && json.at(key).is_string()) {
57 value = json.at(key).get<std::string>();
58 }
59 return true;
60 }
61
GetJsonInt(const nlohmann::json & json,const std::string & key,int & value)62 bool JsonParserUtils::GetJsonInt(const nlohmann::json& json, const std::string& key, int& value)
63 {
64 if (!json.is_object()) {
65 return false;
66 }
67 if (json.find(key) != json.end() && json.at(key).is_number()) {
68 value = json.at(key).get<int>();
69 }
70 return true;
71 }
72
GetJsonStringVec(const nlohmann::json & json,const std::string & key,StringVec & value)73 bool JsonParserUtils::GetJsonStringVec(const nlohmann::json& json, const std::string& key, StringVec& value)
74 {
75 if (!json.is_object()) {
76 return false;
77 }
78 if (json.find(key) != json.end() && json.at(key).is_array()) {
79 value = json.at(key).get<StringVec>();
80 }
81 return true;
82 }
83
ParseJsonToObjVec(const nlohmann::json & json,const std::string & key,JsonObjVec & jsonObjVec)84 bool JsonParserUtils::ParseJsonToObjVec(const nlohmann::json& json, const std::string& key, JsonObjVec& jsonObjVec)
85 {
86 if (!json.is_object()) {
87 return false;
88 }
89 if (json.find(key) != json.end() && json.at(key).is_array()) {
90 jsonObjVec = json.at(key).get<JsonObjVec>();
91 }
92 return true;
93 }
94
ParseJsonToMap(const nlohmann::json & json,JsonMap & jsonMap)95 void JsonParserUtils::ParseJsonToMap(const nlohmann::json& json, JsonMap& jsonMap)
96 {
97 for (auto it = json.begin(); it != json.end(); it++) {
98 if (it.value().is_string()) {
99 jsonMap[it.key()] = it.value().get<std::string>();
100 }
101 }
102 }
103 } // namespace Verify
104 } // namespace Security
105 } // namespace OHOS
106