• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "json_util.h"
16 
17 namespace OHOS {
18 namespace AppExecFwk {
GetStrValueIfFindKey(const nlohmann::json & jsonObject,const nlohmann::detail::iter_impl<const nlohmann::json> & end,const std::string & key,std::string & data,bool isNecessary,int32_t & parseResult)19 void BMSJsonUtil::GetStrValueIfFindKey(const nlohmann::json &jsonObject,
20     const nlohmann::detail::iter_impl<const nlohmann::json> &end,
21     const std::string &key, std::string &data, bool isNecessary, int32_t &parseResult)
22 {
23     if (parseResult) {
24         return;
25     }
26     if (jsonObject.find(key) != end) {
27         if (!jsonObject.at(key).is_string()) {
28             APP_LOGE("type error %{public}s not string", key.c_str());
29             parseResult = ERR_APPEXECFWK_PARSE_PROFILE_PROP_TYPE_ERROR;
30             return;
31         }
32         data = jsonObject.at(key).get<std::string>();
33         if (data.length() > Constants::MAX_JSON_STRING_LENGTH) {
34             parseResult = ERR_APPEXECFWK_PARSE_PROFILE_PROP_SIZE_CHECK_ERROR;
35         }
36         return;
37     }
38     if (isNecessary) {
39         APP_LOGE("profile prop %{public}s mission", key.c_str());
40         parseResult = ERR_APPEXECFWK_PARSE_PROFILE_MISSING_PROP;
41     }
42 }
GetBoolValueIfFindKey(const nlohmann::json & jsonObject,const nlohmann::detail::iter_impl<const nlohmann::json> & end,const std::string & key,bool & data,bool isNecessary,int32_t & parseResult)43 void BMSJsonUtil::GetBoolValueIfFindKey(const nlohmann::json &jsonObject,
44     const nlohmann::detail::iter_impl<const nlohmann::json> &end,
45     const std::string &key, bool &data, bool isNecessary, int32_t &parseResult)
46 {
47     if (parseResult) {
48         return;
49     }
50     if (jsonObject.find(key) != end) {
51         if (!jsonObject.at(key).is_boolean()) {
52             APP_LOGE("type error %{public}s not bool", key.c_str());
53             parseResult = ERR_APPEXECFWK_PARSE_PROFILE_PROP_TYPE_ERROR;
54             return;
55         }
56         data = jsonObject.at(key).get<bool>();
57         return;
58     }
59     if (isNecessary) {
60         APP_LOGE("profile prop %{public}s mission", key.c_str());
61         parseResult = ERR_APPEXECFWK_PARSE_PROFILE_MISSING_PROP;
62     }
63 }
64 }  // namespace AppExecFwk
65 }  // namespace OHOS