1 /*
2 * Copyright (c) 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 "json_cfg.h"
17
18 namespace OHOS::Security::SecurityGuard {
Unmarshal(uint64_t & data,nlohmann::json jsonObj,std::string key)19 bool JsonCfg::Unmarshal(uint64_t &data, nlohmann::json jsonObj, std::string key)
20 {
21 JSON_CHECK_HELPER_RETURN_IF_FAILED(jsonObj, key, number, false);
22 data = jsonObj.at(key).get<uint64_t>();
23 return true;
24 }
25
Unmarshal(int64_t & data,nlohmann::json jsonObj,std::string key)26 bool JsonCfg::Unmarshal(int64_t &data, nlohmann::json jsonObj, std::string key)
27 {
28 JSON_CHECK_HELPER_RETURN_IF_FAILED(jsonObj, key, number, false);
29 data = jsonObj.at(key).get<int64_t>();
30 return true;
31 }
32
Unmarshal(uint32_t & data,nlohmann::json jsonObj,std::string key)33 bool JsonCfg::Unmarshal(uint32_t &data, nlohmann::json jsonObj, std::string key)
34 {
35 JSON_CHECK_HELPER_RETURN_IF_FAILED(jsonObj, key, number, false);
36 data = jsonObj.at(key).get<uint32_t>();
37 return true;
38 }
39
Unmarshal(int32_t & data,nlohmann::json jsonObj,std::string key)40 bool JsonCfg::Unmarshal(int32_t &data, nlohmann::json jsonObj, std::string key)
41 {
42 JSON_CHECK_HELPER_RETURN_IF_FAILED(jsonObj, key, number, false);
43 data = jsonObj.at(key).get<int32_t>();
44 return true;
45 }
46
Unmarshal(std::string & data,nlohmann::json jsonObj,std::string key)47 bool JsonCfg::Unmarshal(std::string &data, nlohmann::json jsonObj, std::string key)
48 {
49 JSON_CHECK_HELPER_RETURN_IF_FAILED(jsonObj, key, string, false);
50 data = jsonObj.at(key).get<std::string>();
51 return true;
52 }
53
Unmarshal(std::vector<int32_t> & data,nlohmann::json jsonObj,std::string key)54 bool JsonCfg::Unmarshal(std::vector<int32_t> &data, nlohmann::json jsonObj, std::string key)
55 {
56 JSON_CHECK_HELPER_RETURN_IF_FAILED(jsonObj, key, array, false);
57 nlohmann::json arrays = jsonObj.at(key);
58 for (const auto &element : arrays) {
59 if (!element.is_number()) {
60 return false;
61 }
62 data.emplace_back(element.get<int32_t>());
63 }
64 return true;
65 }
66
Unmarshal(std::vector<std::string> & data,nlohmann::json jsonObj,std::string key)67 bool JsonCfg::Unmarshal(std::vector<std::string> &data, nlohmann::json jsonObj, std::string key)
68 {
69 JSON_CHECK_HELPER_RETURN_IF_FAILED(jsonObj, key, array, false);
70 nlohmann::json arrays = jsonObj.at(key);
71 for (const auto &element : arrays) {
72 if (!element.is_string()) {
73 return false;
74 }
75 data.emplace_back(element.get<std::string>());
76 }
77 return true;
78 }
79
Unmarshal(std::vector<int64_t> & data,nlohmann::json jsonObj,std::string key)80 bool JsonCfg::Unmarshal(std::vector<int64_t> &data, nlohmann::json jsonObj, std::string key)
81 {
82 JSON_CHECK_HELPER_RETURN_IF_FAILED(jsonObj, key, array, false);
83 nlohmann::json arrays = jsonObj.at(key);
84 for (const auto &element : arrays) {
85 if (!element.is_number()) {
86 return false;
87 }
88 data.emplace_back(element.get<int64_t>());
89 }
90 return true;
91 }
92 } // namespace OHOS::Security::SecurityGuard